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

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

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

对应的流程框架如下图:

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

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

    背景建模技术(二):BgsLibrary的框架.背景建模的37种算法性能分析.背景建模技术的挑战 1.基于MFC的BgsLibrary软件下载 下载地址:http://download.csdn.ne ...

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

    背景减法库(BGS Library = background subtraction library)包含了37种背景建模算法,也是目前国际上关于背景建模技术研究最全也最权威的资料.本文将更加详细的介 ...

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

    视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数:第二个函数是VideoAnalysis ...

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

    前面几篇文章简单介绍了BgsLibrary的入口函数.视频分析和视频捕获模块,本文将简单介绍帧处理模块,即对每一帧进行处理的函数,也就是真正调用背景建模算法的接口处. 下面贴出源码供大家分析: #in ...

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

    预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的“预处理”过程,其主要功能包括‘去模糊’.‘获得灰度图’.'应用Canny算子‘等可选模块. 下面 ...

  6. 浅析软件工程中的UML建模技术

    一.基本信息 标题:浅析软件工程中的UML建模技术 时间:2018 出版源:电子世界 领域分类:软件工程:UML建模技术:需求分析 二.研究背景 问题定义:软件工程中UML建模技术的研究 难点:明确软 ...

  7. iOS仿写有妖气漫画、视频捕获框架、启动页广告页demo、多种动画效果等源码

    iOS精选源码 以tableview的section为整体添加阴影效果/ta'b'le'vi'e'w顶部悬浮.... 一个可以轻松应用自定义过滤器的视频捕获框架. 基于UITableView的组件,旨 ...

  8. [MOC062066]背景建模资料收集整理

    一.相关博客 背景建模相关资料收集,各个链接都已给出. 资料,不可能非常完整,以后不定期更新. -----------------切割线----------------- 这个哥们总结的非常好啊,看完 ...

  9. OpenCV ——背景建模之CodeBook(1)

    1,CodeBook算法流程介绍 CodeBook算法的基本思想是得到每个像素的时间序列模型.这种模型能很好地处理时间起伏,缺点是需要消耗大量的内存.CodeBook算法为当前图像的每一个像素建立一个 ...

随机推荐

  1. mysql新手入门随笔4

    40.子查询:出现在其他SQL语句里的SELECT语句 例如:SELECT sname,mark FROM student WHERE mark = (SELECT max(mark) FROM st ...

  2. ObjectMapper的使用

    Jackson ObjectMapper类 ObjectMapper类是Jackson库的主要类它提供一些功能将Java对象转换成JSON结构,反之亦然它使用JsonParser和JsonGenera ...

  3. 【转】MMO即时战斗:技能实现

    转自 http://blog.csdn.net/cyblueboy83/article/details/41628743 一.前言 基本所有MMO游戏无论是回合制.策略类.即时战斗等等类型都需要有相应 ...

  4. CSP201409-2:画图

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  5. 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

    Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...

  6. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  7. HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)

    Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...

  8. 使用cout进行格式化

    以下内容摘自木缥缈的博客 使用cout进行格式化 ostream插入运算符将值转换为文本格式.在默认情况下,格式化值的方式如下. * 对于char值,如果它代表的是可打印字符,则将被作为一个字符显示在 ...

  9. ARKit----学习一

    一.ARKit的简介 开始进入正题吧 ARKit在iOS 11上推出的一个AR移动平台,支持A9以上的处理器,不支持模拟器.ARKit使用相机捕捉现实世界,使用SceneKit,SpriteKit或者 ...

  10. 按Backspace键删除时,会出现^H

    按Backspace键删除时,会出现^H 2014-08-12 19:38 1180人阅读 评论(0) 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 在linux/unix 平台的经常使 ...