mình có đoạn code để capture từ video (1) hoặc từ camera(2). nhưng khi mình chọn từ video và nhập vào địa chỉ file.avi thì luôn bị lỗi "Unhandled exception at 0x00905a4d in demo.exe: 0xC0000005: Access violation."
int main(int argc, TCHAR* argv[])//MainProgram
{
int b=0,a=0,d=0;
CvCapture* capture;
int input=0,bfv = 0,counter = 0;
double mean=10;
double area1 = 0;
cout<<"Please specify input\nvideo(1) or webcam(2)?";
cin>> input;
if (input == 1)
{
char filename[255];
memset(filename,0,255);
cout<<"Please key in the adress to the video\n";
cin>>filename;
capture = cvCreateFileCapture( filename );
}
if (input == 2)
{
capture =cvCreateCameraCapture(0);
}
IplImage* start;
start = cvQueryFrame(capture);
for (int counter =0;counter<30;counter ++)
{
frame [counter] = NULL;
frameG [counter] = NULL;
frame2 [counter] = NULL;
frame2G [counter] = NULL;
frame3 [counter] = NULL;
frame3G [counter] = NULL;
}
while(1)
{
if(storage==NULL)storage = cvCreateMemStorage(0);
frame[a] = cvQueryFrame( capture );
IplImage* _img= cvQueryFrame( capture );
if(_img==NULL)break;
frame[a]= cvCreateImage(cvGetSize(_img),_img->depth,_img->nChannels);
frameG[a]=cvCreateImage(cvGetSize(_img),8,1);
frame2[a]=cvCreateImage(cvGetSize(_img),_img->depth,_img->nChannels);
frame2G[a]=cvCreateImage(cvGetSize(_img),8,1);
frame3[a]=cvCreateImage(cvGetSize(_img),_img->depth,_img->nChannels);
frame3G[a]=cvCreateImage(cvGetSize(_img),8,1);
cvCopy(_img,frame[a]);
if( !frame ) break;
char c = cvWaitKey(33);
if( c == 27 ) break;
cvCvtColor(frame[a], frameG[a], CV_BGR2GRAY);
CvScalar avg = cvAvg(frameG[a]);
cout<< avg.val[0]<< "\n";
if (avg.val[0] < 2)
{
mean=(avg.val[0]/0.015);
}
else if ((avg.val[0]>=2) && (avg.val[0] <= 70))
{
mean=(avg.val[0]/1.5);
}
else
{
mean =(avg.val[0]/5);
}
if(frame[29])
cvReleaseImage(&frame[29]);
if(frameG[29])
cvReleaseImage(&frameG[29]);
if(frame2[29])
cvReleaseImage(&frame2[29]);
if(frame2G[29])
cvReleaseImage(&frame2G[29]);
if(frame3[29])
cvReleaseImage(&frame3[29]);
if(frame3G[29])
cvReleaseImage(&frame3G[29]);
for ( int counter2=29;counter2>(0);counter2--)
{
frame[counter2]= frame [(counter2-1)];
frameG[counter2] = frameG [(counter2-1)];
frame2[counter2]= frame2 [(counter2-1)];
frame2G[counter2] = frame2G [(counter2-1)];
frame3[counter2]= frame3 [(counter2-1)];
frame3G[counter2]= frame3G [(counter2-1)];
}
bfv++;
a=0;
b=15;
if ( bfv >=30)
{
cvSub(frame[b],frame[a],frame2[a]);
cvSub(frame[d],frame[a],frame3[a]);
cvCvtColor(frame2[a],frame2G[a],CV_BGR2GRAY);
cvThreshold(frame2G[a],frame3G[a],mean,255,CV_THRESH_BINARY);
cvSmooth(frame3G[a],frame3G[a],CV_MEDIAN,9,9);
cvDilate(frame3G[a],frame3G[a],NULL,5);
cvErode(frame3G[a],frame3G[a],NULL,6);
cvFindContours(frame3G[a],storage,&contour,sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
IplImage*frameb = frame[15];
IplImage*framec = frame[16];
contourprocess(frameb,framec,contour,area1);
if (contour!=NULL)
{ cvClearSeq(contour);
cvClearMemStorage(storage);
}
}
}
printf("End of File\n");
cvReleaseMemStorage(&storage);
cvReleaseCapture( &capture );
//cvDestroyWindow( "FireDetect" );
cvDestroyAllWindows();
fclose(fp);
return 0;
}
int main(int argc, TCHAR* argv[])//MainProgram
{
int b=0,a=0,d=0;
CvCapture* capture;
int input=0,bfv = 0,counter = 0;
double mean=10;
double area1 = 0;
cout<<"Please specify input\nvideo(1) or webcam(2)?";
cin>> input;
if (input == 1)
{
char filename[255];
memset(filename,0,255);
cout<<"Please key in the adress to the video\n";
cin>>filename;
capture = cvCreateFileCapture( filename );
}
if (input == 2)
{
capture =cvCreateCameraCapture(0);
}
IplImage* start;
start = cvQueryFrame(capture);
for (int counter =0;counter<30;counter ++)
{
frame [counter] = NULL;
frameG [counter] = NULL;
frame2 [counter] = NULL;
frame2G [counter] = NULL;
frame3 [counter] = NULL;
frame3G [counter] = NULL;
}
while(1)
{
if(storage==NULL)storage = cvCreateMemStorage(0);
frame[a] = cvQueryFrame( capture );
IplImage* _img= cvQueryFrame( capture );
if(_img==NULL)break;
frame[a]= cvCreateImage(cvGetSize(_img),_img->depth,_img->nChannels);
frameG[a]=cvCreateImage(cvGetSize(_img),8,1);
frame2[a]=cvCreateImage(cvGetSize(_img),_img->depth,_img->nChannels);
frame2G[a]=cvCreateImage(cvGetSize(_img),8,1);
frame3[a]=cvCreateImage(cvGetSize(_img),_img->depth,_img->nChannels);
frame3G[a]=cvCreateImage(cvGetSize(_img),8,1);
cvCopy(_img,frame[a]);
if( !frame ) break;
char c = cvWaitKey(33);
if( c == 27 ) break;
cvCvtColor(frame[a], frameG[a], CV_BGR2GRAY);
CvScalar avg = cvAvg(frameG[a]);
cout<< avg.val[0]<< "\n";
if (avg.val[0] < 2)
{
mean=(avg.val[0]/0.015);
}
else if ((avg.val[0]>=2) && (avg.val[0] <= 70))
{
mean=(avg.val[0]/1.5);
}
else
{
mean =(avg.val[0]/5);
}
if(frame[29])
cvReleaseImage(&frame[29]);
if(frameG[29])
cvReleaseImage(&frameG[29]);
if(frame2[29])
cvReleaseImage(&frame2[29]);
if(frame2G[29])
cvReleaseImage(&frame2G[29]);
if(frame3[29])
cvReleaseImage(&frame3[29]);
if(frame3G[29])
cvReleaseImage(&frame3G[29]);
for ( int counter2=29;counter2>(0);counter2--)
{
frame[counter2]= frame [(counter2-1)];
frameG[counter2] = frameG [(counter2-1)];
frame2[counter2]= frame2 [(counter2-1)];
frame2G[counter2] = frame2G [(counter2-1)];
frame3[counter2]= frame3 [(counter2-1)];
frame3G[counter2]= frame3G [(counter2-1)];
}
bfv++;
a=0;
b=15;
if ( bfv >=30)
{
cvSub(frame[b],frame[a],frame2[a]);
cvSub(frame[d],frame[a],frame3[a]);
cvCvtColor(frame2[a],frame2G[a],CV_BGR2GRAY);
cvThreshold(frame2G[a],frame3G[a],mean,255,CV_THRESH_BINARY);
cvSmooth(frame3G[a],frame3G[a],CV_MEDIAN,9,9);
cvDilate(frame3G[a],frame3G[a],NULL,5);
cvErode(frame3G[a],frame3G[a],NULL,6);
cvFindContours(frame3G[a],storage,&contour,sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
IplImage*frameb = frame[15];
IplImage*framec = frame[16];
contourprocess(frameb,framec,contour,area1);
if (contour!=NULL)
{ cvClearSeq(contour);
cvClearMemStorage(storage);
}
}
}
printf("End of File\n");
cvReleaseMemStorage(&storage);
cvReleaseCapture( &capture );
//cvDestroyWindow( "FireDetect" );
cvDestroyAllWindows();
fclose(fp);
return 0;
}
Comment