OpenCV CommandLineParser 的用法

时间:2021-02-12 21:50:29

OpenCV CommandLineParser 的用法

去百度了一下,关键字:OpenCV CommandLineParser  发现,最多的讲解是:opencv源码解析之(5):CommandLineParser类的简单理解  链接:http://www.cnblogs.com/tornadomeet/archive/2012/04/15/2450505.html

 // minimalistic foreground-background segmentation sample, based off OpenCV's bgfg_segm sample

 #include "BackgroundSubtractorSuBSENSE.h"

 #include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h> static void help() {
printf("\nMinimalistic example of foreground-background segmentation in a video sequence using\n"
"OpenCV's BackgroundSubtractor interface; will analyze frames from the default camera\n"
"or from a specified file.\n\n"
"Usage: \n"
" ./bgfg_segm [--camera]=<use camera, true/false>, [--file]=<path to file> \n\n");
} const char* keys = {
"{c |camera |true | use camera or not}"
"{f |file |tree.avi | movie file path }"
}; int main(int argc, const char** argv) {
help();
cv::CommandLineParser parser(argc, argv, keys);
const bool bUseDefaultCamera = parser.get<bool>("camera");
const std::string sVideoFilePath = parser.get<std::string>("file");
cv::VideoCapture oVideoInput;
cv::Mat oCurrInputFrame, oCurrSegmMask, oCurrReconstrBGImg;
if(bUseDefaultCamera) {
oVideoInput.open();
oVideoInput >> oCurrInputFrame;
}
else {
oVideoInput.open(sVideoFilePath);
oVideoInput >> oCurrInputFrame;
oVideoInput.set(CV_CAP_PROP_POS_FRAMES,);
}
parser.printParams();
if(!oVideoInput.isOpened() || oCurrInputFrame.empty()) {
if(bUseDefaultCamera)
printf("Could not open default camera.\n");
else
printf("Could not open video file at '%s'.\n",sVideoFilePath.c_str());
return -;
}
oCurrSegmMask.create(oCurrInputFrame.size(),CV_8UC1);
oCurrReconstrBGImg.create(oCurrInputFrame.size(),oCurrInputFrame.type());
cv::Mat oSequenceROI(oCurrInputFrame.size(),CV_8UC1,cv::Scalar_<uchar>()); // for optimal results, pass a constrained ROI to the algorithm (ex: for CDnet, use ROI.bmp)
cv::namedWindow("input",cv::WINDOW_NORMAL);
cv::namedWindow("segmentation mask",cv::WINDOW_NORMAL);
cv::namedWindow("reconstructed background",cv::WINDOW_NORMAL);
BackgroundSubtractorSuBSENSE oBGSAlg;
oBGSAlg.initialize(oCurrInputFrame,oSequenceROI);
for(int k=;;++k) {
oVideoInput >> oCurrInputFrame;
if(oCurrInputFrame.empty())
break;
oBGSAlg(oCurrInputFrame,oCurrSegmMask,double(k<=)); // lower rate in the early frames helps bootstrap the model when foreground is present
oBGSAlg.getBackgroundImage(oCurrReconstrBGImg);
imshow("input",oCurrInputFrame);
imshow("segmentation mask",oCurrSegmMask);
imshow("reconstructed background",oCurrReconstrBGImg);
if(cv::waitKey()==)
break;
}
return ;
}

以下内容出自:http://www.cnblogs.com/tornadomeet/archive/2012/04/15/2450505.html

第一行就是这个类的构造函数,前2个参数是命令行传过来的,第3个就是刚刚定义的keys了,keys的结构有一定规 律,比如说"{c |camera   |false    | use camera or not}" 都是用大括号和双引号引起来,然后中间的内容分成4断,用”|”分隔开,分别表示简称,文件来源,文件值和帮助语句。第二行和第三行表示打开摄像头和打开 文件,文件的文件名等都在keys指针中了。

  最后一行为打印keys中的参数,如下:

  OpenCV CommandLineParser 的用法

大概可以看出来用这个类的好处就是很方便,因为以前版本没这个类时,如果要运行带参数的.exe,必须在命令行中输入文件路径以及各种参数,并且输入的参 数格式要与代码中的if语句判断内容格式一样,一不小心就输错了,很不方便。另外如果想要更改输入格式的话在主函数文件中要相应更改很多地方。现在有了这 个类,只需要改keys里面的内容就可以了,并且运行时可以直接在vs下用F5,不需要cmd命令行带参运行。最后这个类封装了很多函数,可以直接用,只 不过这个本来就是类结构的优点。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------