OpenCV CommandLineParser 的用法
去百度了一下,關鍵字:OpenCV CommandLineParser 發現,最多的講解是:opencv源碼解析之(5):CommandLineParser類的簡單理解 鏈接:http://www.cnblogs.com/tornadomeet/archive/2012/04/15/2450505.html
1 // minimalistic foreground-background segmentation sample, based off OpenCV's bgfg_segm sample 2 3 #include "BackgroundSubtractorSuBSENSE.h" 4 5 #include <opencv2/core/core.hpp> 6 #include <opencv2/imgproc/imgproc.hpp> 7 #include <opencv2/video/background_segm.hpp> 8 #include <opencv2/highgui/highgui.hpp> 9 #include <stdio.h> 10 11 static void help() { 12 printf("\nMinimalistic example of foreground-background segmentation in a video sequence using\n" 13 "OpenCV's BackgroundSubtractor interface; will analyze frames from the default camera\n" 14 "or from a specified file.\n\n" 15 "Usage: \n" 16 " ./bgfg_segm [--camera]=<use camera, true/false>, [--file]=<path to file> \n\n"); 17 } 18 19 const char* keys = { 20 "{c |camera |true | use camera or not}" 21 "{f |file |tree.avi | movie file path }" 22 }; 23 24 int main(int argc, const char** argv) { 25 help(); 26 cv::CommandLineParser parser(argc, argv, keys); 27 const bool bUseDefaultCamera = parser.get<bool>("camera"); 28 const std::string sVideoFilePath = parser.get<std::string>("file"); 29 cv::VideoCapture oVideoInput; 30 cv::Mat oCurrInputFrame, oCurrSegmMask, oCurrReconstrBGImg; 31 if(bUseDefaultCamera) { 32 oVideoInput.open(0); 33 oVideoInput >> oCurrInputFrame; 34 } 35 else { 36 oVideoInput.open(sVideoFilePath); 37 oVideoInput >> oCurrInputFrame; 38 oVideoInput.set(CV_CAP_PROP_POS_FRAMES,0); 39 } 40 parser.printParams(); 41 if(!oVideoInput.isOpened() || oCurrInputFrame.empty()) { 42 if(bUseDefaultCamera) 43 printf("Could not open default camera.\n"); 44 else 45 printf("Could not open video file at '%s'.\n",sVideoFilePath.c_str()); 46 return -1; 47 } 48 oCurrSegmMask.create(oCurrInputFrame.size(),CV_8UC1); 49 oCurrReconstrBGImg.create(oCurrInputFrame.size(),oCurrInputFrame.type()); 50 cv::Mat oSequenceROI(oCurrInputFrame.size(),CV_8UC1,cv::Scalar_<uchar>(255)); // for optimal results, pass a constrained ROI to the algorithm (ex: for CDnet, use ROI.bmp) 51 cv::namedWindow("input",cv::WINDOW_NORMAL); 52 cv::namedWindow("segmentation mask",cv::WINDOW_NORMAL); 53 cv::namedWindow("reconstructed background",cv::WINDOW_NORMAL); 54 BackgroundSubtractorSuBSENSE oBGSAlg; 55 oBGSAlg.initialize(oCurrInputFrame,oSequenceROI); 56 for(int k=0;;++k) { 57 oVideoInput >> oCurrInputFrame; 58 if(oCurrInputFrame.empty()) 59 break; 60 oBGSAlg(oCurrInputFrame,oCurrSegmMask,double(k<=100)); // lower rate in the early frames helps bootstrap the model when foreground is present 61 oBGSAlg.getBackgroundImage(oCurrReconstrBGImg); 62 imshow("input",oCurrInputFrame); 63 imshow("segmentation mask",oCurrSegmMask); 64 imshow("reconstructed background",oCurrReconstrBGImg); 65 if(cv::waitKey(1)==27) 66 break; 67 } 68 return 0; 69 }
以下內容出自: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中的參數,如下:
大概可以看出來用這個類的好處就是很方便,因為以前版本沒這個類時,如果要運行帶參數的.exe,必須在命令行中輸入文件路徑以及各種參數,並且輸入的參 數格式要與代碼中的if語句判斷內容格式一樣,一不小心就輸錯了,很不方便。另外如果想要更改輸入格式的話在主函數文件中要相應更改很多地方。現在有了這 個類,只需要改keys里面的內容就可以了,並且運行時可以直接在vs下用F5,不需要cmd命令行帶參運行。最后這個類封裝了很多函數,可以直接用,只 不過這個本來就是類結構的優點。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------