opencv基于轮廓寻找的视频流运动检测

  1. #include "cv.h"  
  2. #include "highgui.h"  
  3. #include <time.h>  
  4. #include <math.h>  
  5. #include <ctype.h>  
  6. #include <stdio.h>  
  7. #include <string.h>  
  8. // various tracking parameters (in seconds) //跟踪的参数(单位为秒)  
  9. const double MHI_DURATION = 0.5;//0.5s为运动跟踪的最大持续时间  
  10. const double MAX_TIME_DELTA = 0.5;  
  11. const double MIN_TIME_DELTA = 0.05;  
  12. const int N = 3;  
  13. //  
  14. const int CONTOUR_MAX_AERA = 1000;  
  15. // ring image buffer 圈出图像缓冲  
  16. IplImage **buf = 0;//指针的指针  
  17. int last = 0;  
  18. // temporary images临时图像  
  19. IplImage *mhi = 0; // MHI: motion history image  
  20. CvFilter filter = CV_GAUSSIAN_5x5;  
  21. CvConnectedComp *cur_comp, min_comp;  
  22. CvConnectedComp comp;  
  23. CvMemStorage *storage;  
  24. CvPoint pt[4];  
  25. //  参数:  
  26. //  img – 输入视频帧  
  27. //  dst – 检测结果  
  28. void  update_mhi( IplImage* img, IplImage* dst, int diff_threshold )  
  29. {  
  30.     double timestamp = clock()/100.; // get current time in seconds 时间戳  
  31.     CvSize size = cvSize(img->width,img->height);  
  32.     // get current frame size,得到当前帧的尺寸  
  33.     int i, idx1, idx2;  
  34.     IplImage* silh;  
  35.     IplImage* pyr = cvCreateImage( cvSize((size.width & -2)/2, (size.height & -2)/2), 8, 1 );  
  36.     CvMemStorage *stor;  
  37.     CvSeq *cont;  
  38.   
  39.     /*先进行数据的初始化*/  
  40.     if( !mhi || mhi->width != size.width || mhi->height != size.height )  
  41.     {  
  42.         if( buf == 0 ) //若尚没有初始化则分配内存给他  
  43.         {  
  44.             buf = (IplImage**)malloc(N*sizeof(buf[0]));  
  45.             memset( buf, 0, N*sizeof(buf[0]));  
  46.         }  
  47.           
  48.         for( i = 0; i < N; i++ )  
  49.         {  
  50.             cvReleaseImage( &buf[i] );  
  51.             buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );  
  52.             cvZero( buf[i] );// clear Buffer Frame at the beginning  
  53.         }  
  54.         cvReleaseImage( &mhi );  
  55.         mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );  
  56.         cvZero( mhi ); // clear MHI at the beginning  
  57.     } // end of if(mhi)  
  58.     /*将当前要处理的帧转化为灰度放到buffer的最后一帧中*/  
  59.     cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale  
  60.     /*设定帧的序号*/  
  61.     /* 
  62.     last---->idx1 
  63.      ^ 
  64.      | 
  65.      | 
  66.      | 
  67.     idx2<-----(last+1)%3 
  68.     */  
  69.       
  70.     idx1 = last;  
  71.     idx2 = (last + 1) % N; // index of (last - (N-1))th frame  
  72.     last = idx2;  
  73.     // 做帧差  
  74.     silh = buf[idx2];//差值的指向idx2 |idx2-idx1|-->idx2(<-silh)  
  75.     cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames  
  76.       
  77.     // 对差图像做二值化  
  78.     cvThreshold( silh, silh, 30, 255, CV_THRESH_BINARY ); //threshold it,二值化  
  79.       
  80.     cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI  
  81.    
  82.     cvConvert( mhi, dst );//将mhi转化为dst,dst=mhi     
  83.       
  84.     // 中值滤波,消除小的噪声  
  85.     cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 );  
  86.       
  87.       
  88.     cvPyrDown( dst, pyr, CV_GAUSSIAN_5x5 );// 向下采样,去掉噪声,图像是原图像的四分之一  
  89.     cvDilate( pyr, pyr, 0, 1 );  // 做膨胀操作,消除目标的不连续空洞  
  90.     cvPyrUp( pyr, dst, CV_GAUSSIAN_5x5 );// 向上采样,恢复图像,图像是原图像的四倍  
  91.     //  
  92.     // 下面的程序段用来找到轮廓  
  93.     //  
  94.     // Create dynamic structure and sequence.  
  95.     stor = cvCreateMemStorage(0);  
  96.     cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);  
  97.       
  98.     // 找到所有轮廓  
  99.     cvFindContours( dst, stor, &cont, sizeof(CvContour),  
  100.                     CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));  
  101.     // 直接使用CONTOUR中的矩形来画轮廓  
  102.     for(;cont;cont = cont->h_next)  
  103.     {  
  104.               CvRect r = ((CvContour*)cont)->rect;  
  105.               if(r.height * r.width > CONTOUR_MAX_AERA) // 面积小的方形抛弃掉  
  106.               {  
  107.                   cvRectangle( img, cvPoint(r.x,r.y),  
  108.                           cvPoint(r.x + r.width, r.y + r.height),  
  109.                           CV_RGB(255,0,0), 1, CV_AA,0);  
  110.               }  
  111.     }  
  112.     // free memory  
  113.     cvReleaseMemStorage(&stor);  
  114.     cvReleaseImage( &pyr );  
  115. }  
  116. int main(int argc, char** argv)  
  117. {  
  118.     IplImage* motion = 0;  
  119.     CvCapture* capture = 0;  
  120.       
  121.     if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))  
  122.         capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );//摄像头为视频来源  
  123.     else if( argc == 2 )  
  124.         capture = cvCaptureFromAVI( argv[1] );//AVI为视频来源  
  125.     if( capture )  
  126.     {  
  127.         cvNamedWindow( "Motion", 1 );//建立窗口  
  128.         for(;;)  
  129.         {  
  130.             IplImage* image;  
  131.             if( !cvGrabFrame( capture ))//捕捉一桢  
  132.                 break;  
  133.             image = cvRetrieveFrame( capture );//取出这个帧  
  134.             if( image )//若取到则判断motion是否为空  
  135.             {  
  136.                 if( !motion )  
  137.                 {  
  138.                     motion = cvCreateImage( cvSize(image->width,image->height), 8, 1 );  
  139.                     //创建motion帧,八位,一通道  
  140.                     cvZero( motion );  
  141.                     //零填充motion  
  142.                     motion->origin = image->origin;  
  143.                     //内存存储的顺序和取出的帧相同  
  144.                 }  
  145.             }  
  146.             update_mhi( image, motion, 60 );//更新历史图像  
  147.             cvShowImage( "Motion", image );//显示处理过的图像  
  148.             if( cvWaitKey(10) >= 0 )//10ms中按任意键退出  
  149.                 break;  
  150.         }  
  151.         cvReleaseCapture( &capture );//释放设备  
  152.         cvDestroyWindow( "Motion" );//销毁窗口  
  153.     }  
  154.     return 0;  
  155. }   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值