背景建模技术(五):视频捕获(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);
- }
- }
对应的流程框架如下图:
背景建模技术(五):视频捕获(VideoCapture)模块的更多相关文章
- 背景建模技术(二):BgsLibrary的框架、背景建模的37种算法性能分析、背景建模技术的挑战
背景建模技术(二):BgsLibrary的框架.背景建模的37种算法性能分析.背景建模技术的挑战 1.基于MFC的BgsLibrary软件下载 下载地址:http://download.csdn.ne ...
- 背景建模技术(三):背景减法库(BGS Library)的基本框架与入口函数main()的功能
背景减法库(BGS Library = background subtraction library)包含了37种背景建模算法,也是目前国际上关于背景建模技术研究最全也最权威的资料.本文将更加详细的介 ...
- 背景建模技术(四):视频分析(VideoAnalysis)模块
视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数:第二个函数是VideoAnalysis ...
- 背景建模技术(六):帧处理(FrameProcessor)模块
前面几篇文章简单介绍了BgsLibrary的入口函数.视频分析和视频捕获模块,本文将简单介绍帧处理模块,即对每一帧进行处理的函数,也就是真正调用背景建模算法的接口处. 下面贴出源码供大家分析: #in ...
- 背景建模技术(七):预处理(PreProcessor)模块
预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的“预处理”过程,其主要功能包括‘去模糊’.‘获得灰度图’.'应用Canny算子‘等可选模块. 下面 ...
- 浅析软件工程中的UML建模技术
一.基本信息 标题:浅析软件工程中的UML建模技术 时间:2018 出版源:电子世界 领域分类:软件工程:UML建模技术:需求分析 二.研究背景 问题定义:软件工程中UML建模技术的研究 难点:明确软 ...
- iOS仿写有妖气漫画、视频捕获框架、启动页广告页demo、多种动画效果等源码
iOS精选源码 以tableview的section为整体添加阴影效果/ta'b'le'vi'e'w顶部悬浮.... 一个可以轻松应用自定义过滤器的视频捕获框架. 基于UITableView的组件,旨 ...
- [MOC062066]背景建模资料收集整理
一.相关博客 背景建模相关资料收集,各个链接都已给出. 资料,不可能非常完整,以后不定期更新. -----------------切割线----------------- 这个哥们总结的非常好啊,看完 ...
- OpenCV ——背景建模之CodeBook(1)
1,CodeBook算法流程介绍 CodeBook算法的基本思想是得到每个像素的时间序列模型.这种模型能很好地处理时间起伏,缺点是需要消耗大量的内存.CodeBook算法为当前图像的每一个像素建立一个 ...
随机推荐
- Objective-C Block数据类型 @protocol关键字
Block数据类型 Block封装了一段代码 可以在任何时候执行 Block可以作为函数参数或者函数的返回值 而其本身又可以带输入参数或返回值 苹果官方建议尽量多用Block 在多线程 异步任务 集合 ...
- Unity制作人物头像小图标和小地图
人物头像的制作: 在场景中添加人物模型和环境模型 设置人物的layer为Player 在主摄像机的基础上,新建一个次摄像机并将摄像机镜头对准人物面部,调整至合适大小. 设置次摄像机 culling m ...
- mybatis interceptor 处理查询参数及查询结果
拦截器:拦截update,query方法,处理查询参数及返回结果. /** * Created by windwant on 2017/1/12. */ @Intercepts({ @Signatur ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- 《Git学习指南》学习笔记(三)
多次提交 提交一般分未两步:add和commit. add将修改存入到索引(index)或叫暂存区(staging area)中. status命令 status命令会出现三种可能的状态: chang ...
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
- 查看python中包的文档
核心命令:python -m pydoc 查询某包:python -m pydoc 包名 示例: C:\Users\xxx>python -m pydoc pydoc - the Python ...
- 五:Edits Viewer离线日志查看器
离线日志查看器可以将二进制日志翻译成可读的文件(如XML),只有当hadoop集群停止时才能使用.输入文件支持的类型:XML和二进制.输出文件支持类型:XML 二进制 Stats(标准输出?) ...
- LINUX监控一:监控命令
简单的整理一下常用的linux监控命令 本篇参考了:http://www.cnblogs.com/JemBai/archive/2010/07/30/1788484.html的内容 1.top top ...
- Nodejs第一天-{Nodejs基础 深刻理解浏览器 环境变量 基础语法}
Nodejs第一天 1.什么是Nodejs Nodejs是一个可以运行(解析)ECMAScript的环境; ECMAScript是规定了一些列的语法 ,这些语法想要解析的执行就需要放在某个环境 ...