我最近呢,正在做运动识别跟踪程序,用opencv想着把运动目标提取出来加上轮廓求质心呢,结果有错误。
#include"cv.h"
#include"highgui.h"
#include "cxcore.h"
#include <D:\opencv\opencv\build\include\opencv2\legacy\legacy.hpp>
using namespace std;
using namespace cv;
IplImage* doCanny(IplImage* in,
double lowThresh,
double highThresh,
double aperture);
int main(int argc,char** argv)
{
cvNamedWindow("trace",CV_WINDOW_AUTOSIZE);
cvNamedWindow("fore",CV_WINDOW_AUTOSIZE);
CvCapture* capture=cvCreateFileCapture("bike.avi");
IplImage* frame1;
IplImage* curr=NULL;//use to store the first image
IplImage* next=NULL;//use to store the second image
IplImage* fore=NULL;//use to store the diversity between the first and the second image
IplImage* result=NULL;
IplImage* result1=NULL;
IplImage* result2=NULL;
IplImage* img2=NULL;
IplImage* img3=NULL;
double fps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
int frames=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
int flag=0;
while(frames>60)
{
//first should get the first image from the video
frame1=cvQueryFrame(capture);
curr=cvQueryFrame(capture);
flag++;
if(flag==1)
{
next=cvCreateImage(cvGetSize(curr),8,3);
fore=cvCreateImage(cvGetSize(curr),8,3);
result=cvCreateImage(cvGetSize(curr),8,3);
result1=cvCreateImage(cvGetSize(curr),8,3);
result2=cvCreateImage(cvGetSize(curr),8,3);
cvCopyImage(curr,next);//set the next is current iamge
}
else
{
cvAbsDiff(curr,next,fore);
//增加对于差图的处理
cvSmooth(fore,result,CV_GAUSSIAN,3,3);//delete some noise
CvMemStorage* storage=cvCreateMemStorage(0);//apply new store space
CvSeq* comp=0;
cvPyrSegmentation(result,result1,storage,&comp,4,200,50);
int n_comp=comp->total;//get the connected area number,we hope is 1,if not equals 1 we will use the Dilate
img3=cvCreateImage(cvGetSize(curr),8,3);
cvThreshold(result1,result2, 60, 255, CV_THRESH_BINARY);
cvDilate(result2,img3,NULL,2);
int mode=CV_RETR_EXTERNAL;
cvFindContours(img3,storage,&comp,sizeof(CvContour),mode,CV_CHAIN_APPROX_SIMPLE);//一到这里就出问题了,编译无错误,运行出现错误了
int i;
for(i=1;comp!=0;comp->h_next)
{//get the area
double area=fabs(cvContourArea(comp,CV_WHOLE_SEQ));
CvScalar color = CV_RGB(i, rand()&255, rand()&255);
cvDrawContours(img3, comp, color, color, -1, CV_FILLED, 8 );
i++; // label递增
}
// img3=doCanny(img2,10.0,100,3);//make a canny detect
cvShowImage("trace",frame1);
cvShowImage("fore",img3);
char s=cvWaitKey(33);
if(s==27) break;
frames--;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("fore");
cvDestroyWindow("trace");
return(0);
}