Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Clarification needed for RGB -> YCbCr -> RGB conversion

Status
Not open for further replies.

anandkumarcr

Junior Member level 1
Joined
Sep 9, 2011
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,437
HI,

I needed a clarification in the conversion of RGB -> YCbCr and resulting YCbCr back to RGB.

The following is the formula I am using to convert RGB -> YCbCr.

Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16..................(1)

Cb = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128..................(2)

Cr = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128....................(3)

And to get back the original RGB from YCbCr I am using the following :-

R = 1.164(Y - 16) + 1.596(Cr - 128)..........................(4)

G = 1.164(Y - 16) - 0.813(Cr - 128) - 0.391(Cb - 128).................(5)

B = 1.164(Y - 16) + 2.018(Cb - 128)....................(6)


However I am getting the following outputs for the following inputs:-

Original R = 187
Original G = 146
Original B = 69

From equations (1),(2),(3) above

Y = 144
Cb = 37
Cr = 51

And on reverting back the YCbCr to obtain the original RGB according to equations (4),(5),(6) -I am getting

Obtained R = 185
Obtained G = 165
Obtained B = -34

Though the obtained RGB is correct according to the calculations, it does not make any sense that it is not equal to the original RGB input and even worse - obtained RGB attains negative values at times.

Could anybody please point out any mistakes or any steps that need to be corrected ?

Thank you
 

Hi;
It seems to me your calculation is somehow wrong. Otherwise equations should be OK.
Could you please share your code? It is Matlab or C?
 

Hi emresel,

I suppose I have after all made a mistake in equation (2) due to oversight.
Cb = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128..................(2)
The one marked in red, I had taken it as 0.418 instead of 0.148. So now after rectifying it I seem to be getting the right result.

However I had another question, what is the acceptable range of error in RGB->YCbCr ->RGB conversion. I seem to be getting about a difference of 2-3 units of intensity difference on an average.

Is the error measured in terms of Quantization Error ? Or is there any other metric ?

Thank you.
 

Hi;
If you substitute Y and Cb equations (1 and 3) in equation 4 (reverse R calculation) you can obtain R=R.
(I mean on paper there shouldn’t be any difference like that).
If you consider in matrix form [YCbCr]=A*[RGB], and [RGB]=B*[YCbCr], here B is inv(A). See it.

So may be some kind of rounding error appears in your code.
i.e. check variable types, select them as floating not integer.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top