OpenCV 4.12.0
开源计算机视觉
加载中...
搜索中...
无匹配项
cv::CommandLineParser 类参考

专为命令行解析设计。 更多...

#include <opencv2/core/utility.hpp>

cv::CommandLineParser 的协作图

公共成员函数

 CommandLineParser (const CommandLineParser &parser)
 复制构造函数。
 
 CommandLineParser (int argc, const char *const argv[], const String &keys)
 构造函数。
 
 ~CommandLineParser ()
 析构函数。
 
void about (const String &message)
 设置 about 消息。
 
bool check () const
 检查解析错误。
 
template<typename T >
get (const String &name, bool space_delete=true) const
 按名称访问参数。
 
template<typename T >
get (int index, bool space_delete=true) const
 通过索引访问位置参数。
 
String getPathToApplication () const
 返回应用程序路径。
 
bool has (const String &name) const
 检查命令行中是否提供了字段。
 
CommandLineParseroperator= (const CommandLineParser &parser)
 赋值运算符。
 
void printErrors () const
 打印发生的错误列表。
 
void printMessage () const
 打印帮助信息。
 

保护成员函数

void getByIndex (int index, bool space_delete, Param type, void *dst) const
 
void getByName (const String &name, bool space_delete, Param type, void *dst) const
 

保护属性

Impl * impl
 

详细描述

如果数组没有元素,则返回 true。

下面的示例演示了如何使用 CommandLineParser

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");
double fps = parser.get<double>("fps");
String path = parser.get<String>("path");
use_time_stamp = parser.has("timestamp");
String img1 = parser.get<String>(0);
String img2 = parser.get<String>(1);
int repeat = parser.get<int>(2);
if (!parser.check())
{
parser.printErrors();
return 0;
}
如果数组没有元素,则返回 true。
int64_t int64
void repeat(InputArray src, int ny, int nx, OutputArray dst)
用输入数组的重复副本填充输出数组。
std::string String
定义 cvstd.hpp:151

键语法

keys 参数是一个字符串,包含多个块,每个块用大括号括起来,描述一个参数。每个参数包含由 | 符号分隔的三个部分

  1. 参数名是一系列用标准空格字符 ' ' 分隔的选项同义词(要将参数标记为位置参数,请在其前加上 @ 符号)
  2. 如果未提供参数,将使用默认值(可以为空)
  3. 帮助消息(可以为空)

例如

const String keys =
"{help h usage ? | | 打印此消息 }"
"{@image1 | | 用于比较的 image1 }"
"{@image2 |<none>| 用于比较的 image2 }"
"{@repeat |1 | 数字 }"
"{path |. | 文件路径 }"
"{fps | -1.0 | 输出视频的 fps }"
"{N count |100 | 对象数量 }"
"{ts timestamp | | 使用时间戳 }"
;
}

请注意,helptimestamp 没有默认值,因此我们可以使用 has() 方法检查它们的存在。具有默认值的参数被认为始终存在。在这些情况下,请使用 get() 方法检查它们的实际值。请注意,除标准空格外的空白字符被视为字符串的一部分。此外,帮助消息前后的标准空格将被忽略。

get<String>("@image1") 这样的字符串键默认返回空字符串 ""——即使默认值为空。使用特殊的 <none> 默认值强制返回的字符串不能为空。(例如 get<String>("@image2")

用法

对于所描述的键

# 正确调用(3个位置参数:image1、image2 和 repeat;N 为 200,ts 为 true)
$ ./app -N=200 1.png 2.jpg 19 -ts
# 错误调用
$ ./app -fps=aaa
错误
参数 'fps': 无法将: [aaa] 转换为 [double]
示例
fld_lines.cpp, modules/shape/samples/shape_example.cpp, samples/cpp/camshiftdemo.cpp, samples/cpp/connected_components.cpp, samples/cpp/contours2.cpp, samples/cpp/convexhull.cpp, samples/cpp/cout_mat.cpp, samples/cpp/create_mask.cpp, samples/cpp/demhist.cpp, samples/cpp/distrans.cpp, samples/cpp/edge.cpp, samples/cpp/facedetect.cpp, samples/cpp/ffilldemo.cpp, samples/cpp/fitellipse.cpp, samples/cpp/grabcut.cpp, samples/cpp/image_alignment.cpp, samples/cpp/laplace.cpp, samples/cpp/lkdemo.cpp, samples/cpp/lsd_lines.cpp, samples/cpp/pca.cpp, samples/cpp/peopledetect.cpp, samples/cpp/polar_transforms.cpp, samples/cpp/segment_objects.cpp, samples/cpp/train_HOG.cpp, samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp, samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp, samples/cpp/tutorial_code/ImgProc/Morphology_2.cpp, samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp, samples/cpp/tutorial_code/features2D/Homography/decompose_homography.cpp, samples/cpp/tutorial_code/features2D/Homography/homography_from_camera_displacement.cpp, samples/cpp/tutorial_code/features2D/Homography/pose_from_homography.cpp, samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp, samples/cpp/tutorial_code/photo/non_photorealistic_rendering/npr_demo.cpp, samples/cpp/warpPerspective_demo.cpp, samples/cpp/watershed.cpp, samples/dnn/classification.cpp, samples/dnn/colorization.cpp, samples/dnn/object_detection.cpp, samples/dnn/openpose.cpp, samples/dnn/segmentation.cpp, samples/dnn/text_detection.cpp, 和 samples/tapi/hog.cpp

构造函数 & 析构函数文档

◆ CommandLineParser() [1/2]

cv::CommandLineParser::CommandLineParser ( int argc,
const char *const argv[],
const String & keys )

构造函数。

初始化命令行解析器对象

参数
argc命令行参数数量(来自 main()
argv命令行参数数组(来自 main()
描述可接受命令行参数的字符串(参见类描述了解语法)

◆ CommandLineParser() [2/2]

cv::CommandLineParser::CommandLineParser ( const CommandLineParser & 解析器)

复制构造函数。

◆ ~CommandLineParser()

cv::CommandLineParser::~CommandLineParser ( )

析构函数。

成员函数文档

◆ about()

void cv::CommandLineParser::about ( const String & 消息)

设置 about 消息。

当调用 printMessage 时,将在参数表之前显示“关于”消息。

示例
samples/cpp/polar_transforms.cpp, 和 samples/dnn/classification.cpp

◆ check()

bool cv::CommandLineParser::check ( ) const

检查解析错误。

如果在访问参数时发生错误(例如,类型转换错误、缺少参数等),则返回 false。调用 printErrors 打印错误消息列表。

示例
samples/dnn/classification.cpp.

◆ get() [1/2]

template<typename T >
T cv::CommandLineParser::get ( const String & name,
bool space_delete = true ) const
inline

按名称访问参数。

返回转换为所选类型的参数。如果参数未知或无法转换为所选类型,则设置错误标志(可用 check 检查)。

例如,定义

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

调用

$ ./my-app -N=20
# 或
$ ./my-app --count=20

Access

int N = parser.get<int>("N");
参数
name参数名称
space_delete删除字符串左右两边的空格
模板参数
T如果可能,参数将被转换为此类型
注意
您可以通过其 @ 前缀名称访问位置参数
parser.get<String>("@image");
示例
fld_lines.cpp, samples/cpp/lsd_lines.cpp, samples/cpp/polar_transforms.cpp, samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp, samples/cpp/tutorial_code/photo/non_photorealistic_rendering/npr_demo.cpp, samples/dnn/classification.cpp, 和 samples/tapi/hog.cpp

◆ get() [2/2]

template<typename T >
T cv::CommandLineParser::get ( int index,
bool space_delete = true ) const
inline

通过索引访问位置参数。

返回转换为所选类型的参数。索引从零开始计数。

例如,定义

String keys = "{@arg1||}{@arg2||}"

调用

./my-app abc qwe

访问参数

String val_1 = parser.get<String>(0); // 返回 "abc", arg1
String val_2 = parser.get<String>(1); // 返回 "qwe", arg2
参数
index参数索引
space_delete删除字符串左右两边的空格
模板参数
T如果可能,参数将被转换为此类型

◆ getByIndex()

void cv::CommandLineParser::getByIndex ( int index,
bool space_delete,
Param type,
void * dst ) const
保护

◆ getByName()

void cv::CommandLineParser::getByName ( const String & name,
bool space_delete,
Param type,
void * dst ) const
保护

◆ getPathToApplication()

String cv::CommandLineParser::getPathToApplication ( ) const

返回应用程序路径。

此方法返回命令行中可执行文件的路径(argv[0])。

例如,如果应用程序以以下命令启动

$ ./bin/my-executable

此方法将返回 ./bin

◆ has()

bool cv::CommandLineParser::has ( const String & name) const

检查命令行中是否提供了字段。

参数
name要检查的参数名称
示例
fld_lines.cpp, samples/dnn/classification.cpp, 和 samples/tapi/hog.cpp

◆ operator=()

CommandLineParser & cv::CommandLineParser::operator= ( const CommandLineParser & 解析器)

赋值运算符。

◆ printErrors()

void cv::CommandLineParser::printErrors ( ) const

打印发生的错误列表。

另请参见
check
示例
samples/dnn/classification.cpp.

◆ printMessage()

void cv::CommandLineParser::printMessage ( ) const

打印帮助信息。

此方法将打印包含“关于”消息和参数描述的标准帮助消息。

另请参见
关于
示例
fld_lines.cpp, samples/cpp/lsd_lines.cpp, samples/cpp/polar_transforms.cpp, samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp, 和 samples/dnn/classification.cpp

成员数据文档

◆ impl

Impl* cv::CommandLineParser::impl
保护

此类的文档是从以下文件生成的