1

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)

GodIsAnAstronaut
  • 351
  • 5
  • 17
  • Could you, please, indent you code in a sane way? – Dan Mašek May 05 '16 at 22:05
  • "Instead I get a B/G/R channel image" -- but you are explicitly creating a 3 channel image, where you set one colour channel to luminance and the other 2 to zero. What else than a red, green or blue colour do you expect to see? If you want it in grayscale, either have a single channel image, or set all 3 components of RGB to the same values. – Dan Mašek May 05 '16 at 22:09
  • Well, that was awfully stupid and simple. Thanks alot. – GodIsAnAstronaut May 05 '16 at 22:12

1 Answers1

1

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]);
GodIsAnAstronaut
  • 351
  • 5
  • 17