运动模板检测_第1页
运动模板检测_第2页
运动模板检测_第3页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、运动模板检测(摄像头)#include "cv.h"#include "highgui.h"#include#include#include#include/ various tracking parameters (in seconds) const double MHI_DURATION = 0.5;const double MAX_TIME_DELTA = 0.5;const double MIN_TIME_DELTA = 0.05;/ 用于运动检测的循环帧数, 与机器速度以及 FPS 设置有关 const int N = 2;/ ring ima

2、ge bufferIplImage *buf = 0;int last = 0;/ temporary imagesIplImage *mhi = 0; / MHI: motion history imageIplImage *orient = 0; / orientationIplImage *mask = 0; / valid orientation maskIplImage *segmask = 0; / motion segmentation mapCvMemStorage* storage = 0; / temporary storage/ parameters: / img - i

3、nput video frame/ dst - resultant motion picture/ args - optional parametersvoid update_mhi( IplImage* img, IplImage* dst, int diff_threshold )double timestamp = clock()/1000.; / get current time in secondsCvSize size = cvSize(img->width,img->height); / get current frame sizeint i, idx

4、1 = last, idx2;IplImage* silh;CvSeq* seq;CvRect comp_rect;double count;double angle;CvPoint center;double magnitude;CvScalar color;/ allocate images at the beginning or/ reallocate them if the frame size is changed if( !mhi | mhi->width != size.width | mhi->height != size.height )if( b

5、uf = 0 )buf = (IplImage*)malloc(N*sizeof(buf0); memset( buf, 0, N*sizeof(buf0);for( i = 0; i < N; i+ ) cvReleaseImage( &bufi );bufi = cvCreateImage( size, IPL_DEPTH_8U, 1 );cvZero( bufi );cvReleaseImage( &mhi ); cvReleaseImage( &orient );cvReleaseImage( &segmas

6、k ); cvReleaseImage( &mask );mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); cvZero( mhi ); / clear MHI at the beginning orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); segmask = cvCreateImage( size, IPL_DEPTH_32F,1 );mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );cvCvtColor( img, buflast, C

7、V_BGR2GRAY ); /convert frame to grayscaleidx2 = (last + 1) % N; / index of(last - (N-1)th framelast = idx2; silh = bufidx2;/ 相邻两帧的差cvAbsDiff( bufidx1, bufidx2, silh ); / get difference between frames/ 对差图像做二值化 cvThreshold( silh, silh, diff_threshold, 1,CV_THRESH_BINARY ); / and threshold it cvUpdate

8、MotionHistory( silh, mhi, timestamp,MHI_DURATION ); / update MHI/ convert MHI to blue8u image/ cvCvtScale 的第四个参数 shift = (MHI_DURATION - timestamp)*255./MHI_DURATION/ 控制帧差的消失速率cvCvtScale( mhi, mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION ); cvZero( dst );cvCvtPlaneToPix(mask

9、, 0, 0, 0, dst ); / B,G,R,0 >dist : convert to BLUE image / 计算运动的梯度方向以及 正确的方向掩模 mask/ Filter size = 3 cvCalcMotionGradient( mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3 );if( !storage ) storage = cvCreateMemStorage(0);else cvClearMemStorage(storage);/ 运动分割 : 获得运动部件的连续序列/ segmask is ma

10、rked motion components map. It is not used furtherseq = cvSegmentMotion( mhi, segmask, storage, timestamp, MAX_TIME_DELTA );/ iterate through the motioncomponents,/ One more iteration (i = -1) corresponds to the whole image (global motion)for( i = 0; i < seq->total; i+ )if( i <

11、0 ) / case of the whole image ,对整幅图像做操作comp_rect = cvRect( 0, 0, size.width,size.height );color = CV_RGB(255,255,255); / white color magnitude = 100; / 画线长度以及圆半径的大 小控制else / i-th motion componentcomp_rect =(CvConnectedComp*)cvGetSeqElem( seq, i )->rect;/ 去掉小的部分if( comp_rect.width + comp_rect.

12、height < 100 ) continue;color = CV_RGB(255,0,0);/ red colormagnitude = 30;/if(seq->total > 0) MessageBox(NULL,"Motion Detected",NULL,0); / select component ROIcvSetImageROI( silh, comp_rect );cvSetImageROI( mhi, comp_rect );cvSetImageROI( orient, comp_rect );cvSetImage

13、ROI( mask, comp_rect );/ 在选择的区域内 , 计算运动方向angle = cvCalcGlobalOrientation( orient, mask, mhi,timestamp, MHI_DURATION);angle = 360.0 - angle; / adjust for images with top-left origin / 在轮廓内计算点数/ Norm(L1) = sum of total pixel valuescount = cvNorm( silh, 0, CV_L1, 0 );/ Thefunction cvResetImageROI relea

14、ses image ROIcvResetImageROI( mhi );cvResetImageROI( orient );cvResetImageROI( mask );cvResetImageROI( silh );/ check for thecase of little motionif( count < comp_rect.width*comp_rect.height *0.05 ) / five percent of pixelcontinue;/ draw a clock with arrowindicating the directioncenter = cvPo

15、int( (comp_rect.x + comp_rect.width/2), (comp_rect.y + comp_rect.height/2) );cvCircle( dst, center,cvRound(magnitude*1.2), color, 3, CV_AA, 0 );cvLine( dst, center, cvPoint( cvRound( center.x +magnitude*cos(angle*CV_PI/180),cvRound( center.y -magnitude*sin(angle*CV_PI/180),color, 3, CV_AA, 0 );int m

16、ain(int argc, char* argv)IplImage* motion = 0;CvCapture* capture = 0;if( argc = 1 | (argc = 2 && strlen(argv1) =1 && isdigit(argv10)capture = cvCaptureFromCAM( argc = 2 ? argv10 - '0' : 0 );else if( argc = 2 )capture = cvCaptureFromA VI( argv1 );if( capture ) cvNamedWindow( "Motion", 1 );for(;)IplImage* image;if( !cvGrabFrame( capture )break;image = cvRetrieveFrame( capture );if( image )if( !motion )motion =cvCreateImage( cvSize(image->width,image->height),8,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论