Why does the function call cvCvtColor() leads to a crash program

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does the function call cvCvtColor() leads to a crash program

Prognmagn
This post has NOT been accepted by the mailing list yet.
Hello!

Why does the function call cvCvtColor() leads to a crash program?

Environment: Qt 4.6.2 + OpenCV 2.1.0

My code:

    CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);

    CvSize imgSize;                
    imgSize.width = 640;
    imgSize.height = 480;

    IplImage* colourImage  = cvCloneImage(cvQueryFrame(capture));
    IplImage* greyImage    = cvCloneImage(cvQueryFrame(capture));

    IplImage* movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
    IplImage* difference    = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
    IplImage* temp          = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);

    cvNamedWindow( "Image", 1 );
    cvNamedWindow( "BG", 1 );
    cvNamedWindow( "Source", 1 );

    int key=-1;
    int flag=0;
    while(key != 'q')
      {
        cvCopyImage(cvQueryFrame(capture), colourImage);
        cvCvtColor(colourImage,greyImage,CV_BGR2GRAY);

        if (flag==0)
        {
            cvConvertScale(colourImage,movingAverage,1.0,0.0);
            flag=1;
        }
        else
        {
            cvRunningAvg( colourImage, movingAverage, 0.015 ,NULL);
        }

        cvConvertScale(movingAverage,temp,1.0,0.0);
        cvShowImage("BG",temp);
        cvAbsDiff(colourImage,temp,difference);
        cvThreshold(difference,difference,50,255,CV_THRESH_BINARY);
        cvCvtColor( difference,greyImage, CV_BGR2GRAY );

        cvShowImage("Source",colourImage);
        cvShowImage("Image",greyImage);

        key = cvWaitKey(1);
      }

Thank you for your attention.
Reply | Threaded
Open this post in threaded view
|

Re: Why does the function call cvCvtColor() leads to a crash program

Matthias Schmieder
Hi,
I'm not entirely sure, but I think the BGR2Gray conversion expects a single channel image.

Try this code:

    CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);

    CvSize imgSize;                
    imgSize.width = 640;
    imgSize.height = 480;

    IplImage* colourImage  = cvCloneImage(cvQueryFrame(capture));
    IplImage* greyImage    = cvCreateImage( cvGetSize(colourImage), IPL_DEPTH_8U, 1);

    IplImage* movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
    IplImage* difference    = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
    IplImage* temp          = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);

    cvNamedWindow( "Image", 1 );
    cvNamedWindow( "BG", 1 );
    cvNamedWindow( "Source", 1 );

    int key=-1;
    int flag=0;
    while(key != 'q')
      {
        cvCopyImage(cvQueryFrame(capture), colourImage);
        cvCvtColor(colourImage,greyImage,CV_BGR2GRAY);

        if (flag==0)
        {
            cvConvertScale(colourImage,movingAverage,1.0,0.0);
            flag=1;
        }
        else
        {
            cvRunningAvg( colourImage, movingAverage, 0.015 ,NULL);
        }

        cvConvertScale(movingAverage,temp,1.0,0.0);
        cvShowImage("BG",temp);
        cvAbsDiff(colourImage,temp,difference);
        cvThreshold(difference,difference,50,255,CV_THRESH_BINARY);
        cvCvtColor( difference,greyImage, CV_BGR2GRAY );

        cvShowImage("Source",colourImage);
        cvShowImage("Image",greyImage);

        key = cvWaitKey(1);
      }
 
Reply | Threaded
Open this post in threaded view
|

Re: Why does the function call cvCvtColor() leads to a crash program

Prognmagn
This post has NOT been accepted by the mailing list yet.
Thanks!