# color conversion

• 09-25-2002
master5001
color conversion
Hey guys, I haven't been on the boards in a while. Anyway, I have been looking for a specific algorithm for converting RGB colors to YUV. Here is the deal I have actually found many examples of how to do this (both ways), but the examples always have mystery numbers. For example:

Code:

```Y  = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16.0 U = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128.0 V = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128.0```
As you can see there are numbers such as 0.257. Does anyone know how to derive these numbers? Or if in no one does know, then maybe you can help me with what I'm trying to do. Do you know how to calculate YUV for a 16-bit RGB value?
• 09-26-2002
confuted
I think it might have something to do with the phosphors of the pixels in the monitor...but I don't remember, and if someone more enlightened posts something that sounds better, go with that (: Could you give me a refresher on what YUV stands for?
• 09-27-2002
master5001
As far as I know The U and V don't actually mean anything. This is just a common way of expressing YCbCr (composite video). Y is luminance, Cb is the blue component and Cr is the red component.

Your answer, sounds like you may be on to something. Perhaps those magic numbers could be a constant of how much of that perticular color component that is visible by the eye. I don't know though.
• 09-27-2002
master5001
Ah, here is someone who looks like they know what they are talking about :)
• 09-27-2002
Magos
Interesting! I've always wondered how JPEGs could be so small.
• 09-27-2002
confuted
that link wasn't working when I checked it, so I don't know what was on that page, but I found this tonight. I don't know how much help it will be to you, though.

Quote:

1) The afine transformation in colour space : [R G B] -> [Y Cb Cr]
---------------------------------------------------------------------

(It is defined in the CCIR Recommendation 601)

(R,G,B are 8-bit unsigned values)

| Y | | 0.299 0.587 0.114 | | R | | 0 |
| Cb | = |- 0.1687 - 0.3313 0.5 | * | G | + |128|
| Cr | | 0.5 - 0.4187 - 0.0813| | B | |128|

The new value Y = 0.299*R + 0.587*G + 0.114*B is called the luminance.
It is the value used by the monochrome monitors to represent an RGB colour.
Physiologically, it represents the intensity of an RGB colour perceived by
the eye.
You see that the formula for Y it's like a weighted-filter with different weights
for each spectral component: the eye is most sensitive to the Green component
then it follows the Red component and the last is the Blue component.

The values Cb = - 0.1687*R - 0.3313*G + 0.5 *B + 128
Cr = 0.5 *R - 0.4187*G - 0.0813*B + 128
are called the chromimance values and represent 2 coordinates in a system
which measures the nuance and saturation of the colour ([Approximately], these
values indicate how much blue and how much red is in that colour).
These 2 coordinates are called shortly the chrominance.

[Y,Cb,Cr] to [R,G,B] Conversion (The inverse of the previous transform)
--------------------------------
RGB can be computed directly from YCbCr ( 8-bit unsigned values) as follows:

R = Y + 1.402 *(Cr-128)
G = Y - 0.34414*(Cb-128) - 0.71414*(Cr-128)
B = Y + 1.772 *(Cb-128)
Here's the .zip file that came out of...
• 09-29-2002
master5001
Ah so I was on the right track with what I said. That is the most complete document about this subject I've seen so far. Thanks a bunch. I can always derive the 16-bit coefficients algebraically. Or even better yet I can still with how I am doing it now. Since my input images are 24-bit I just use the 24-bit rgb to yuv conversion do my compression and quantization routines and then take take the converted rgb and change that to 16-bit color. (I am doubtful that sharing this will be helpful to anyone but you never know).