How to convert from RGB to YUV lossless?

조회 수: 27 (최근 30일)
Balkar Singh
Balkar Singh 2020년 5월 17일
댓글: DGM 2022년 4월 24일
I am using rgb2ycbcr and ycbcr2rgb function to convert from RGB to YUV and vice-versa. But by using these function there is loss of data and due to which I am not able to reconstruct my image. Please help.
Thanks

채택된 답변

Asvin Kumar
Asvin Kumar 2020년 5월 20일
Short Answer:
You can avoid data loss in images of type ‘float’. You most likely can’t do it with images of type ‘uint8’ or ‘uint16’ and the like. To convert your image to float types, have a look at im2single and im2double. To convert it back, have a look at im2uint8, im2uint16, etc.
Long answer:
rgb2ycbcr and ycbcr2rgb are essentially linear combinations of the different channels in each representation. Say x1, x2, x3 are the three channels of your RGB image and y1, y2, y3 are the three channels of the equivalent YCbCr image then:
y1 = a0 + a1*x1 + a2*x2 + a3*x3
y2 = b0 + b1*x1 + b2*x2 + b3*x3
y3 = c0 + c1*x1 + c2*x2 + c3*x3
where the ai-s bi-s and ci-s are all some pre-defined decimal (float) constants. There exist similar constants for conversion from YCbCr to RGB too. If your input image is of type ‘int8’, your yi-s will be restricted to be of the same type. This introduces inaccuracies because the linear combination of input channels with decimal (float) coefficients will also be decimal which you are trying to store in an integral container. Internally, these decimal values get rounded off to the nearest integer. That should explain why most of the differences/errors you notice are either 0/1.
More inaccuracies add up when you do the reverse operation by calling ycbcr2rgb. None of this would occur if the input image is of type float because they can hold other intermediate float values. If you tried the same comparison which you’ve mentioned here, your difference would be of the order of 1e-16 which can be attributed to the numerical accuracy of that datatype.
Foot note:
  댓글 수: 5
Balkar Singh
Balkar Singh 2020년 5월 26일
Ok Sir Thank you
DGM
DGM 2022년 4월 24일
This question is likely based not only on the mistaken assumption that YUV and YCbCr are the same, but also the mistaken assumption that YCbCr and YCoCg are the same. If you expect to get a lossless full-cycle conversion out of any luma-chroma transformation, you're probably thinking of YCoCg or YCoCg-R.
This answer demonstrates a lossless full-cycle RGB-YCoCg-RGB conversion and covers some notes on why it works and simple mistakes to avoid (based on only a cursory skim of the spec).
Here's the Dirac spec paper, which outlines conversion routines:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by