Ask Your Question
0

Crash in cvtColor

asked 2013-10-23 21:46:35 -0600

kalpana gravatar image

updated 2013-10-23 23:43:54 -0600

Haris gravatar image

Hi,

I m trying to apply opencv algo on a yuv framebuffer.

I m doing something like this My framebuffer input in of type unsigned char.

Mat frame(640,480,CV_8UC1,0.0);
memcpy(frame.data,yuvframebuffer,bufferLen);
Mat gray(640,480,CV_8UC1,0.0);  
cvtColor(frame, gray,CV_YUV2RGB_YV12);

I m getting "CV exception in memory location [..] "

Please let me know what I m doing wrong here..

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-10-24 01:21:58 -0600

Siegfried gravatar image

updated 2013-10-24 01:27:49 -0600

Hi, the first thing is that your image type for cv::Mat frame and cv::Mat gray is wrong. YUV images have 3 channels and also RGB images. You initialize the images with 1 channel (CV_8UC1) and then you try to use a conversion method for 3 channels CV_YUV2RGB.

Mat frame(640,480,CV_8UC3,0.0); // create an image with 640x480 pixel, 3 channels and 8bit per channel
memcpy(frame.data,yuvframebuffer,bufferLen);
Mat gray(640,480,CV_8UC3,0.0);  
cvtColor(frame, gray,CV_YUV2RGB_YV12);

For future questions, please add the error message to your post. This is very helpful for people who want to help you.

edit flag offensive delete link more

Comments

Thanks for your reply... I tried the above and it didnt help.. Getting the below exception : "Unhandled exception in xxx.exe : Microsoft C++ exception: cv::Exception at memory location 0x055db900.."

kalpana gravatar imagekalpana ( 2013-10-24 07:29:38 -0600 )edit

Ok, then we should go one step back and start with debugging the code. Run your code in debug-mode and report in which line the error occur. The second thing what you can do is to check if number of channels from the frame and gray image is exactly the same as required by the cvtColor function. The source code from cvtColor is located here http://code.opencv.org/projects/opencv/repository/revisions/master/entry/modules/imgproc/src/color.cpp#L2696 .

Siegfried gravatar imageSiegfried ( 2013-10-24 10:59:53 -0600 )edit
  • both of you got rows & cols wrong , should be Mat frame(480,640, ... );
  • use either Mat::zeros(480,640,CV_8UC3); or Mat(480,640,CV_8UC3,Scalar::all(0)); a single 0.0 will only initialize the 1st channel
  • you don't need to initialize OutputArrays, just pass an empty Mat for dst to cvtColor()
berak gravatar imageberak ( 2013-10-24 13:08:19 -0600 )edit

Thanks a lot for your reply ..It worked :-)

kalpana gravatar imagekalpana ( 2013-10-24 13:27:39 -0600 )edit

so, just the size was the wrong way ?

berak gravatar imageberak ( 2013-10-24 13:41:25 -0600 )edit

yes...interchanging width and height caused the crash

kalpana gravatar imagekalpana ( 2013-10-24 23:51:14 -0600 )edit

Thanks to berak for the solution. To prevent that other user searching for this topic run into the wrong way. I want to withdraw my answer. So can you please add your solution to an separate answer? Then I can delete my answer.

Siegfried gravatar imageSiegfried ( 2013-10-25 01:12:19 -0600 )edit

The crash is gone but i didnt notice that gray MAT is empty... and my channel is 16 bit yuy2

I made the below changes Mat frame(360,640,CV_16UC3,Scalar::all(0));

memcpy(frame.data,m_pBufferedData,bufferLen);

cvtColor(frame, gray,CV_YUV2GRAY_YUY2 );

I m seeing the crash still... can anybody pls help?

kalpana gravatar imagekalpana ( 2013-10-25 05:00:06 -0600 )edit

hmm, both CV_YUV2RGB_YUY2 and CV_YUV2GRAY_YUY2 assume a 2 channel 8bit src img. (are you sure you picked the right flag ?)

why not simply CV_YUV2RGB ? (that would work with the current setup)

again, please append your error messages here, else it's pretty hard to help you !

berak gravatar imageberak ( 2013-10-25 05:26:58 -0600 )edit

yes, but you need 2 steps ( and why didn't you say that in the 1st place ? )

  1. Mat rgb; cvtColor(yuv, rgb,CV_YUV2RGB);
  2. Mat gray; cvtColor(rgb,gray, CV_RGB2GRAY);
berak gravatar imageberak ( 2013-10-25 08:07:09 -0600 )edit

Question Tools

Stats

Asked: 2013-10-23 21:46:35 -0600

Seen: 4,980 times

Last updated: Oct 24 '13