资源描述:
《Opencv视频运动跟踪代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include"cv.h"#include"highgui.h"#include#include#include#include#include#include#include//跟踪参数constdoubleMHI_DURATION=0.5;//最大跟踪时间constdoubleMAX_TIME_DELTA=0.5;constdoubleMIN_TIME_DELTA=0.05;constintN=3;constintC
2、ONTOUR_MAX_AERA=100;//矩形面积IplImage**buf=0;intlast=0;intflag;IplImage*mhi=0;//MHI:motionhistoryimageCvConnectedComp*cur_comp,min_comp;CvConnectedCompcomp;CvMemStorage*storage;CvPointpt[4];//img–输入视频帧//dst–检测结果voidupdate_mhi(IplImage*img,IplImage*dst,intdiff_threshold){doubletimest
3、amp=clock()/100.;//获取当前时间CvSizesize=cvSize(img->width,img->height);inti,idx1,idx2;IplImage*silh;IplImage*pyr=cvCreateImage(cvSize((size.width&-2)/2,(size.height&-2)/2),8,1);CvMemStorage*stor;CvSeq*cont;if(!mhi
4、
5、mhi->width!=size.width
6、
7、mhi->height!=size.height){if(buf==0){buf=(Ipl
8、Image**)malloc(N*sizeof(buf[0]));//动态内存分配memset(buf,0,N*sizeof(buf[0]));}for(i=0;i9、AY);//rgb->grayidx1=last;idx2=(last+1)%N;last=idx2;//做帧差silh=buf[idx2];cvAbsDiff(buf[idx1],buf[idx2],silh);//两帧差异//对差图像做二值化cvThreshold(silh,silh,30,255,CV_THRESH_BINARY);//src(x,y)>threshold,dst(x,y)=max_value;否则,dst(x,y)=0;cvUpdateMotionHistory(silh,mhi,timestamp,MHI_DURATION);/
10、/更新像素点的运动历史cvCvtScale(mhi,dst,255./MHI_DURATION,(MHI_DURATION-timestamp)*255./MHI_DURATION);//timestamp是时间戳;MHI_DURATION,获得的是当前时间cvCvtScale(mhi,dst,255./MHI_DURATION,0);//中值滤波,消除小的噪声cvSmooth(dst,dst,CV_MEDIAN,3,0,0,0);//向下采样,去掉噪声cvPyrDown(dst,pyr,7);cvDilate(pyr,pyr,0,1);//做膨胀操作,
11、消除目标的不连续空洞cvPyrUp(pyr,dst,7);////下面的程序段用来找到轮廓////Createdynamicstructureandsequence.stor=cvCreateMemStorage(0);cont=cvCreateSeq(CV_SEQ_ELTYPE_POINT,sizeof(CvSeq),sizeof(CvPoint),stor);//找到所有轮廓cvFindContours(dst,stor,&cont,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE,cvPoi
12、nt(0,0));//直接使用CONTOUR中的矩形来画轮廓for(;cont;