Ask Your Question
-1

OpenCV Error- Assertion failed (dst.data == dst0.data) when converting to grayscale and background subtraction. Please help me! Thank you very much.

asked 2014-07-10 03:13:53 -0600

hamybomi gravatar image

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;
}
edit retag flag offensive close merge delete

Comments

please, do not use the arcane c-api.

your problem will simply vanish, once you re-write it using the c++ api.

berak gravatar imageberak ( 2014-07-10 03:39:04 -0600 )edit

thanks for your quick answer, but i'm using C++ now

hamybomi gravatar imagehamybomi ( 2014-07-10 03:49:45 -0600 )edit

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.

berak gravatar imageberak ( 2014-07-10 03:55:06 -0600 )edit

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

hamybomi gravatar imagehamybomi ( 2014-07-10 03:56:35 -0600 )edit

Oh, i see.Thank you so much. I will follow your advice.

hamybomi gravatar imagehamybomi ( 2014-07-10 04:06:07 -0600 )edit

also, would you be so nice, to format your code in the question properly ?

(edit, mark it, press 10101 button)

berak gravatar imageberak ( 2014-07-10 04:17:27 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2014-07-10 04:06:25 -0600

berak gravatar image

updated 2014-07-10 04:07:34 -0600

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat prev;
    VideoCapture cap(0); // 1st cam
    while( cap.isOpened() )
    {
        Mat frame,gray,diff;
        if ( ! cap.read(frame) )
            break;
        imshow("lalala",frame);
        cvtColor( frame, gray, CV_BGR2GRAY );
        if ( ! prev.empty() )
        {
            absdiff( gray, prev, diff );
            imshow("diff",diff);
        }
        prev = gray; // swap
        int k = waitKey(10);
        if ( k==27 )
            break;
    }
    return 0;
}
edit flag offensive delete link more

Comments

Great! thanks for your help, my friend! it sounds very interesting...

hamybomi gravatar imagehamybomi ( 2014-07-10 05:41:31 -0600 )edit

Question Tools

Stats

Asked: 2014-07-10 03:13:53 -0600

Seen: 2,647 times

Last updated: Jul 10 '14