Вы находитесь на странице: 1из 3

Converting Between YUV and RGB

It is frequently necessary to convert between YUV pixel formats (used by the JPEG and MPEG compression methods) and RGB format (used by many hardware manufacturers.) The following formulas show how to compute a pixel's value in one format from the pixel value in the other format. YUV format allows for higher compression rates without a proportionately high loss of data, as the U and V portions can be highly compressed and computed from the non- or lowly-compressed Y portion. Computer RGB888, or full-scale RGB, uses 8 bits each for the red, green, and blue channels. Black is represented by R = G = B = 0, and white is represented by R = G = B = 255. The 4:4:4 YUV format uses 8 bits each for the Y, U, and V channels.

Converting RGB888 to YUV


The following formulas define the conversion from RGB to YUV:

Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16 U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128 V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128
These formulas produce 8-bit results using coefficients that require no more than 8 bits of (unsigned) precision. Intermediate results require up to 16 bits of precision.

Converting 8-bit YUV to RGB888


The following coefficients are used in conversion process:

C = Y - 16 D = U - 128 E = V - 128
Using the previous coefficients and noting that clip() denotes clipping a value to the range of 0 to 255, the following formulas provide the conversion from YUV to RGB:

R = clip(( 298 * C + 409 * E + 128) >> 8) G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8) B = clip(( 298 * C + 516 * D + 128) >> 8)
These formulas use some coefficients that require more than 8 bits of precision to produce each 8-bit result, and intermediate results require more than 16 bits of precision. Note All units range from 0 (zero) to 1.0 (one). In DirectDraw, they range from 0 to 255. Overflow and underflow can (and does) occur, and the results must be saturated. To convert 4:2:0 or 4:2:2 YUV to RGB, convert the YUV data to 4:4:4 YUV, and then convert from 4:4:4 YUV to RGB.

RGB to YUV Conversion


Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16 (0.439 * R) - (0.368 * G) - (0.071 * B) + 128 Cr = V =

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

YUV to RGB Conversion


B = 1.164(Y - 16) + 2.018(U - 128) G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) R = 1.164(Y - 16) + 1.596(V - 128) In both these cases, you have to clamp the output values to keep them in the [0-255] range. Rumour has it that the valid range is actually a subset of [0-255] (I've seen an RGB range of [16-235] mentioned) but clamping the values into [0-255] seems to produce acceptable results to me.

How it Works
Its a simple arithmetic to convert RGB to YUV. The formula is based on the relative contributions that red, green, and blue make to the luminance and chrominance factors. There are several different formulas in use depending on the target monitor. Here I am chooses ITU-R version formula RGB to YUV Y = 0.299 * R + 0.587 * G + 0.114 * B U = -0.1687 * R 0.3313* G + 0.5 * B + V = 0.5 * R 0.4187 * G 0.813 * B + 128 128

YUV to RGB R = Y+ 0 * U + 1.13983 * V G = Y+ -0.39465 * U + -0.58060 * V B = Y+ -0.03211 * U + 0 * V

Now we have the YUV data and its time to apply Chroma sampling, all about Chroma sampling is shown in the below image. The available chroma samplings are given below

Chroma Sampling 4:4:4 4:4: 0 4:2:2 4:2: 0 4:1:1 4:1: 0

Output Y size corresponding to a 4x4 block Y - for each pixel, Cr and Cb-For each pixel Y - for each pixel, Cr and Cr For each pixel of alternate rows Y - for each pixel, Cr and Cr For each pixel of alternate columns Y - for each pixel, Cr and Cr For each pixel of alternate columns and rows Y - for each pixel, Cr and Cr For each pixel of every 4th columns Y - for each pixel, Cr and Cr For each pixel of every 4th columns and rows

Вам также может понравиться