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
保護

此類的文件是從以下檔案生成的