Ask Your Question

Runtime error while conversion of color space from RGb to YCbCr

asked 2014-08-15 19:27:46 -0500

Iona gravatar image

updated 2014-08-16 00:29:44 -0500

berak gravatar image


I have Y, Cr, Cb channels which I need to combine together into one RGB image. Here is the code I'm using which is giving me run time error. Any help would be appreciated.

    CvMat finalLayer; //output to store the merged ycbcr image
    cvMerge(y_output, cr_output, cb_output, NULL, &finalLayer); //merging ycbcr
    cv::Mat finalLayer1 = cv::cvarrToMat(&finalLayer); //convert IplImage to cv::Mat
    cv::Mat finalLayer2; //store RGB output image
    cvtColor(finalLayer1, finalLayer2, CV_YCrCb2BGR); //convert from ycbcr to rgb

    namedWindow("Final Image",CV_WINDOW_AUTOSIZE);
    imshow("Final Image",finalLayer2);waitKey(0);
edit retag flag offensive close merge delete


don't use CvMat, but cv::Mat, don't use cvMerge, but cv::merge. in other words, - avoid using functions from the deprecated c-api.

berak gravatar imageberak ( 2014-08-16 00:28:34 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-08-16 00:47:18 -0500

berak gravatar image

try like this:

Mat y,cr,cb; // your ycrcb channels. and no, no IplImages or CvMat, please...

Mat chan[3] = { y,cr,cb };
Mat ycrcb;  // output

merge(chan, 3, ycrcb);

Mat bgr;
cvtColor(ycrcb, bgr, CV_YCrCb2BGR);
edit flag offensive delete link more


This looks easy and understandable. Thank you Berak!

However, I have a question, "y", "cb" and "cr" are values returned from a different function. Hence they are returned as cv::Mat * instead of cv::Mat. I was wondering what would be a suitable way to convert cv::Mat * to cV::Mat?

Iona gravatar imageIona ( 2014-08-16 22:17:32 -0500 )edit

like this? :

Mat* ptr_y = function();  // returns a pointer
Mat y = *ptr_y;           // dereference it
berak gravatar imageberak ( 2014-08-17 01:28:38 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-08-15 19:27:46 -0500

Seen: 417 times

Last updated: Aug 16 '14