无法在 opencv 中使用命令行解析器

Joh*_*Lui 1 c++ opencv

我有以下代码:

int main(int argc, char** argv)
{
    cv::CommandLineParser parser(argc, argv,
            "{eyes||}{nose||}{mouth||}{help h||}");
    if (parser.has("help"))
    {
        help();
        return 0;
    }
    input_image_path = parser.get<string>(0);
    face_cascade_path = parser.get<string>(1);
    eye_cascade_path = parser.has("eyes") ? parser.get<string>("eyes") : "";
    nose_cascade_path = parser.has("nose") ? parser.get<string>("nose") : "";
    mouth_cascade_path = parser.has("mouth") ? parser.get<string>("mouth") : "";
    if (input_image_path.empty() || face_cascade_path.empty())
    {
        cout << "IMAGE or FACE_CASCADE are not specified\n";
        return 1;
    }
    // Load image and cascade classifier files
    Mat image;
    image = imread(input_image_path);
Run Code Online (Sandbox Code Playgroud)

当我按如下方式运行代码时: ./code help

或者作为,

./code eyes: /home/Users/Matt/rob.jpg
Run Code Online (Sandbox Code Playgroud)

它总是给我错误:IMAGE or FACE_CASCADE are not specified。我在这里错过了什么吗?我一直在关注这个链接http://docs.opencv.org/trunk/d0/d2e/classcv_1_1CommandLineParser.html,我正在输入那里指定的命令,但它仍然没有接受参数。

Mik*_*iki 6

有几个错误:

  1. 您没有为指定位置参数input_image_path,并face_cascade_pathparser钥匙。
  2. 要使用非位置参数,您需要使用-arg=,例如:-eyes=/path/to/eyes.xml

您可以使用正确的键和示例调用检查下面的代码。为完整起见,我将图像指定为强制(<none>默认值),并指定了人脸级联路径的默认值。

此代码在 OpenCV 3.1 中按预期工作。以前的版本不支持<none>.

您可以将这个程序称为:

./foo -help  // -> show help
./foo /path/to/image // -> use dafault face cascade
./foo /path/to/image /path/to/face_cascade
./foo /path/to/image /path/to/face_cascade -eyes=/path/to/eyes_cascade
Run Code Online (Sandbox Code Playgroud)

等等。

代码:

#include <opencv2\opencv.hpp>

int main(int argc, char** argv)
{
    cv::String keys =
        "{@image |<none>           | input image path}"         // input image is the first argument (positional)
        "{@face  |/path/to/file.xml| face cascade path}"        // optional, face cascade is the second argument (positional)
        "{eyes   |                 | eye cascade path}"         // optional, default value ""
        "{nose   |      | nose cascade path}"       // optional, default value ""
        "{mouth  |      | mouth cascade path}"      // optional, default value ""
        "{help   |      | show help message}";      // optional, show help optional


    cv::CommandLineParser parser(argc, argv, keys);
    if (parser.has("help")) {
        parser.printMessage();
        return 0;
    }

    cv::String input_image_path = parser.get<cv::String>(0); // read @image (mandatory, error if not present)
    cv::String face_cascade_path = parser.get<cv::String>(1); // read @face (use default value if not in cmd)

    bool hasEyeCascade = parser.has("eyes");
    bool hasNoseCascade = parser.has("nose");
    bool hasMouthCascade = parser.has("mouth");
    cv::String eye_cascade_path = parser.get<cv::String>("eyes"); 
    cv::String nose_cascade_path = parser.get<cv::String>("nose"); 
    cv::String mouth_cascade_path = parser.get<cv::String>("mouth");


    if (!parser.check()) {
        parser.printErrors();
        return -1;
    }

    // No errors!

    // image path always present
    //cv::Mat img = cv::imread(input_image_path);

    // face cascaed path always present (eventually the default value)
    // load face cascade

    // You need to check optional arguments
    if (hasEyeCascade) {
        // load eye cascade
    }
    // You need to check optional arguments
    if (hasNoseCascade) {
        // load nose cascade
    }
    // You need to check optional arguments
    if (hasMouthCascade) {
        // load mouth cascade
    }

    // Do your stuff

    return 0;
}
Run Code Online (Sandbox Code Playgroud)