2

I have a yuv camera.

I convert yuv to bgr (because of opencv use bgr) but I get an exception:

Unhandled exception at 0x76c1a832 in test1.exe: Microsoft C++ exception: cv::Exception at memory location 0x00baee60..

How can I fix it?

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>

void main()
{
    IplImage* image ;
    CvCapture* capture=cvCaptureFromCAM(CV_CAP_ANY);

    //cv::Mat input;
    cv::Mat output;

    cvNamedWindow("webcam",1);

    cvGrabFrame( capture );

    image = cvRetrieveFrame( capture );

    cv::Mat input = cv::cvarrToMat(image);

    cv::cvtColor(input,output,CV_YUV2BGR_YUY2);

    imshow("webcam", output);


    /*
    while(1)
    {
        //get image from Camera
        image = cvQueryFrame(capture);

        //Iplimage to Mat
        cv::Mat input = cv::cvarrToMat(image);

        //YUV to RGB, CV_YUV2RGB_NV12 CV_YUV2BGR_NV12 CV_YUV2RGB_YV12 CV_YUV2BGR_YV12 CV_YUV2RGB_IYUV CV_YUV2BGR_IYUV CV_YUV2RGB_UYVY CV_YUV2BGR_UYVY
        cv::cvtColor(input,output,CV_YUV2BGR_YUY2);

        // Draw image
        //cvShowImage("webcam", image);
        imshow("webcam", output);


        //key = cvWaitKey(30);
        if(cvWaitKey(33)>=27)
            break;
    }
    */

    cvReleaseCapture(&capture);
    cvDestroyWindow("webcam");
}
3
  • 2
    Do yourself a favour and use the C++ API, the concerned code can be found at docs.opencv.org/3.0-beta/modules/videoio/doc/… Commented Feb 14, 2017 at 9:08
  • (1) Use the C++ interface; (2) Use a try-catch block to catch the exception and see what the problem is. It has a .what() method. Commented Feb 14, 2017 at 9:44
  • thank you. after I use c++ API, error disappears. Commented Feb 14, 2017 at 12:10

1 Answer 1

2

The code seems to be correct, but you are not checking whether the image was really retrieved from the capture device. The most likely problem in your code is that you are not retrieving an image and it is an empty Mat you are trying to convert (which is impossible) and this produces the error.

However, if you are already using C++, why don't you use C++ API?

Sign up to request clarification or add additional context in comments.

1 Comment

I already voted you. but questioner's vote is not displayed(stackoverflow noticed me)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.