zoukankan      html  css  js  c++  java
  • 背景建模技术(五):视频捕获(VideoCapture)模块

    本次对“视频捕获(VideoCapture)模块”做出分析,给出源代码和对应的程序流程框架。

    视频捕获模块的主要功能是设置视频或相机参数,并读取设置配置参数,最后进入帧处理模块的process进程,该模块的源码如下,请重点关注start()函数:

    1. #include "VideoCapture.h"  
    2.   
    3. namespace bgslibrary  
    4. {  
    5.   namespace VC_ROI  
    6.   {  
    7.     IplImage* img_input1 = 0;  
    8.     IplImage* img_input2 = 0;  
    9.     int roi_x0 = 0;  
    10.     int roi_y0 = 0;  
    11.     int roi_x1 = 0;  
    12.     int roi_y1 = 0;  
    13.     int numOfRec = 0;  
    14.     int startDraw = 0;  
    15.     bool roi_defined = false;  
    16.     bool use_roi = true;  
    17.     bool disable_event = false;  
    18.   
    19.     void reset(void)  
    20.     {  
    21.       disable_event = false;  
    22.       startDraw = false;  
    23.     }  
    24.   
    25.     void VideoCapture_on_mouse(int evt, int x, int y, int flag, void* param)  
    26.     {  
    27.       if (use_roi == false || disable_event == true)  
    28.         return;  
    29.   
    30.       if (evt == CV_EVENT_LBUTTONDOWN)  
    31.       {  
    32.         if (!startDraw)  
    33.         {  
    34.           roi_x0 = x;  
    35.           roi_y0 = y;  
    36.           startDraw = 1;  
    37.         }  
    38.         else  
    39.         {  
    40.           roi_x1 = x;  
    41.           roi_y1 = y;  
    42.           startDraw = 0;  
    43.           roi_defined = true;  
    44.           disable_event = true;  
    45.         }  
    46.       }  
    47.   
    48.       if (evt == CV_EVENT_MOUSEMOVE && startDraw)  
    49.       {  
    50.         //redraw ROI selection  
    51.         img_input2 = cvCloneImage(img_input1);  
    52.         cvRectangle(img_input2, cvPoint(roi_x0, roi_y0), cvPoint(x, y), CV_RGB(255, 0, 0), 1);  
    53.         cvShowImage("Input", img_input2);  
    54.         cvReleaseImage(&img_input2);  
    55.         //startDraw = false;  
    56.         //disable_event = true;  
    57.       }  
    58.     }  
    59.   }  
    60.   
    61.   VideoCapture::VideoCapture() : key(0), start_time(0), delta_time(0), freq(0), fps(0), frameNumber(0), stopAt(0),  
    62.     useCamera(false), useVideo(false), input_resize_percent(100), showOutput(true), enableFlip(false)  
    63.   {  
    64.     std::cout << "VideoCapture()" << std::endl;  
    65.   }  
    66.   
    67.   VideoCapture::~VideoCapture()  
    68.   {  
    69.     std::cout << "~VideoCapture()" << std::endl;  
    70.   }  
    71.   
    72.   void VideoCapture::setFrameProcessor(IFrameProcessor* frameProcessorPtr)  
    73.   {  
    74.     frameProcessor = frameProcessorPtr;  
    75.   }  
    76.   
    77.   void VideoCapture::setCamera(int index)  
    78.   {  
    79.     useCamera = true;  
    80.     cameraIndex = index;  
    81.   
    82.     useVideo = false;  
    83.   }  
    84.   
    85.   void VideoCapture::setUpCamera()  
    86.   {  
    87.     std::cout << "Camera index:" << cameraIndex << std::endl;  
    88.     capture = cvCaptureFromCAM(cameraIndex);  
    89.   
    90.     if (!capture)  
    91.       std::cerr << "Cannot open initialize webcam! " << std::endl;  
    92.   }  
    93.   
    94.   void VideoCapture::setVideo(std::string filename)  
    95.   {  
    96.     useVideo = true;  
    97.     videoFileName = filename;  
    98.   
    99.     useCamera = false;  
    100.   }  
    101.   
    102.   void VideoCapture::setUpVideo()  
    103.   {  
    104.     capture = cvCaptureFromFile(videoFileName.c_str());  
    105.   
    106.     if (!capture)  
    107.       std::cerr << "Cannot open video file " << videoFileName << std::endl;  
    108.   }  
    109.   
    110.     void VideoCapture::start()  
    111.     {  
    112.         ///////////////loadConfig  
    113.         loadConfig();  
    114.   
    115.         ///////////////setUpCamera  
    116.         if (useCamera) setUpCamera();  
    117.         ///////////////setUpVideo  
    118.         if (useVideo)  setUpVideo();  
    119.   
    120.     if (!capture)  std::cerr << "Capture error..." << std::endl;  
    121.   
    122.     int input_fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);  
    123.     std::cout << "input->fps:" << input_fps << std::endl;  
    124.   
    125.     IplImage* frame1 = cvQueryFrame(capture);  
    126.     frame = cvCreateImage(cvSize((int)((frame1->width*input_resize_percent) / 100), (int)((frame1->height*input_resize_percent) / 100)), frame1->depth, frame1->nChannels);  
    127.     //cvCreateImage(cvSize(frame1->width/input_resize_factor, frame1->height/input_resize_factor), frame1->depth, frame1->nChannels);  
    128.     std::cout << "input->resize_percent:" << input_resize_percent << std::endl;  
    129.     std::cout << "input->" << frame->width << std::endl;  
    130.     std::cout << "input->height:" << frame->height << std::endl;  
    131.   
    132.     double loopDelay = 33.333;  
    133.     if (input_fps > 0)  
    134.       loopDelay = (1. / input_fps)*1000.;  
    135.     std::cout << "loopDelay:" << loopDelay << std::endl;  
    136.   
    137.     std::cout << "Press 'ESC' to stop..." << std::endl;  
    138.     bool firstTime = true;  
    139.     do  
    140.     {  
    141.       frameNumber++;  
    142.   
    143.       frame1 = cvQueryFrame(capture);  
    144.       if (!frame1) break;  
    145.   
    146.       cvResize(frame1, frame);  
    147.   
    148.       if (enableFlip)  
    149.         cvFlip(frame, frame, 0);  
    150.   
    151.       if (VC_ROI::use_roi == true && VC_ROI::roi_defined == false && firstTime == true)  
    152.       {  
    153.         VC_ROI::reset();  
    154.   
    155.         do  
    156.         {  
    157.           cv::Mat img_input(frame);  
    158.   
    159.           if (showOutput)  
    160.           {  
    161.             cv::imshow("Input", img_input);  
    162.   
    163.             std::cout << "Set ROI (press ESC to skip)" << std::endl;  
    164.             VC_ROI::img_input1 = new IplImage(img_input);  
    165.             cvSetMouseCallback("Input", VC_ROI::VideoCapture_on_mouse, NULL);  
    166.             key = cvWaitKey(0);  
    167.             delete VC_ROI::img_input1;  
    168.           }  
    169.           else  
    170.             key = KEY_ESC;  
    171.   
    172.           if (key == KEY_ESC)  
    173.           {  
    174.             std::cout << "ROI disabled" << std::endl;  
    175.             VC_ROI::reset();  
    176.             VC_ROI::use_roi = false;  
    177.             break;  
    178.           }  
    179.   
    180.           if (VC_ROI::roi_defined)  
    181.           {  
    182.             std::cout << "ROI defined (" << VC_ROI::roi_x0 << "," << VC_ROI::roi_y0 << "," << VC_ROI::roi_x1 << "," << VC_ROI::roi_y1 << ")" << std::endl;  
    183.             break;  
    184.           }  
    185.           else  
    186.             std::cout << "ROI undefined" << std::endl;  
    187.   
    188.         } while (1);  
    189.       }  
    190.   
    191.       if (VC_ROI::use_roi == true && VC_ROI::roi_defined == true)  
    192.       {  
    193.         CvRect rect = cvRect(VC_ROI::roi_x0, VC_ROI::roi_y0, VC_ROI::roi_x1 - VC_ROI::roi_x0, VC_ROI::roi_y1 - VC_ROI::roi_y0);  
    194.         cvSetImageROI(frame, rect);  
    195.       }  
    196.   
    197.       cv::Mat img_input(frame);  
    198.   
    199.       if (showOutput)  
    200.         cv::imshow("Input", img_input);  
    201.   
    202.         ///////////////saveConfig  
    203.         if (firstTime)  
    204.             saveConfig();  
    205.   
    206.       start_time = cv::getTickCount();  
    207.         ///////////////frameProcessor,start "Background Modeling"  
    208.         frameProcessor->process(img_input);  
    209.       int64 delta_time = cv::getTickCount() - start_time;  
    210.       freq = cv::getTickFrequency();  
    211.       fps = freq / delta_time;  
    212.       //std::cout << "FPS: " << fps << std::endl;  
    213.   
    214.       cvResetImageROI(frame);  
    215.   
    216.       key = cvWaitKey(loopDelay);  
    217.       //std::cout << "key: " << key << std::endl;  
    218.   
    219.       if (key == KEY_SPACE)  
    220.         key = cvWaitKey(0);  
    221.   
    222.       if (key == KEY_ESC)  
    223.         break;  
    224.   
    225.       if (stopAt > 0 && stopAt == frameNumber)  
    226.         key = cvWaitKey(0);  
    227.   
    228.       firstTime = false;  
    229.     } while (1);  
    230.   
    231.     cvReleaseCapture(&capture);  
    232.   }  
    233.   
    234.   void VideoCapture::saveConfig()  
    235.   {  
    236.     CvFileStorage* fs = cvOpenFileStorage("./config/VideoCapture.xml", 0, CV_STORAGE_WRITE);  
    237.   
    238.     cvWriteInt(fs, "stopAt", stopAt);  
    239.     cvWriteInt(fs, "input_resize_percent", input_resize_percent);  
    240.     cvWriteInt(fs, "enableFlip", enableFlip);  
    241.     cvWriteInt(fs, "use_roi", VC_ROI::use_roi);  
    242.     cvWriteInt(fs, "roi_defined", VC_ROI::roi_defined);  
    243.     cvWriteInt(fs, "roi_x0", VC_ROI::roi_x0);  
    244.     cvWriteInt(fs, "roi_y0", VC_ROI::roi_y0);  
    245.     cvWriteInt(fs, "roi_x1", VC_ROI::roi_x1);  
    246.     cvWriteInt(fs, "roi_y1", VC_ROI::roi_y1);  
    247.     cvWriteInt(fs, "showOutput", showOutput);  
    248.   
    249.     cvReleaseFileStorage(&fs);  
    250.   }  
    251.   
    252.   void VideoCapture::loadConfig()  
    253.   {  
    254.     CvFileStorage* fs = cvOpenFileStorage("./config/VideoCapture.xml", 0, CV_STORAGE_READ);  
    255.   
    256.     stopAt = cvReadIntByName(fs, 0, "stopAt", 0);  
    257.     input_resize_percent = cvReadIntByName(fs, 0, "input_resize_percent", 100);  
    258.     enableFlip = cvReadIntByName(fs, 0, "enableFlip", false);  
    259.     VC_ROI::use_roi = cvReadIntByName(fs, 0, "use_roi", true);  
    260.     VC_ROI::roi_defined = cvReadIntByName(fs, 0, "roi_defined", false);  
    261.     VC_ROI::roi_x0 = cvReadIntByName(fs, 0, "roi_x0", 0);  
    262.     VC_ROI::roi_y0 = cvReadIntByName(fs, 0, "roi_y0", 0);  
    263.     VC_ROI::roi_x1 = cvReadIntByName(fs, 0, "roi_x1", 0);  
    264.     VC_ROI::roi_y1 = cvReadIntByName(fs, 0, "roi_y1", 0);  
    265.     showOutput = cvReadIntByName(fs, 0, "showOutput", true);  
    266.   
    267.     cvReleaseFileStorage(&fs);  
    268.   }  
    269. }  


    对应的流程框架如下图:

  • 相关阅读:
    Python安装及编辑器UliPad安装
    安装配置Django开发环境(Eclipse + Pydev)
    VsSDK_sfx.exe 安装出错
    [转]代码分析工具FxCop1.36之一:介绍与使用
    [转]IBM Rational系列产品介绍
    [转]C#控件——DataGridView单元格文本自动换行
    [转]FrameWork4.0的兼容问题 .
    【整理】C# ToString格式字符串整理(Format)(数字、日期和枚举的标准格式设置说明符)(SamWang)
    [转]史上最好的20本敏捷开发书籍
    [转]C#数字千分位问题
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/4511776.html
Copyright © 2011-2022 走看看