Opencv学习之二帧差法运动目标检测与轮廓提取 ,供大家参考,具体内容如下
代码是从网上摘抄学习的,加了好多注释,感觉就像边看书边做笔记一样,给人以满足的享受。Let's do this!
#include "highgui.h" #include "cv.h" #include "stdio.h" #include#include #include const double MHI_DURATION=0.1;//运动跟踪的最大持续时间0.1s const double MAX_TIME_DELTA=0.5//最大时间增量0.5s const double MIN_TIME_DELTA=0.05;//最小时间增量0.05s const int N=3; const int CONTOUR_MAX_AERA=16; IplImage **buf=0; int last=0; IplImage* mhi=0;//运动历史图像mhi CvConnectedComp* cur_comp,mincomp; CvMemStorage* storage; CvPoint pt[4]; int nCurframeIndex=0; void update(IplImage *img,IplImage *dst,int diff_threshold) { double timestamp=clock()/100; CvSize size=cvSize(img->width,img->height); int i,idx1,idx2; IplImage* nimg; IplImage* pyr=cvCreateImage(cvSize((size.width&-2)/2,(size.height&-2)/2),8,1); CvMemStorage* stor; CvSeq* seq; if(!mhi||mhi->width!=size.width||mhi->height!=size.height) { if(buf==0) { buf=(IplImage**)malloc(N*sizeof(buf[0])); memset(buf,0,N*sizeof(buf[0])); } for(i=0;i h_next) { CvRect r=((CvContour*)cont)->rect;//将序列类型转换成轮廓类型的指针? if((r.height*r.width>CONTOUR_MAX_AERA)&&(r.height*r.width>2560)) { cvRectangle(img,//图像 cvPoint(r.x,r.y),//一个顶点 cvPoint(r.x + r.width, r.y + r.height),//另一个顶点 CV_RGB(255,0,0),//线条颜色 1,//线条粗细程度 CV_AA,//线条类型 0); //坐标点的小数点位数 } } cvReleaseMemStorage(&stor); cvReleaseImage(&pyr); } /处理视频,主函数/ int main(int argc,char**argv) { IplImage *motion=0; CvCapture *capture=0; capture=cvCaptureFromFile("D:\视频\01.mp4"); 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); cvZero(motion); motion->origin=image->origin; } } update(image,motion,10); cvShowImage("Motion",image); if(cvWaitKey(10)>=0) break; } cvReleaseCapture(&capture); cvDestroyWindow("Motion"); } return 0; }
经过测试,这个程序能够成功检测并用红色方框圈出移动的车辆和行人。
待改进的地方有:
①视频处理速度慢,导致视频处理速度只有视频正常播放速度的二分之一。
②对于行人的检测,画出的红色方框不稳定,不是将整个行人框出,经常会分别框出一个人的几个不同部位orz。
③当两个物体稍有重叠时,会将重叠物体当作一个物体圈出。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



