level 1
棍神一浩
楼主
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
const double MHI_DURATION=0.5;
const double MAX_TIME_DELTA=0.5;
const double MIN_TIME_DELTA=0.05;
const int N=3;
const int CONTOUR_MAX_AREA=10;
IplImage **buf=0;
int last=0;
IplImage *mhi=0;
CvFilter filter=CV_GAUSSIAN_5x5;
CvConnectedComp *cur_comp,min_comp;
CvConnectedComp comp;
CvMemStorage *storage;
CvPoint pt[4];
void update_mhi(IplImage* img,IplImage* dst,int diff_threshold)
{
double timestamp=clock()/100.;
CvSize size=cvSize(img->width,img->height);
int i,idx1,idx2;
IplImage* silh;
IplImage* pyr=cvCreateImage(cvSize((size.width&-2)/2,(size.height&-2)/2),8,1);
CvMemStorage *stor;
CvSeq *cont;
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<N;i++)
{
cvReleaseImage(&buf[i]);
buf[i]=cvCreateImage(size,IPL_DEPTH_8U,1);
cvZero(buf[i]);
}
cvReleaseImage(&mhi);
mhi=cvCreateImage(size,IPL_DEPTH_32F,1);
cvZero(mhi);
}
cvCvtColor(img,buf[last],CV_BGR2GRAY);
idx1=last;
idx2=(last+1)%N;
last=idx2;
silh=buf[idx2];
cvAbsDiff(buf[idx1],buf[idx2],silh);
cvThreshold(silh,silh,30,255,CV_THRESH_BINARY);
cvUpdateMotionHistory(silh,mhi,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);
cvPyrUp(pyr,dst,7);
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,cvPoint(0,0));
/*for(;cont;cont=cont->h_next)
{
if(cont->total<6)
continue;
cvDrawContours(img,cont,CV_RGB(255,0,0),CV_RGB(255,0,0),0,1,8,cvPoint(0,0));
}*/
for(;cont;cont=cont->h_next)
{
CvRect r=((CvContour*)cont)->rect;
if(r.height*r.width>CONTOUR_MAX_AREA)
{
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);
//cvCircle(img, cvPoint((r.x+r.width)/2,(r.y+r.height)/2), r.width<=r.height?r.width:r.height,CV_RGB(255,0,0),1);
}
}
cvReleaseMemStorage(&stor);
cvReleaseImage(&pyr);
}
int main(int argc,char** argv)
{
IplImage* motion=0;
CvCapture* capture=0;
if(argc==1||(argc==2&&strlen(argv[1])==1&&isdigit(argv[1][0])))
capture=cvCaptureFromCAM(argc==2?argv[1][0]-'0':0);
else if(argc==2)
capture=cvCaptureFromAVI(argv[1]);
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_mhi(image,motion,60);
cvShowImage("Motion",image);
if(cvWaitKey(10)>=0)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Motion");
}
return 0;
}
2017年03月18日 09点03分
1
#include "highgui.h"
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
const double MHI_DURATION=0.5;
const double MAX_TIME_DELTA=0.5;
const double MIN_TIME_DELTA=0.05;
const int N=3;
const int CONTOUR_MAX_AREA=10;
IplImage **buf=0;
int last=0;
IplImage *mhi=0;
CvFilter filter=CV_GAUSSIAN_5x5;
CvConnectedComp *cur_comp,min_comp;
CvConnectedComp comp;
CvMemStorage *storage;
CvPoint pt[4];
void update_mhi(IplImage* img,IplImage* dst,int diff_threshold)
{
double timestamp=clock()/100.;
CvSize size=cvSize(img->width,img->height);
int i,idx1,idx2;
IplImage* silh;
IplImage* pyr=cvCreateImage(cvSize((size.width&-2)/2,(size.height&-2)/2),8,1);
CvMemStorage *stor;
CvSeq *cont;
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<N;i++)
{
cvReleaseImage(&buf[i]);
buf[i]=cvCreateImage(size,IPL_DEPTH_8U,1);
cvZero(buf[i]);
}
cvReleaseImage(&mhi);
mhi=cvCreateImage(size,IPL_DEPTH_32F,1);
cvZero(mhi);
}
cvCvtColor(img,buf[last],CV_BGR2GRAY);
idx1=last;
idx2=(last+1)%N;
last=idx2;
silh=buf[idx2];
cvAbsDiff(buf[idx1],buf[idx2],silh);
cvThreshold(silh,silh,30,255,CV_THRESH_BINARY);
cvUpdateMotionHistory(silh,mhi,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);
cvPyrUp(pyr,dst,7);
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,cvPoint(0,0));
/*for(;cont;cont=cont->h_next)
{
if(cont->total<6)
continue;
cvDrawContours(img,cont,CV_RGB(255,0,0),CV_RGB(255,0,0),0,1,8,cvPoint(0,0));
}*/
for(;cont;cont=cont->h_next)
{
CvRect r=((CvContour*)cont)->rect;
if(r.height*r.width>CONTOUR_MAX_AREA)
{
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);
//cvCircle(img, cvPoint((r.x+r.width)/2,(r.y+r.height)/2), r.width<=r.height?r.width:r.height,CV_RGB(255,0,0),1);
}
}
cvReleaseMemStorage(&stor);
cvReleaseImage(&pyr);
}
int main(int argc,char** argv)
{
IplImage* motion=0;
CvCapture* capture=0;
if(argc==1||(argc==2&&strlen(argv[1])==1&&isdigit(argv[1][0])))
capture=cvCaptureFromCAM(argc==2?argv[1][0]-'0':0);
else if(argc==2)
capture=cvCaptureFromAVI(argv[1]);
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_mhi(image,motion,60);
cvShowImage("Motion",image);
if(cvWaitKey(10)>=0)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Motion");
}
return 0;
}
