视频摘要 视频浓缩(一)
视频摘要又称视频浓缩,是对视频内容的一个简单概括,以自动或半自动的方式,先通过运动目标分析,提取运动目标,然后对各个目标的运动轨迹进行分析,将不同的目标拼接到一个共同的背景场景中,并将它们以某种方式进行组合。视频摘要在视频分析和基于内容的视频检索中扮演着重要角色。
视频录像存在存储数据量大,存储时间长等特点,通过录像寻找线索,获取证据传统的做法是要耗费大量人力、物力以及时间,效率极其低下,以至于错过最佳破案时机。因此在视频监控系统中,对原始视频进行浓缩,可以快速浏览,锁定检索对象,对于公安加快破案速度,提高大案、要案的破案效率具有重要指导意义。(1)
对于企业应用来说,视频摘要与压缩技术可以使企业管理人员在短时间内浏览完视频。在智能手机大行其道的今天,使用视频摘要技术对监控视频进行处理,供手机浏览,既可以节约管理者的时间,又可以节约大量的流量。(2)
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205254968-447913273.jpg)
![](e2a98c0f-c1c6-4812-b92a-5b2079e19632_files/5bc0d188-3ee9-4e36-8e18-441fb35f0417.jpg)
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205518515-1172043052.jpg)
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205550155-681042676.jpg)
void GoBgModeling( const char * videoFilePath, const int frame_num_used, Mat * bgMat, \ const int size1, const int size2, const int sigma1, const int sigma2 ){ //声明 int frame_no = 0; Mat frame; Mat tmp; VideoCapture pCapture(videoFilePath); //自己选取一段avi视频 if( !pCapture.isOpened()){ printf( "Unable to open video file for background modeling!\n"); return; } printf( "Background Modeling...\n"); //逐帧读取视频 Mat matTmp; while(frame_no < frame_num_used){ pCapture >>frame; frame.convertTo(frame,CV_32FC3); frame_no += 1; if(frame_no == 1){ //初始化 tmp = Mat : :zeros(frame.rows,frame.cols,CV_32FC3); matTmp = frame.clone(); } tmp = tmp + frame /frame_num_used; *bgMat = tmp; } bgMat - >convertTo( *bgMat,CV_8UC3); matTmp.convertTo(matTmp,CV_8UC3); absdiff(matTmp, *bgMat,matTmp); printf( "Background Model has been achieved!\n"); }
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205601609-1016016345.jpg)
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205612968-929265802.jpg)
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205614593-1050365281.jpg)
/** *背景建模 */ void bgModeling( const char * videoFilePath, const int frame_num_used, IplImage * * bgImg, \ const int size1, const int size2, const int sigma1, const int sigma2){ //声明 IplImage * frame = NULL; CvMat * frameMat = NULL; CvMat * bgMat = NULL; CvCapture * pCapture = NULL; IplImage * framtmp = NULL; CvMat * mattmp = NULL; int frame_no = 0; pCapture = cvCaptureFromFile(videoFilePath); //自己选取一段avi视频 if( !pCapture){ printf( "Unable to open video file for background modeling!\n"); return; } if( *bgImg != NULL){ //非空需先清空*bgImg指向的内存 cvReleaseImage(bgImg); } printf( "Background Modeling...\n"); //逐帧读取视频 while(frame_no < frame_num_used){ frame = cvQueryFrame(pCapture); frame_no += 1; if(frame_no == 1){ //初始化 framtmp = cvCreateImage(cvSize(frame - >width, frame - >height), frame - >depth, frame - >nChannels); cvCopy(frame,framtmp); *bgImg = cvCreateImage(cvSize(frame - >width, frame - >height), frame - >depth, frame - >nChannels); cvCopy(frame, *bgImg); frameMat = cvCreateMat(frame - >height, frame - >width, CV_32FC3); bgMat = cvCreateMat(( *bgImg) - >height, ( *bgImg) - >width, CV_32FC3); cvConvert(frame, frameMat); cvConvert( *bgImg, bgMat); continue; } //视频帧IplImage转CvMat cvConvert(frame, frameMat); //高斯滤波先,以平滑图像 cvSmooth(frame, frame, CV_GAUSSIAN, size1, size2, sigma1, sigma2); //滑动平均更新背景(求平均) cvRunningAvg(frameMat, bgMat, ( double) 1 /frame_num_used); } cvConvert(bgMat, *bgImg); printf( "Background Model has been achieved!\n"); //释放内存 cvReleaseCapture( &pCapture); cvReleaseMat( &frameMat); cvReleaseMat( &bgMat); }
![](https://images2015.cnblogs.com/blog/508489/201605/508489-20160514205617968-423857991.jpg)
{ Mat bgMat; // GoBgModeling("1.avi",100,&bgMat); cv : :VideoCapture capture; capture.open( "1.avi"); if ( !capture.isOpened()) { std : :cout << "read video failure" <<std : :endl; return - 1; } cv : :BackgroundSubtractorMOG2 mog; cv : :Mat foreground; cv : :Mat background; cv : :Mat frame; long frameNo = 0; while (capture.read(frame)) { ++frameNo; std : :cout <<frameNo <<std : :endl; // 运动前景检测,并更新背景 mog(frame, foreground, 0. 001); // 腐蚀 cv : :erode(foreground, foreground, cv : :Mat()); // 膨胀 cv : :dilate(foreground, foreground, cv : :Mat()); mog.getBackgroundImage(background); // 返回当前背景图像 cv : :imshow( "video", foreground); cv : :imshow( "background", background); if (cv : :waitKey( 25) > 0) { break; } } return 0; }