背景建模技术(五):视频捕获(VideoCapture)模块

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

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

#include "VideoCapture.h"

namespace bgslibrary
{
  namespace VC_ROI
  {
    IplImage* img_input1 = 0;
    IplImage* img_input2 = 0;
    int roi_x0 = 0;
    int roi_y0 = 0;
    int roi_x1 = 0;
    int roi_y1 = 0;
    int numOfRec = 0;
    int startDraw = 0;
    bool roi_defined = false;
    bool use_roi = true;
    bool disable_event = false;

    void reset(void)
    {
      disable_event = false;
      startDraw = false;
    }

    void VideoCapture_on_mouse(int evt, int x, int y, int flag, void* param)
    {
      if (use_roi == false || disable_event == true)
        return;

      if (evt == CV_EVENT_LBUTTONDOWN)
      {
        if (!startDraw)
        {
          roi_x0 = x;
          roi_y0 = y;
          startDraw = 1;
        }
        else
        {
          roi_x1 = x;
          roi_y1 = y;
          startDraw = 0;
          roi_defined = true;
          disable_event = true;
        }
      }

      if (evt == CV_EVENT_MOUSEMOVE && startDraw)
      {
        //redraw ROI selection
        img_input2 = cvCloneImage(img_input1);
        cvRectangle(img_input2, cvPoint(roi_x0, roi_y0), cvPoint(x, y), CV_RGB(255, 0, 0), 1);
        cvShowImage("Input", img_input2);
        cvReleaseImage(&img_input2);
        //startDraw = false;
        //disable_event = true;
      }
    }
  }

  VideoCapture::VideoCapture() : key(0), start_time(0), delta_time(0), freq(0), fps(0), frameNumber(0), stopAt(0),
    useCamera(false), useVideo(false), input_resize_percent(100), showOutput(true), enableFlip(false)
  {
    std::cout << "VideoCapture()" << std::endl;
  }

  VideoCapture::~VideoCapture()
  {
    std::cout << "~VideoCapture()" << std::endl;
  }

  void VideoCapture::setFrameProcessor(IFrameProcessor* frameProcessorPtr)
  {
    frameProcessor = frameProcessorPtr;
  }

  void VideoCapture::setCamera(int index)
  {
    useCamera = true;
    cameraIndex = index;

    useVideo = false;
  }

  void VideoCapture::setUpCamera()
  {
    std::cout << "Camera index:" << cameraIndex << std::endl;
    capture = cvCaptureFromCAM(cameraIndex);

    if (!capture)
      std::cerr << "Cannot open initialize webcam!\n" << std::endl;
  }

  void VideoCapture::setVideo(std::string filename)
  {
    useVideo = true;
    videoFileName = filename;

    useCamera = false;
  }

  void VideoCapture::setUpVideo()
  {
    capture = cvCaptureFromFile(videoFileName.c_str());

    if (!capture)
      std::cerr << "Cannot open video file " << videoFileName << std::endl;
  }

	void VideoCapture::start()
	{
		///////////////loadConfig
		loadConfig();

		///////////////setUpCamera
		if (useCamera) setUpCamera();
		///////////////setUpVideo
		if (useVideo)  setUpVideo();

    if (!capture)  std::cerr << "Capture error..." << std::endl;

    int input_fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    std::cout << "input->fps:" << input_fps << std::endl;

    IplImage* frame1 = cvQueryFrame(capture);
    frame = cvCreateImage(cvSize((int)((frame1->width*input_resize_percent) / 100), (int)((frame1->height*input_resize_percent) / 100)), frame1->depth, frame1->nChannels);
    //cvCreateImage(cvSize(frame1->width/input_resize_factor, frame1->height/input_resize_factor), frame1->depth, frame1->nChannels);
    std::cout << "input->resize_percent:" << input_resize_percent << std::endl;
    std::cout << "input->width:" << frame->width << std::endl;
    std::cout << "input->height:" << frame->height << std::endl;

    double loopDelay = 33.333;
    if (input_fps > 0)
      loopDelay = (1. / input_fps)*1000.;
    std::cout << "loopDelay:" << loopDelay << std::endl;

    std::cout << "Press 'ESC' to stop..." << std::endl;
    bool firstTime = true;
    do
    {
      frameNumber++;

      frame1 = cvQueryFrame(capture);
      if (!frame1) break;

      cvResize(frame1, frame);

      if (enableFlip)
        cvFlip(frame, frame, 0);

      if (VC_ROI::use_roi == true && VC_ROI::roi_defined == false && firstTime == true)
      {
        VC_ROI::reset();

        do
        {
          cv::Mat img_input(frame);

          if (showOutput)
          {
            cv::imshow("Input", img_input);

            std::cout << "Set ROI (press ESC to skip)" << std::endl;
            VC_ROI::img_input1 = new IplImage(img_input);
            cvSetMouseCallback("Input", VC_ROI::VideoCapture_on_mouse, NULL);
            key = cvWaitKey(0);
            delete VC_ROI::img_input1;
          }
          else
            key = KEY_ESC;

          if (key == KEY_ESC)
          {
            std::cout << "ROI disabled" << std::endl;
            VC_ROI::reset();
            VC_ROI::use_roi = false;
            break;
          }

          if (VC_ROI::roi_defined)
          {
            std::cout << "ROI defined (" << VC_ROI::roi_x0 << "," << VC_ROI::roi_y0 << "," << VC_ROI::roi_x1 << "," << VC_ROI::roi_y1 << ")" << std::endl;
            break;
          }
          else
            std::cout << "ROI undefined" << std::endl;

        } while (1);
      }

      if (VC_ROI::use_roi == true && VC_ROI::roi_defined == true)
      {
        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);
        cvSetImageROI(frame, rect);
      }

      cv::Mat img_input(frame);

      if (showOutput)
        cv::imshow("Input", img_input);

		///////////////saveConfig
		if (firstTime)
			saveConfig();

      start_time = cv::getTickCount();
		///////////////frameProcessor,start "Background Modeling"
		frameProcessor->process(img_input);
      int64 delta_time = cv::getTickCount() - start_time;
      freq = cv::getTickFrequency();
      fps = freq / delta_time;
      //std::cout << "FPS: " << fps << std::endl;

      cvResetImageROI(frame);

      key = cvWaitKey(loopDelay);
      //std::cout << "key: " << key << std::endl;

      if (key == KEY_SPACE)
        key = cvWaitKey(0);

      if (key == KEY_ESC)
        break;

      if (stopAt > 0 && stopAt == frameNumber)
        key = cvWaitKey(0);

      firstTime = false;
    } while (1);

    cvReleaseCapture(&capture);
  }

  void VideoCapture::saveConfig()
  {
    CvFileStorage* fs = cvOpenFileStorage("./config/VideoCapture.xml", 0, CV_STORAGE_WRITE);

    cvWriteInt(fs, "stopAt", stopAt);
    cvWriteInt(fs, "input_resize_percent", input_resize_percent);
    cvWriteInt(fs, "enableFlip", enableFlip);
    cvWriteInt(fs, "use_roi", VC_ROI::use_roi);
    cvWriteInt(fs, "roi_defined", VC_ROI::roi_defined);
    cvWriteInt(fs, "roi_x0", VC_ROI::roi_x0);
    cvWriteInt(fs, "roi_y0", VC_ROI::roi_y0);
    cvWriteInt(fs, "roi_x1", VC_ROI::roi_x1);
    cvWriteInt(fs, "roi_y1", VC_ROI::roi_y1);
    cvWriteInt(fs, "showOutput", showOutput);

    cvReleaseFileStorage(&fs);
  }

  void VideoCapture::loadConfig()
  {
    CvFileStorage* fs = cvOpenFileStorage("./config/VideoCapture.xml", 0, CV_STORAGE_READ);

    stopAt = cvReadIntByName(fs, 0, "stopAt", 0);
    input_resize_percent = cvReadIntByName(fs, 0, "input_resize_percent", 100);
    enableFlip = cvReadIntByName(fs, 0, "enableFlip", false);
    VC_ROI::use_roi = cvReadIntByName(fs, 0, "use_roi", true);
    VC_ROI::roi_defined = cvReadIntByName(fs, 0, "roi_defined", false);
    VC_ROI::roi_x0 = cvReadIntByName(fs, 0, "roi_x0", 0);
    VC_ROI::roi_y0 = cvReadIntByName(fs, 0, "roi_y0", 0);
    VC_ROI::roi_x1 = cvReadIntByName(fs, 0, "roi_x1", 0);
    VC_ROI::roi_y1 = cvReadIntByName(fs, 0, "roi_y1", 0);
    showOutput = cvReadIntByName(fs, 0, "showOutput", true);

    cvReleaseFileStorage(&fs);
  }
}

对应的流程框架如下图:

时间: 2024-08-24 17:34:10

背景建模技术(五):视频捕获(VideoCapture)模块的相关文章

背景建模技术(一):介绍、资源下载、“背景建模库”平台搭建

背景建模技术(一):介绍.资源下载."背景建模库"平台搭建 1.介绍 视频分析与理解是一个非常活跃的研究领域,在这个研究领域(如视频监控.多媒体应用等)中,第一步要做的就是检测场景中运动的目标.而背景建模技术是检测前景最常用的技术之一,具有举足轻重的作用和研究意义. 2.资源下载 BgsLibrary的下载:BgsLibrary库 OpenCV的下载:http://opencv.org/  (推荐下载版本2.4.10) 注:PC默认已经安装VS2010. 3."背景建模库&q

背景建模技术(二):BgsLibrary的框架、背景建模的37种算法性能分析、背景建模技术的挑战

背景建模技术(二):BgsLibrary的框架.背景建模的37种算法性能分析.背景建模技术的挑战 1.基于MFC的BgsLibrary软件下载 下载地址:http://download.csdn.net/detail/frd2009041510/8691475 该软件平台中包含了37种背景建模算法,可以显示输入视频/图像.基于背景建模得到的前景和背景建模得到的背景图像,还可以显示出每种算法的计算复杂度等等.并且,测试的可以是视频.图片序列以及摄像头输入视频.其界面如下图所示: 2.BgsLibr

背景建模技术(三):背景减法库(BGS Library)的基本框架与入口函数main()的功能

背景减法库(BGS Library = background subtraction library)包含了37种背景建模算法,也是目前国际上关于背景建模技术研究最全也最权威的资料.本文将更加详细的介绍背景减法库(BGS Library)的基本框架与入口函数main()的功能. BGS库的整体框架在背景建模技术(二)中已经全部给出,此处从函数的角度再次给出BGS库的基本框架,有利于代码的修改与维护. 如下图所示是基于C++的BGS库的函数流程图: 接下来将会对每个函数进行更加详细的分析. 首先,

背景建模技术(四):视频分析(VideoAnalysis)模块

视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数:第二个函数是VideoAnalysis::start(),其主要功能初始化视频处理.设置视频获取方式以及开始视频捕获功能等. 1.VideoAnalysis::setup(....) 该函数的代码如下: [cpp] view plaincopyprint? bool VideoAnalysis::setup(int argc, cons

背景建模技术(六):帧处理(FrameProcessor)模块

前面几篇文章简单介绍了BgsLibrary的入口函数.视频分析和视频捕获模块,本文将简单介绍帧处理模块,即对每一帧进行处理的函数,也就是真正调用背景建模算法的接口处. 下面贴出源码供大家分析: #include "FrameProcessor.h" #include <iomanip> namespace bgslibrary { FrameProcessor::FrameProcessor() : firstTime(true), frameNumber(0), dura

背景建模技术(七):预处理(PreProcessor)模块

预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的"预处理"过程,其主要功能包括'去模糊'.'获得灰度图'.'应用Canny算子'等可选模块. 下面给出源码: #include "PreProcessor.h" namespace bgslibrary { PreProcessor::PreProcessor() : firstTime(true), equalizeHist(false), gaussianBlu

使用视频作为网页背景的技术探讨

http://www.chinaz.com/web/2014/0530/353774.shtml 怎么开淘宝店 网站优化方法 创业如何获得投资 小米note顶配版评测 最新LOL活动 使用视频作为网页背景是件很酷的事情,但也是件困难的事情.CSS里的background-image属性只能使用图片.SVG.颜色或渐变色.但从技术讲,我们是可以伪造出一种效果,让视频以背景的角色出现在其它HTML元素后面.这其中的难点是视频要填充整个浏览器页面,而且要响应浏览器窗口大小的变化. 观看演示1 视频作为

视频捕获

目 录 一. 视频捕获快速入门 2 二.基本的捕获设置 3 1.设置捕获速度: 3 2.设置终止捕获 4 3.捕获的时间限制 4 三.关于捕获窗口 4 1.创建一个AVICAP捕获窗口 5 2.将一个捕获窗口连接至捕获设备 5 3. 父窗口与子窗口的交互 5 4.捕获窗口的状态 6 四.视频捕获驱动和音频驱动 6 1.视频捕获驱动的性能: 6 2.视频对话框: 6 3.PREVIEW 和 OVERLAY模式: 7 4.视频格式 7 5.视频捕获设置 7 6.声频格式 8 五.使用视频捕获 8 1

运动目标检测_混合高斯背景建模

1.混合高斯背景建模理论 混合高斯背景建模是基于像素样本统计信息的背景表示方法,利用像素在较长时间内大量样本值的概率密度等统计信息(如模式数量.每个模式的均值和标准差)表示背景,然后使用统计差分(如3σ原则)进行目标像素判断,可以对复杂动态背景进行建模,计算量较大. 在混合高斯背景模型中,认为像素之间的颜色信息互不相关,对各像素点的处理都是相互独立的.对于视频图像中的每一个像素点,其值在序列图像中的变化可看作是不断产生像素值的随机过程,即用高斯分布来描述每个像素点的颜色呈现规律{单模态(单峰),