how to convert RGB to YUV ?

42 views (last 30 days)
namita chandrakar
namita chandrakar on 2 Dec 2014
Edited: DGM on 4 Nov 2021
hello sir, I am trying to convert an RGB image to YUV, and convert it back to RGB. This is my code:
%host image
T=imread('......');
M=imresize(T,[512 512]);
I=im2double(M);
figure(1),imshow(I); title('Host Image');
%separate host image into R G & B components
R=I(:,:,1); figure(2),imshow (R);
G=I(:,:,2); figure(3),imshow(G);
B=I(:,:,3); figure(4),imshow(B);
Y = 0.299 * R + 0.587 * G + 0.114 * B;
figure(5),imshow(Y);
U = -0.14713 * R - 0.28886 * G + 0.436 * B;
figure(6),imshow(U);
V = 0.615 * R - 0.51499 * G - 0.10001 * B;
figure(7),imshow(V);
YUV = cat(3,Y,U,V);
figure(8),imshow(YUV);
R = Y + 1.139834576 * V;
G = Y -.3946460533 * U -.58060 * V;
B = Y + 2.032111938 * U;
RGB = cat(3,R,G,B);
figure(9),imshow(RGB);
The U component of image that Matlab shows me is very dark black and very different from the U component of image i have seen in some paper. what is the correct way of converting into YUV color space ? what is meaning of cat ?
  1 Comment
Fonkeng Nkah Peter
Fonkeng Nkah Peter on 25 Mar 2019
Thanks a lot sir. I used this to get my desire result

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 2 Dec 2014
Edited: Thorsten on 2 Dec 2014
in this code 128 is added to U and V
cat connects the three matrices R, G, B along the third dimension
  1 Comment
namita chandrakar
namita chandrakar on 2 Dec 2014
hello sir, can you please provide me code for RGB to YIQ color space and vice versa .

Sign in to comment.

More Answers (1)

DGM
DGM on 4 Nov 2021
Edited: DGM on 4 Nov 2021
Tools like imshow() are not intended to view anything other than simple intensity or RGB data. They assume certain conventions for how the image data will be presented. The problem here is that for a floating point image, imshow() expects black to be 0 and white to be 1.
Looking at the matrix or at the projection of sRGB in YUV shown above, it's apparent that U and V are centered on 0, and span into negative values. Accordingly, imshow() will render U and V content as mostly black. The same is true for other luma-chroma models and things like CIELab or CIELuv.
You might manually offset the values as the above link suggests, but keep track of the numeric class and the data range. One common approach is to convert the image to uint8-scaled floating point, transform, and then offset by 128. While this works when the axis limits are [0.5 0.5] as they are for YPbPr/YCbCr, the limits for YUV are wider [0.436 0.615]. Blindly offsetting by half the range of an integer class would result in V being truncated.
Since your workflow is normalized floating-point, there's no need to change the data into some nonstandard format. If you want to simply view images with content spanning nonstandard ranges, you can either explicitly or implicitly specify normalization limits in the call to imshow():
imshow(U,[]) % implicitly normalize U to the extent of its content
imshow(U,[-0.436 0.436]) % explicitly normalize U to the extent of its nominal range
Similar can be done for V.
Note that using implicit normalization limits is easy and may be fine for quick viewing, but there's no guarantee that different images will be centered or scaled consistently.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!