I am very new to Matlab. I am trying to convert an RGB image to YUV, and convert it back to RGB. This is my code:
RGB = imread('ist.jpg');
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);
Y = 0.299 * R + 0.587 * G + 0.114 * B;
U = -0.14713 * R - 0.28886 * G + 0.436 * B;
V = 0.615 * R - 0.51499 * G - 0.10001 * B;
R = Y + 1.139834576 * V;
G = Y -.3946460533 * U -.58060 * V;
B = Y + 2.032111938 * U;
RGB = cat(3,R,G,B);
imshow(RGB);
The final image that Matlab shows me is very blue-ish and very different from the initial RGB image. Also when I compare the certain pixels before and after Blue channel values, I get different values then each other. What am I doing wrong.
Also if there is a more efficient and shorter way to let me access an image's Y, U and V values, it would be better.
I would be really thankful for any help of any kind.
I do not have MATLAB access any more so can't test this. However, imread
is most likely returning uint8
data. Do whos
and see what the data type is. If it is uint8
, the RGB->YUV->RGB conversion is clipping. Try this:
RGB = double(imread('ist.jpg')); % convert to double
% RGB->YUV->RGB like you have them in the current code
RGB = cat(3,R,G,B)./255; % since it's a double now, need 0..1 range for imshow.
% Divide 0..255 by 255 to get 0..1.
imshow(RGB);
See this for more discussion of imshow
and data types.