Introduction to RGB and YUV Multimedia Programming Basics _ Basics

Source: Internet
Author: User
Tags color representation

RGB and YUV Multimedia programming

First, the concept

1. What is RGB?

The method of encoding a color is collectively called " Color space " or " gamut ." In the simplest terms, the "color space" of any color in the world can be defined as a fixed number or variable. RGB (red, green, blue) is just one of many color spaces. With this coding method, each color can be represented by three variables-red green and the intensity of the blue. RGB is one of the most common scenarios for recording and displaying color images.

2. What is YUV?

YUV is a color coding method (belonging to PAL) used by European television systems and is the color space used by the PAL and SECAM analog color TV format.

In the modern color TV system, usually using a three-tube color camera or color CCD camera for image, and then the color image signal obtained by color separation, amplification and correction after the RGB, and then through the matrix transformation circuit to get the brightness signal Y and two color difference signal b-y (that is, u), r-y (ie v), At last the transmitter encodes the brightness and chromatic aberration by three signals, and sends out the same channel. This method of color representation is called YUV color space representation.

This shows that RGB and YUV belong to the color space (or "color space"), if not clear the concept of color space.

Two, the advantages and disadvantages of RGB and YUV

1.RGB lack of good compatibility with early Black-and-white display systems. Therefore, many electronic appliances manufacturers commonly adopted the practice is to convert RGB to YUV color space to maintain compatibility, and then change the RGB format as needed to display color graphics on the computer monitor.

2.YUV is mainly used to optimize the transmission of color video signal, so that it is backward compatible with old Black-and-white. Compared with the RGB video signal transmission, its biggest advantage is that only a very small amount of bandwidth (RGB requires three independent video signal transmission simultaneously).

3. The importance of using YUV color space is that its brightness signal y and chroma signal u, v are separated. If only the Y signal component does not have a U or v component, then the image represented by this is a black-and-white grayscale image. Color TV using YUV space is precisely to use the brightness signal y to solve the color TV and Black-and-white TV compatibility problem, so that black-and-whites can also receive color TV signal.

Three, YUV and RGB realization principle

1.RGB is designed from the principle of color luminescence , popular point that its color mixing mode is like red, green, blue three lights, when their light overlapping, color mix, and brightness is equal to the sum of both brightness, the more mixed brightness, that is, additive mixing.

Red, green, blue three lamp superposition situation, center three color brightest superposition area is white, additive mixing characteristic: the superposition is brighter.

Red, green, blue three color channels each color is divided into 256-order brightness, at 0 o'clock "lamp" weakest-is turned off, and in 255 "lamp" brightest. When the three-color gray degree value of the same time, the gray color of different gray values, that is, three-color gray is 0 o'clock, is the darkest black tone, the three-color gray is 255, is the brightest white hue.

RGB colors are called additive color because you can produce white by adding R, G, and B together (that is, all light bounces back to the eye). The additive is used for lighting, television and computer monitors. For example, the display produces color by emitting light from a red, green, and blue phosphor. Most of the visible spectra can be expressed as red, green, Blue (RGB) Three colors in different proportions and intensity of the mixture. If these colours overlap, they produce cyan, magenta, and yellow.

2. In YUV, "Y" denotes brightness (luminance or Luma), which is the gray-scale value, whereas "U" and "V" represent chroma (chrominance or Chroma), which describes the color and saturation of the image, Used to specify the color of the pixel. "Brightness" is created by an RGB input signal, which is superimposed on a specific part of the RGB signal. "Chroma" defines two aspects of the color-hue and saturation, respectively, expressed in CR and CB. Among them, CR reflects the difference between the red part of RGB input signal and the brightness value of RGB signal. And the CB reflects the RGB input signal blue part and RGB signal brightness value difference.

Four, RGB and YUV format

1.RGB format

① Web page format

②rgb555

③rgb565

④rgb24

⑤rgb32

2.YUV format

The YUV format usually has two main categories: the package (packed) format (planar) format. The former holds the YUV component in the same array, usually several neighboring pixels form a macro pixel (Macro-pixel), and the latter uses three arrays to store the YUV three components, just like a three-dimensional plane.

The ①yuy2 (and YUYV) format retains the Y component for each pixel, and the UV component is sampled once per two pixels in the horizontal direction. A macro pixel is 4 bytes, which actually represents 2 pixels. (4:2:2 actually means a macro pixel with 2 y components, a U component, and a V component.) The YUV components in the image data are arranged in the following order:

Y0 U0 Y1 V0 Y2 U2 ...

The ②yvyu format is similar to YUY2, except that the YUV components in the image data are arranged in a different order:

Y0 V0 Y1 U0 Y2 V2 ...

The ③uyvy format is similar to YUY2, except that the YUV components in the image data are arranged in a different order:

U0 Y0 V0 Y1 U2 Y2 ...

The ④AYUV format has an alpha channel and extracts YUV components for each pixel, and the image Data format is as follows:

A0 Y0 U0 V0 A1 Y1 ...

The ⑤y41p (and Y411) format retains the Y component for each pixel, and the UV component is sampled once per 4 pixels in the horizontal direction. A macro pixel is 12 bytes, which actually represents 8 pixels. The YUV components in the image data are arranged in the following order:

U0 Y0 V0 Y1 U4 Y2 V4 Y3 Y4 Y5 Y6 ...

The ⑥y211 format is sampled once per 2 pixels in the horizontal direction, while the UV component is sampled every 4 pixels. A macro pixel is 4 bytes, which actually represents 4 pixels. The YUV components in the image data are arranged in the following order:

Y0 U0 Y2 V0 Y4 U4 ...

The ⑦YVU9 format extracts y components for each pixel, and when the UV component is extracted, the image is first divided into several 4 x 4 macro blocks, and each macro block extracts a U component and a V component. When the image data is stored, the first is an array of y components of the entire image, followed by the U-component array, and the V-component array. The IF09 format is similar to YVU9.

The ⑧IYUV format extracts y components for each pixel, and when the UV component is extracted, the image is first divided into several 2 x 2 macro blocks, and each macro block extracts a U component and a V component. The YV12 format is similar to IYUV.

The ⑨yuv411 and YUV420 formats are mostly found in DV data, which is used in NTSC and the latter for PAL system. YUV411 extracts y components for each pixel, and the UV component is sampled once per 4 pixels in the horizontal direction. YUV420 is not a V component sampling of 0, but compared with YUV411, in the horizontal direction to improve the sampling frequency of chromatic aberration, in the vertical direction u/v interval to reduce the half color sampling.

3. In DirectShow, the common RGB formats are RGB1, RGB4, RGB8, RGB565, RGB555, RGB24, RGB32, ARGB32, and so on; the common YUV formats are YUY2, YUYV, Yvyu, Uyvy, AYUV, Y41P, Y411, Y211, IF09, IYUV, YV12, YVU9, YUV411, YUV420, etc.

Five, RGB and YUV conversion

For digital video, the conversion from RGB to two primary YUV is defined. Both conversions are based on a specification called Itu-r recommendation bt.709.

The first conversion is the earlier YUV format defined in bt.709 for 50-hz. It is the same as the relationship specified in Itu-r recommendation bt.601, Itu-r recommendation bt.601 is also known as its old name CCIR 601. This format should be considered the preferred YUV format for standard definition of TV resolution (720 x 576) and lower resolution video. Its characteristics are defined by the following two constants, Kr and Kb values:

Kr = 0.299

Kb = 0.114

The second conversion to the newer YUV format defined for 60-hz in bt.709 should be considered the preferred format for video resolution higher than SDTV. Its characteristics are defined by the following two different constant values:

Kr = 0.2126

Kb = 0.0722

The definition of conversion from RGB to YUV begins with the following: L = Kr * R + Kb * B + (1–KR–KB) * g Then, the YUV value is obtained in the following ways:

Y = Floor (2^ (M-8) * (219* (l–z)/S + 16) + 0.5)

U = CLIP3 (0, 2^m-1, Floor (2^ (M-8) * (112* (B-L)/((1-KB) *s) + 128) + 0.5)

V = CLIP3 (0, 2^m-1, Floor (2^ (M-8) * (112* (R-L)/((1-KR) *s) + 128) + 0.5)

where M is the number of digits for each YUV sample (M >= 8).

Z is a black level variable. For a computer rgb,z equals 0. For studio video rgb,z equals 16*2, where n is the number of bits per RGB sample (n >= 8). S is a scaling variable. For a computer rgb,s equals 255. For studio video rgb,s equals 219*2.

function floor (x) returns the largest integer greater than or equal to X. The function clip3 (x, Y, z) is defined as follows:

CLIP3 (x, y, Z) = ((Z < x)? x: ((z > Y)? y:z)) The Y sample represents the brightness, and the U and V examples represent the color biases that are biased toward blue and red, respectively. The nominal range of Y is 16*2 to 235*2. Black is expressed as 16*2, and White is expressed as 235*2. The nominal range of U and V is 16*2 to 240*2, and the value 128*2 denotes neutral chroma. However, the actual value may not be within these ranges.

For the input data in the RGB form of Studio video, you must make a clip operation to keep the U and V values within 0 to 2m-1 range. If you enter computer RGB, you do not need a clip operation because the conversion formula does not generate a value that is outside this range.

These are exact formulas that have no approximate value.

Sampling format for YUV

The main sampling formats for YUV are YCbCr 4:2:0, YCbCr 4:2:2, YCbCr 4:1:1, and YCbCr 4:4:4.

Which YCbCr 4:1:1 more commonly used, the meaning is: each point to save a 8bit brightness value (that is, Y value), each 2x2 point to save a Cr and CB value, the image in the naked eye of the feeling will not be too big changes. Therefore, the original use of RGB (R,g,b are 8bit unsigned) model, a point needs 8x3=24 bits (the first diagram below), (after the full sample, YUV still each accounted for 8bit). After sampling by 4:1:1, and now on average only need 8+ (8/4) + (8/4) =12bits (4 dots, 8*4 (Y) +8 (U) +8 (V) =48bits), an average of 12bits per point (the second figure in the following figure). This will compress the image data by half.

The above only gives a theoretical example that may be different in the actual data store, with several specific storage forms listed below:

(1) YUV 4:4:4

YUV three channels have the same sampling rate, so in the generated image, the three component information of each pixel is complete (usually 8 bits per component), after 8 bit quantization, the uncompressed pixel occupies 3 bytes.

The following four pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

The stream stored is: Y0 U0 V0 Y1 U1 V1 Y2 U2 V2 Y3 U3

(2) YUV 4:2:2

The sampling rate of each color-difference channel is half that of the luminance channel, so the chroma sampling rate in the horizontal direction is only half of the 4:4:4. For uncompressed images of 8-bit quantization, a macro pixel of each pixel adjacent to two horizontal orientations requires 4 bytes of memory.

The following four pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

The stored stream is: Y0 U0 Y1 V1 Y2 U2, Y3 V3

Mapped pixel: [Y0 U0 V1] [Y1 U0 V1] [Y2 U2 V3] [Y3 U2 V3]

(3) YUV 4:1:1

The chroma sampling of 4:1:1 is a 4:1 sampling of chroma in the horizontal direction. This is still acceptable for low-end users and consumer products. For uncompressed 8-bit-quantization video, a macro pixel of each pixel adjacent to 4 horizontal orientations takes up 6 bytes of memory.

The following four pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

The stored code stream is: Y0 U0 Y1 Y2 V2 Y3

Mapped pixel: [Y0 U0 V2] [Y1 U0 V2] [Y2 U0 V2] [Y3 U0 V2]

(4) yuv4:2:0

4:2:0 does not mean that there is only Y,CB but no CR component. It means that for each line of scan, only one chroma component is stored at a sample rate of 2:1. Adjacent scan rows store different chroma components, that is to say, if a row is 4:2:0, the next line is 4:0:2, and then the next line is 4:2:0 ... and so on. For each chroma component, the horizontal and vertical sampling rate is 2:1, so it can be said that the chroma of the sampling rate is 4:1. For uncompressed 8-bit-quantization video, a macro pixel consisting of pixels adjacent to a 2x2 2 row 2 column requires 6 bytes of memory.

The following eight pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

[Y5 U5 V5] [Y6 U6 V6] [Y7u7 V7] [Y8 U8 V8]

The stored code stream is: Y0 U0 Y1 Y2 U2 Y3

Y5 V5 Y6 Y7 V7 Y8

The mapped pixel points are: [Y0 U0 V5] [Y1 U0 V5] [Y2 U2 V7] [Y3 U2 V7]

[Y5 U0 V5] [Y6 U0 V5] [Y7u2 V7] [Y8 U2 V7]

Finish!

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.