[OpenCV] Samples 13: opencv_version

cv::CommandLineParser的使用。

I suppose CommandLineParser::has("something") should be true when the command line has --something in it.

./a.out -h
./a.out --help

打印keys的相关内容。

 

#include <opencv2/core/utility.hpp>
#include <iostream>

using namespace std;

const char* keys =
{
    "{ b build | | print complete build info }"
    "{ h help  | | print this help           }"
};

int main(int argc, const char* argv[])
{
    cv::CommandLineParser parser(argc, argv, keys);

    if (parser.has("help"))
    {
        parser.printMessage();
    }
    else if (!parser.check())
    {
        parser.printErrors();
    }
    else if (parser.has("build"))
    {
        std::cout << cv::getBuildInformation() << std::endl;
    }
    else
    {
        std::cout << "Welcome to OpenCV " << CV_VERSION << std::endl;
    }

    return 0;
}

  

一个更好的例子

For example, define:

String keys = "{N count||}";

Call:

 1 $ ./my-app -N=20
 2 # or
 3 $ ./my-app --count=20  // 注意等号的应用

 


 

Keys赋值:

const string keys =
    "{help h usage ? |      | print this message   }"
    "{@image1        |      | image1 for compare   }"
    "{@image2        |<none>| image2 for compare   }"
    "{@repeat        |1     | number               }"
    "{path           |/home/unsw     | path to file         }"
    "{fps            | -1.0 | fps for output video }"
    "{N count        |100   | count of objects     }"
    "{ts timestamp   |      | use time stamp       }"
    ;

如此解析:

    cv::CommandLineParser parser(argc, argv, keys);
    parser.about("Application name v1.0.0");
    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }
    int N = parser.get<int>("N");
    cout << "N = " << N << endl;
    double fps = parser.get<double>("fps");
    string path = parser.get<string>("path");
    cout << "path: " << path << endl;
    time_t use_time_stamp = parser.has("timestamp");
    string img1 = parser.get<string>(0);
    cout << "img1: " << img1 << endl;
    string img2 = parser.get<string>(1);
    cout << "img2: " << img2 << endl;
    int repeat = parser.get<int>(2);
    cout << "repeat: " << repeat << endl;
    if (!parser.check())
    {
        parser.printErrors();
        return 0;
    }

运行结果:

没毛病!

 

posted @ 2016-12-03 14:09  郝壹贰叁  阅读(1106)  评论(0编辑  收藏  举报