Opencv source code parsing (5): A simple understanding of the commandlineparser class

Source: Internet
Author: User

It seems that in the C version of opencv, The commandlineparser class should have not been found before opencv1.0. Recently, we have seen that the self-contained samples in Versions later than opencv2.3, many of which use commandlineparser.

In this case, what is the role of this class? We can guess from the name that this is a command line parsing class. Because we know that opencv is an open-source library, there are very few graphical operations APIs, which are basically executed based on command lines. This class is mainly used to help you reduce the workload when using the command line. At present, you can only understand this benefit, and you do not know whether to make a mistake.

For example, in a class in samples, the main function defines a keys pointer.

 

Const char * keys =

{

"{C | camera | false | use camera or not }"

"{Fn | file_name | petsd1tec1. Avi | movie file }"

};

 

The following statements are used in the main function:

 

Commandlineparser Parser (argc, argv, keys );

Bool usecamera = parser. Get <bool> ("camera ");

String file = parser. Get <string> ("file_name ");

Videocapture cap;

Bool update_bg_model = true;

 

If (usecamera)

Cap. Open (0 );

Else

Cap. Open (file. c_str ());

Parser. printparams ();

 

The first line is the constructor of this class. The first two parameters are passed through the command line. The first 3rd parameters are the defined keys. The keys structure has a certain regularity, for example, "{c | camera | false | use camera or not}" is caused by braces and double quotation marks, and the content in the center is divided into 4 breaks, separated by "|, this parameter indicates the abbreviation, file source, file value, and help statement. The second and third lines indicate opening the camera and opening the file, and the file names are all in the Keys pointer.

The last line is the parameters in the print keys, as shown below:

  

Begin, you must enter the file path and various parameters in the command line, and the format of the input parameter must be the same as that of the IF statement in the code to determine the content format. Accidentally, you will lose an error, inconvenient. In addition, if you want to change the input format, you must change the format in the main function file. Now with this class, you only need to change the content in keys, and you can directly use F5 in vs during the runtime without running the CMD command line with parameters. Finally, this class encapsulates many functions that can be used directly, but this is the advantage of the class structure.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.