OpenCV Error- Assertion failed (dst.data == dst0.data) when converting to grayscale and background subtraction. Please help me! Thank you very much.
include "stdio.h"
include "openc\cv.h"
include "opencv\cxcore.h"
include "opencv\highgui.h"
define WIDTH 320
define HEIGHT 240
int main( int argc, char **argv ){
int key;
CvCapture *capture = NULL;
IplImage *frameImage;
IplImage *backgroundImage = cvCreateImage( cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 1);
IplImage *grayImage = cvCreateImage( cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 1);
IplImage *differenceImage = cvCreateImage( cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 1);
char windowNameCapture[]="Capture";
char windowNameDifference[]="Difference";
if(( capture = cvCreateCameraCapture(-1)) == NULL) {
printf( "カメラが見つかりません\n");
return -1;
}
cvNamedWindow( windowNameCapture, CV_WINDOW_AUTOSIZE );
cvNamedWindow( windowNameDifference, CV_WINDOW_AUTOSIZE );
frameImage = cvQueryFrame( capture );
cvCvtColor( frameImage, backgroundImage, CV_BGR2GRAY );
while( 1 ){
frameImage = cvQueryFrame( capture );
cvCvtColor( frameImage, grayImage, CV_BGR2GRAY );
cvAbsDiff( grayImage, backgroundImage, differenceImage );
if( (*differenceImage).origin == 0 ) {
cvFlip( differenceImage, differenceImage, 0 );
}
cvShowImage( windowNameCapture, frameImage );
cvShowImage( windowNameDifference, differenceImage );
key = cvWaitKey( 1 );
if( key == 'q' ) {
break;
} else if( key == 'b' ) {
frameImage = cvQueryFrame( capture );
cvCvtColor( frameImage, backgroundImage, CV_BGR2GRAY );
}
}
cvReleaseCapture( &capture );
cvReleaseImage( &backgroundImage );
cvReleaseImage( &grayImage );
cvReleaseImage( &differenceImage );
cvDestroyWindow( windowNameCapture );
cvDestroyWindow( windowNameDifference );
return 0;
}
please, do not use the arcane c-api.
your problem will simply vanish, once you re-write it using the c++ api.
thanks for your quick answer, but i'm using C++ now
you're using a c++ compiler, but that's opencv's c-api. they did away with that in 2010 already.
you should use cv::Mat instead of IplImages, functions from the cv::namespace instead of those prefixed with cv. you won't have to pre-allocate result Mats anymore, also no more need to release anything.
And when i run the program, it throws up a messagebox saying: An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in test.exe
Oh, i see.Thank you so much. I will follow your advice.
also, would you be so nice, to format your code in the question properly ?
(edit, mark it, press 10101 button)