[advanced c++] Professional coding 03 - 命令行参数

OpenCV 命令行参数


OpenCV的优秀示范

参考:OpenCV提供的方式

cv::CommandLineParser parser

 

参数模板

字符串是按照顺序获取,其他参数则是根据“标识符”去定位,然后获取等号后面的“值”。

./count-areas ./hahaah.png --shape=1,2

  

模板如下:

复制代码
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help()
{
    cout << "\nThis program demonstrated the floodFill() function\n"
            "Call:\n"
            "./ffilldemo [image_name -- Default: ../data/fruits.jpg]\n" << endl;

    cout << "Hot keys: \n"
            "\tESC - quit the program\n"
            "\tc - switch color/grayscale mode\n"
            "\tm - switch mask mode\n"
            "\tr - restore the original image\n"
            "\ts - use null-range floodfill\n"
            "\tf - use gradient floodfill with fixed(absolute) range\n"
            "\tg - use gradient floodfill with floating(relative) range\n"
            "\t4 - use 4-connectivity mode\n"
            "\t8 - use 8-connectivity mode\n" << endl;
}


int main( int argc, char** argv )
{
    cv::CommandLineParser parser (argc, argv,
        "{help h | | show help message}"
        "{version v | | v0.1}"
        "{shape s | 10, 10 | height, width}"

        "{@image|../data/fruits.jpg| input image}"
        "{@image1        |      | image1 for compare   }"
        "{@image2        |<none>| image2 for compare   }"

        "{path           |.     | path to file         }"
        "{fps            | -1.0 | fps for output video }"
        "{N count        |100   | count of objects     }"
        "{ts timestamp   |      | use time stamp       }"
 
    );

    // only if "has"
    if (parser.has("help") || parser.has("version"))
    {
        parser.printMessage();
        return 0;
    }

    int N = parser.get<int>("N");
    cout << "Debug --> N = " << N << endl << endl;
    double fps = parser.get<double>("fps");
    cout << "Debug --> fps = " << fps << endl << endl;

    string filename = parser.get<string>("@image");
    cout << "Debug --> filename = " << filename << endl << endl;
    string filename1 = parser.get<string>("@image1");
    cout << "Debug --> filename1 = " << filename1 << endl << endl;
    string filename2 = parser.get<string>("@image2");
    cout << "Debug --> filename2 = " << filename2 << endl << endl;

    if (parser.has("shape"))
    {
        string shape = parser.get<string>("shape");
        cout << "Debug --> shape = " << shape << endl << endl;
    }


    // help();

    return 0;
}
复制代码

 

 

 

  

C++方案


参数 option结构体

typedef struct {
bool print_version_flag = false; bool print_help_flag = false;
bool json_output_flag = false;
const char* slice_subdivisions_directory = nullptr;
// ...other elems map
<string, const char*> detector_options; } DetectorProgramOptions;

 

参数 option函数

复制代码
// parse options from argv
DetectorProgramOptions parse_detector_program_options(int argc, const char** argv)
{
    DetectorProgramOptions results;

    for (int i = 0; i < argc; i++)
    {
//---------------------------------------------------------------------- 参数类型一、需直接返回的参数
if (strcmp(argv[i], "--help") == 0) { results.print_help_flag = true; return results; } else if (strcmp(argv[i], "--version") == 0) { results.print_version_flag = true; return results; } //---------------------------------------------------------------------- 参数类型二、不需要直接返回的参数 else if (strcmp(argv[i], "--json") == 0) results.json_output_flag = true; //---------------------------------------------------------------------- 参数类型三、不需要直接返回,且带有值的参数 else if (strcmp(argv[i], "--output-size") == 0) { if (i + 1 >= argc) { results.error_parsing_flag = true; cerr << "Error: --output-size requires a pair of int arguments after it" << endl; return results; } results.detector_options["output_size"] = argv[++i];  // 将下一个字符串保存 } else if (strcmp(argv[i], "--slice-subdivisions") == 0) { if (i + 1 >= argc) { results.error_parsing_flag = true; cerr << "Error: --slice-subdivisions requires a pair of int arguments after it" << endl; return results; } results.detector_options["slice_subdivisions"] = argv[++i];  // 将下一个字符串保存 }
else { results.error_parsing_flag = true; cerr << "Error: too many arguments" << endl; cerr << "Usage: ./output <input file> <output file>" << endl; return results; } } // verify that the state is valid and we have everything we need if (results.batch_output == false && results.input_file == NULL) { results.error_parsing_flag = true; cerr << "Usage: ./output <input file> <output file>" << endl; } if (results.batch_output && results.input_file_list.size() <= 0) { results.error_parsing_flag = true; cerr << "Usage: ./output --batch-output <output file directory> <input file #1> [input file #2]" << endl; } return results; }
复制代码

 

 

End.

posted @   郝壹贰叁  阅读(166)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示