how to convert RGB to YUV ?
조회 수: 16 (최근 30일)
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
DGM
2021년 11월 4일
편집: DGM
2021년 11월 4일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!