MATLAB: How to convert RGB to YUV

image processing

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 ?

Best Answer

in this code 128 is added to U and V
cat connects the three matrices R, G, B along the third dimension