zoukankan      html  css  js  c++  java
  • OpenCV CommandLineParser 的用法

    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("
    Minimalistic example of foreground-background segmentation in a video sequence using
    "
    13             "OpenCV's BackgroundSubtractor interface; will analyze frames from the default camera
    "
    14             "or from a specified file.
    
    "
    15             "Usage: 
    "
    16             "  ./bgfg_segm [--camera]=<use camera, true/false>, [--file]=<path to file> 
    
    ");
    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.
    ");
    44         else
    45             printf("Could not open video file at '%s'.
    ",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命令行带参运行。最后这个类封装了很多函数,可以直接用,只 不过这个本来就是类结构的优点。

     

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

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

     

     

  • 相关阅读:
    30天自制操作系统之-第四天-
    30天自制操作系统之-第三天-
    30天自制操作系统之-第二天-
    30天自制操作系统之-第一天-
    c语言之连接符
    c语言之函数指针应用
    dpdk之路-环境部署
    linux系统裁剪
    redux和mobx比较(二)
    redux和mobx比较(一)
  • 原文地址:https://www.cnblogs.com/wangxiaocvpr/p/4989612.html
Copyright © 2011-2022 走看看