Ask Your Question
0

Runtime error while conversion of color space from RGb to YCbCr

asked Aug 16 '14

Iona gravatar image

updated Aug 16 '14

berak gravatar image

Hello,

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

    //display
    namedWindow("Final Image",CV_WINDOW_AUTOSIZE);
    imshow("Final Image",finalLayer2);waitKey(0);
Preview: (hide)

Comments

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 (Aug 16 '14)edit

1 answer

Sort by » oldest newest most voted
2

answered Aug 16 '14

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);
Preview: (hide)

Comments

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 (Aug 17 '14)edit

like this? :

Mat* ptr_y = function();  // returns a pointer
Mat y = *ptr_y;           // dereference it
berak gravatar imageberak (Aug 17 '14)edit

Question Tools

Stats

Asked: Aug 16 '14

Seen: 608 times

Last updated: Aug 16 '14