Jump to content

Y′UV

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Xburge03 (talk | contribs) at 05:31, 28 December 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Example of U-V color plane, Y value = 0.5, represented within RGB color gamut
Animation of all the possible RGB colors in the YUV colorspace. Time is Y, X-axis is U and Y-axis is V. (see image above) The black square delimits the (-0.5,-0.5)-(0.5,0.5) range.
File:YUV components.jpg
An image along with its Y, U, and V components.

The YUV model defines a color space in terms of one luminance and two chrominance components. YUV is used in the PAL system of colour encoding in analog video, which is part of television standards in much of the world.

YUV models human perception of color more closely than the standard RGB model used in computer graphics hardware, but not as closely as the HSL color space and HSV color space.

Y stands for the luminance component (the brightness) and U and V are the chrominance (color) components. The YPbPr color space used in analog component video and its digital child YCbCr used in digital video are more or less derived from it (Cb/Pb and Cr/Pr are deviations from grey on blue-yellow and red-cyan axes whereas U and V are blue-luminance and red-luminance differences), and are sometimes inaccurately called "YUV". The YIQ color space used in the analog NTSC television broadcasting system is related to it, although in a more complex way.

Formulas

YUV signals are created from an original RGB (red, green and blue) source. The weighted values of R, G and B are added together to produce a single Y signal, representing the overall brightness, or luminance, of that spot. The U signal is then created by subtracting the Y from the blue signal of the original RGB, and then scaling; and V by subtracting the Y from the red, and then scaling by a different factor. This can be accomplished easily with analog circuitry.

YUV (analog version of YUV) from RGB

Y = 0.299 * R + 0.587 * G + 0.114 * B
U = 0.436 * (B - Y) / (1 - 0.114)
V = 0.615 * (R - Y) / (1 - 0.299)

or

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 formulae approximate the conversion between the RGB color space and YUV.

From RGB to YUV:

From YUV to RGB:

Or, using a matrix representation:

Two things to note regarding the RGB transformation matrix:

  • The top row is identical to that of the YIQ color space
  • If then . In other words, the top row coefficients sum to unity and the last two rows sum to zero.

Note: this formula uses the more traditional model of YUV, which is used for analog PAL equipment; digital PAL and digital NTSC HDTV don't use YUV but YCbCr.

As most actual uses of RGB->YUV are in integer math, it's often convenient to use a fixed-point approximation.

Y := min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235)
U := min(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240)
V := min(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240)

Luminance/chrominance systems in general

The primary advantages of luminance/chrominance systems such as YUV, and its relatives YIQ and YDbDr, are that they remain compatible with black and white analog television (largely due to the work of Georges Valensi). The Y channel saves nearly all the data recorded by black and white cameras, so it produces a signal suitable for reception on old monochrome displays. In this case, the U and V are simply discarded. If displaying color, all three channels are used, and the original RGB information can be decoded.

Another advantage of YUV is that some of the information can be discarded in order to reduce bandwidth. The human eye has fairly little color sensitivity: the accuracy of the brightness information of the luminance channel has far more impact on the image discerned than that of the other two. Understanding this human shortcoming, standards such as NTSC reduce the amount of data consumed by the chrominance channels considerably, leaving the eye to extrapolate much of the color. NTSC saves only 11% of the original blue and 30% of the red. The green information is usually preserved in the Y channel. Therefore, the resulting U and V signals can be substantially compressed.

However, this colorspace conversion is lossy. When the NTSC standard was created in the 1950s this was not a real concern since the quality of the image was limited by the monitor equipment, not the compressed signal being received. However today's modern television is capable of displaying more information than is contained in these lossy signals. To keep pace with the abilities of new technology, attempts have been made to preserve more of the YUV signal while recording images, such as S-Video on VCRs.

Instead of YUV, YCbCr was used as the standard format for (digital) common video compression algorithms such as MPEG-2. Digital television and DVDs preserve their compressed video streams in the MPEG-2 format, which uses a full YCbCr color space. The professional CCIR 601 uncompressed digital video format also uses YCbCr, primarily for compatibility with previous analog video standards. This stream can be easily mixed into any output format needed.

YUV is not an absolute color space. It is a way of encoding RGB information, and the actual color displayed depends on the actual RGB colorants used to display the signal. Therefore a value expressed as YUV is only predictable if standard RGB colorants are used (i.e. a fixed set of primary chromaticities, or particular set of red, green, and blue).

Confusion with Y'CbCr

Y'UV is often and mistakenly used as the term for Y'CbCr. However, they are different formats. Y'UV is an analog system with scale factors different than the digital Y'CbCr system.[1]

Types of sampling

To get a digital signal, YUV images can be sampled in several different ways; see chroma subsampling.

Converting from YUV to RGB

function RGB* YUV444toRGB888(Y, U, V); converts YUV format to simple RGB format and could be implemented using floating point arithmetic as:

  • YUV444
Y = 0.299R + 0.587G + 0.114B 
U = − 0.147R − 0.289G + 0.436B 
V = 0.615R − 0.515G − 0.100B

But floating point arithmetic is much slower than using [fixed point] arithmetic:

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)
  • YUV422

INPUT: Read 4 bytes of YUV (u, y1, v, y2 ) OUTPUT: Writes 6 bytes of RGB (R, G, B, R, G, B)

u  = yuv[0];
y1 = yuv[1];
v  = yuv[2];
y2 = yuv[3];

Using this information it could be parsed as regular YUV444 format to get 2 RGB pixels info:

rgb1 = YUV444toRGB888(y1, u, v);
rgb2 = YUV444toRGB888(y2, u, v);
  • YUV411
// Extract yuv components
u  = yuv[0];
y1 = yuv[1];
y2 = yuv[2];
v  = yuv[3];
y3 = yuv[4];
y4 = yuv[5];
rgb1 = YUV444toRGB888(y1, u, v);
rgb2 = YUV444toRGB888(y2, u, v);
rgb3 = YUV444toRGB888(y3, u, v);
rgb4 = YUV444toRGB888(y4, u, v);

So the result is we are getting 4 RGB pixels values (4*3 bytes) from 6 bytes. This means reducing size of transferred data to half and with quite good loss of quality.

  • YUV420p (and YV12)

YUV420p is a planar format, meaning that the Y, U, and V values are grouped together instead of interspersed. The reason for this is that by grouping the U and V values together, the image becomes much more compressible. When given an array of an image in the YUV420p format, all the Y values come first, followed by all the U values, followed finally by all the V values.

The YV12 format is essentially the same as YUV420p, but it has the U and V data reversed: the Y values are followed by the V values, with the U values last. As long as care is taken to extract U and V values from the proper locations, both YUV420p and YV12 can be processed using the same algorithm.

As with most YUV formats, there are as many Y values as there are pixels. Where X equals the height multiplied by the width, the first X indices in the array are Y values that correspond to each individual pixel. However, there are only one fourth as many U and V values. The U and V values correspond to each 2 by 2 block of the image, meaning each U and V entry applies to four pixels. After the Y values, the next X/4 indices are the U values for each 2 by 2 block, and the next X/4 indices after that are the Y values that also apply to each 2 by 2 block.

Translating YUV420p to RGB is a rather involved process compared to the previous formats. Taking a 16 by 16 image for example, getting the RGB values for pixel (5, 7) where (0, 0) is the top left pixel would be done as follows. The character "/" implies integer division, meaning that if there is a remainder, it will be discarded.

Height = 16;
Width = 16;
YArraySize = Height * Width = 256;
Y = Array[7 * Width + 5];
U = Array[(7/2) * (Width/2) + 5/2 + YArraySize];
V = Array[(7/2) * (Width/2) + 5/2 + YArraySize + YArraySize/4];

RGB = YUV444toRGB888(Y, U, V);

File:Yuv420.png
As is shown on image above, YUV420 is composed by Y part, U part and V part. For single U and single V are relative to 2x2 matrix of Y (distinguished by colors). If you read it from some device as byte stream they all are represented line by line. So first Y could by found on array[0] position, first U on array[x*y] = array[6*4] = array[24], first V on array[x*y+x*y/4] = array[24+6] = array[30].

See also