Search code examples
opencvyuv

Opencv imshow Y from YUV


I'm trying to extract and display

Y channel from YUV converted image

My code is as follows:

Mat src, src_resized, src_gray;

src = imread("11.jpg", 1);

resize(src, src_resized, cvSize(400, 320));

cvtColor(src_resized, src_resized, cv::COLOR_BGR2RGB);

/*
I've tried both with and without the upper conversion 
(mentioned here as bug 
http://stackoverflow.com/questions/7954416/converting-yuv-into-bgr-or-rgb-in-opencv 
 in an opencv 2.4.* version - mine is 2.4.10 )
*/

cvtColor(src_resized, src_gray, CV_RGB2YUV); //YCrCb

vector<Mat> yuv_planes(3);
split(src_gray,yuv_planes);

Mat g, fin_img;

g = Mat::zeros(Size(src_gray.cols, src_gray.rows),0);

 // same result withg = Mat::zeros(Size(src_gray.cols, src_gray.rows), CV_8UC1);

vector<Mat> channels;

channels.push_back(yuv_planes[0]);
channels.push_back(g);
channels.push_back(g);

merge(channels, fin_img);

imshow("Y ", fin_img);

waitKey(0);

return 0;

As result I was expecting a Gray image showing luminescence.

Instead I get a B/G/R channel image depending on the position of (first/second/third)

channels.push_back(yuv_planes[0]);

as shown here: Result

What am I missing? (I plan to use the luminance to do a sum of rows/columns and extracting the License Plate later using the data obtained)


Solution

  • The problem was displaying the luminescence only in one channel instead of filling all channels with it.

    If anyone else hits the same problem just change

    Mat g, fin_img;
    
    g = Mat::zeros(Size(src_gray.cols, src_gray.rows),0);
    
    vector<Mat> channels;
    
    channels.push_back(yuv_planes[0]);
    channels.push_back(g);
    channels.push_back(g);
    

    to (fill all channels with desired channel)

    Mat  fin_img;
    
    vector<Mat> channels;
    
    channels.push_back(yuv_planes[0]);
    channels.push_back(yuv_planes[0]);
    channels.push_back(yuv_planes[0]);