视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数;第二个函数是VideoAnalysis::start(),其主要功能初始化视频处理、设置视频获取方式以及开始视频捕获功能等。
1、VideoAnalysis::setup(....)
该函数的代码如下:
- bool VideoAnalysis::setup(int argc, const char **argv)
- {
- bool flag = false;
- const char* keys =
- "{hp|help|false|Print help message}"
- "{uf|use_file|false|Use video file}"
- "{fn|filename||Specify video file}"
- "{uc|use_cam|false|Use camera}"
- "{ca|camera|0|Specify camera index}"
- "{co|use_comp|false|Use mask comparator}"
- "{st|stopAt|0|Frame number to stop}"
- "{im|imgref||Specify image file}" ;
- cv::CommandLineParser cmd(argc, argv, keys);
- ////////////use_command
- if (argc <= 1 || cmd.get<bool>("help") == true)
- {
- cout << "Usage: " << argv[0] << " [options]" << endl;
- cout << "Avaible options:" << endl;
- cmd.printParams();
- return false;
- }
- ////////////use_file
- use_file = cmd.get<bool>("use_file");
- if (use_file)
- {
- filename = cmd.get<string>("filename");
- if (filename.empty())
- {
- cout << "Specify filename" << endl;
- return false;
- }
- flag = true;
- }
- ////////////use_camera
- use_camera = cmd.get<bool>("use_cam");
- if (use_camera)
- {
- cameraIndex = cmd.get<int>("camera");
- flag = true;
- }
- ////////////use_comp
- if (flag == true)
- {
- use_comp = cmd.get<bool>("use_comp");
- if (use_comp)
- {
- frameToStop = cmd.get<int>("stopAt");
- imgref = cmd.get<string>("imgref");
- if (imgref.empty())
- {
- cout << "Specify image reference" << endl;
- return false;
- }
- }
- }
- return flag;
- }
它的主要流程如下图所示:
2、VideoAnalysis::start()
该函数的代码如下:
- void VideoAnalysis::start()
- {
- //cout << "Press 'ESC' to stop..." << endl;
- do
- {
- videoCapture = new VideoCapture;
- frameProcessor = new FrameProcessor;
- frameProcessor->init();
- frameProcessor->frameToStop = frameToStop;
- frameProcessor->imgref = imgref;
- videoCapture->setFrameProcessor(frameProcessor);///setFrameProcessor
- if (use_file)
- videoCapture->setVideo(filename);///setVideo
- if (use_camera)
- videoCapture->setCamera(cameraIndex);///setCamera
- videoCapture->start();///start
- if (use_file || use_camera)
- break;
- frameProcessor->finish();
- int key = cvWaitKey(500);
- if (key == KEY_ESC)
- break;
- delete frameProcessor;
- delete videoCapture;
- }
- while (1);
- delete frameProcessor;
- delete videoCapture;
- }
它的主要流程如下图所示: