Thursday, June 25, 2009

Back To Basics : YUV to RGB conversion (Color Space)

I know there are like hundreds of books and tutorials available which explain color space. But I also know that neither of those tutorials stop a new author to explain it again in different manner, more explanatory in more easy way, so nor Me.

I remember my first interview in video domain. I can still hear that voice.
"Ok tell me what will be the color if Y 128 and Cb and Cr are 0", I said "Gray"
"if Y is 0 and Cb and Cr are at 128 then color is ", I was not sure what to say....
"Ok all are in 128, so what will be color", I said "White" (It was my guess)

I must say that interviewer taught me a lot, At that time I was mostly working on understanding of H.264 standard. That was a hell work for me. So I was learning how to implement those algorithms but for basics of video thing I was just beginner.

ok, lets start the tutorial now.......

Color models are conversion formats from one model to another. There are RGB, YCbCr, YUV etc. RGB in formations comes though the capturing devices like cameras or scanners. RGB color space is generally used in computer graphics. And combination of these three colors will generate other colors like white, black, yellow, cyan etc.But in real world processing RGB model is not the efficient one. If each color represents with 8 bits/pixel than for RGB we need 24 bits/pixels. If we wish to modify intensity or color of one pixel, we have to read all three components, process it and then store back, NOT good in real time with processing wise as well as memory requirement wise. To solve that problem there are other color space/model which store the pixel information in intensity and color format, like using luma and two color difference which can be converted to RGB or from RGB to that formats. Most common is YUV format.

The basic equations for YUV are :
Y = 0.299R + 0.587G + 0.114B
U = 0.492 (B-Y)
U = 0.887 (R-Y)

and YUV to RGB are :
R = Y + 1.14V
G = Y - 0.395U - 0.581V
B = Y + 2.032U


RGB ranges 0 to 255 while Y has a range of 0 to 255, U ranges 0 to +/- 112 and V ranges 0 to +/-157. But these equations are generally scaled for better implementation in NTSC or PAL digital codec. And for 8 bit data YUV and RGB data are saturated between 0 to 255.

YCbCr is scaled and offset version of YUV model, where Y ranges 16-235, Cb and Cr ranges 16-240. But actually all saturated to 0-255 levels. Here are the equations:

Y = 0.257R + 0.504G + 0.098B + 16
Cb = -0.148R - 0.291G + 0.439B + 128
Cr = 0.439R - 0.368G - 0.071B +128

and YCbCr to RGB conversion equations are :
R = 1.164 (Y-16) + 1.596 (Cr-128)
G = 1.164 (Y-16) - 0.813 (Cr-128) - 0.391 (Cb - 128)
B = 1.164 (Y-16) + 2.018 (Cb- 128)

As per my interview Q & A I have to consider here to this YCbCr to RGB conversion. If you analyze properly YUV to RGB and YCbCR to RGB equations are almost the same. I simplified the equation for better to remember way for human being like me and specially should not be used in computer for implementation. (computer dont give interview ;))

Here is my approximate way for YCbCr to RGB conversion:

R = Y+ 1.5 (Cr-128)
G = Y - 0.8(Cr-128)
B = Y+ 2.0 (Cb-128)

Here is the table for color we will get with different value of YCbCr.

You dont have remember this table, just use above approximate equation and RGB Color Cube (shown below), you will have the answer in sec.



By the way, after these tutorial, here are my answers for previous questions.
1) what will be the color if Y 128 and Cb and Cr are 0 ? GREEN
2) If Y is 0 and Cb and Cr are at 128, what will be the color ? BLACK
3) All are in 128, so color will be ? WHITE

And this time 100% sure... :)

No comments:

Post a Comment