[opencv] the role of Keys in CommandLineParser

Posted by e11even on Sat, 25 Dec 2021 19:45:06 +0100

I The role of CommandLineParser

This class belongs to one of many classes in opencv, which is used to parse the command line. When (int argc,char** argv) in the main function receives the input parameter, it can parse the input command through CommandLineParser to make different responses. One of the advantages of this parsing class is that when your application (an. exe program under windows or an executable file under linux) just needs to input parameters, but others don't know how to open the console to input parameters, you can use this parsing class to set. If there are input parameters, you can follow the parameters, and if not, you can follow the default parameters, so that the program can run normally.

For example, opencv's official routine analysis (this routine will be used for analysis below): open a photo. When you use the console to execute the program and bring the photo path, the photo under the path you specified will be opened. If you simply open the program without entering parameters, the lena.jpg picture in the default path of the program will be opened.

II Header file

The CommandLineParser command line parsing class requires the following header files to be called:

#include <opencv2/core/utility.hpp>

III Class member function

public:
 CommandLineParser (int argc, const char *const argv[], const String &keys);//Constructor
 CommandLineParser (const CommandLineParser &parser);
 ~CommandLineParser ();//Destructor
 void   about (const String &message);
 bool   check () const;
 
template<typename T >
T   get (const String &name, bool space_delete=true) const; //Get parameter value function
template<typename T >
T   get (int index, bool space_delete=true) const; //Get parameter value function
 
String  getPathToApplication () const;
bool    has (const String &name) const; //Function to determine whether the name character exists
CommandLineParser & operator= (const CommandLineParser &parser);
void    printErrors () const; //Print wrong function
void    printMessage () const;  //Function to print information
 
protected:
void    getByIndex (int index, bool space_delete, int type, void *dst) const;
void    getByName (const String &name, bool space_delete, int type, void *dst) const;
Impl *  impl;

The above notes indicate several commonly used functions.

IV Use example

Note: the following code refers to the official example code of OpenCV. The path is opencv-x.x.xx (version number) / samples / cpp / image cpp, other programming languages can refer to other folders (at the same level as cpp folder). The main code segments are as follows:

int main( int argc, char** argv )
{
    cv::CommandLineParser parser(argc, argv, "{help h | |}{@image|lena.jpg|}");
    if (parser.has("help"))
    {
        help();
        return 0;
    }
    string imagename = parser.get<string>("@image");
 
    //  The following is the operation code implementation after obtaining the photo path//
    //  For details, please see your opencv routine, which will not be described again//
}

First, initialize the parsing class:

cv::CommandLineParser parser(argc, argv, "{help h | |}{@image|lena.jpg|}");

The keys value in this constructor is assigned a value:

"{help h | |}{@image|lena.jpg|}"

The value consists of the symbol '' and {} and |, and the fixed format is:

{name_param|default|value|description}
It can be understood as:
{abbreviation | Key name | Key value | remarks/explain}

Where {help h |} indicates that the key name is help or h, while {@ image | lena.jpg |} indicates that the key name is @ image and the key value is Lena jpg.

The next step is to make different corresponding changes to the obtained parameter values:

    if (parser.has("help"))
    {
        help();
        return 0;
    }
    string imagename = parser.get<string>("@image");

The help() function is as follows:

static void help()
{
    cout <<
    "\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
    "It shows reading of images, converting to planes and merging back, color conversion\n"
    "and also iterating through pixels.\n"
    "Call:\n"
    "./image [image-name Default: lena.jpg]\n" << endl;
}

The help() function is called when you enter the following command on the command line:

./image -help  perhaps
./image -h

The output is as follows:

When no command is entered on the command line, the default Lena Jpg pictures are displayed:

When we enter the path, the image of the path you specified will be displayed:

Here you can find the difference between adding "@" and not adding "@". Adding "@" is an input parameter by default and can be entered directly on the command line. Without "@", you need to add "-" on the command line, such as "- help" or "- h"

Topics: C C++ OpenCV Computer Vision