Detailed explanation of YUV and RGB formats

YUV is a color encoding method, and its equivalent is RGB color encoding method.

RGB color coding

The three letters of RGB represent red (Red), green (Green), and blue (Blue). These three colors are called the three primary colors Adding them in different proportions can produce a variety of colors.

In image display, a picture with a size of 1280 * 720 means that it has 1280 * 720 pixels. The color display of each pixel adopts RGB encoding method, and different values ​​of RGB will display different colors.

Please add a picture description

In an RGB image, each pixel has three primary colors of red, green, and blue, and each primary color occupies 8 bits, that is, one byte, so one pixel also occupies 24 bits, that is, three bytes .

A picture with a size of 1280 * 720 occupies 1280 * 720 * 3 / 1024 / 1024 = 2.63 MB storage space

YUV color coding

YUV color encoding uses luminance and chrominance to specify the color of a pixel.

where Y represents brightness (Luminance, Luma), while U and V represent chroma (Chrominance, Chroma).

Chroma, in turn, defines two aspects of color: hue and saturation.

To represent an image using YUV color coding, it should look like this:
Please add a picture description

Similar to RGB image representation, each pixel contains Y, U, V components. But its Y and UV components can be separated. If there is no UV component, the complete image can be displayed, but it is black and white.

For YUV images, not every pixel needs to contain three components of Y, U, and V. According to different sampling formats, each Y component can correspond to its own UV component, or several Y components can be shared. UV component.

RGB to YUV conversion

For image display, it displays images through the RGB model, and uses the YUV model when transmitting image data, because the YUV model can save bandwidth. Therefore, it is necessary to convert the RGB model to the YUV model when capturing the image, and then convert the YUV model to the RGB model when displaying.

The conversion from RGB to YUV is to convert the R, G, and B components of all pixels in the image to Y, U, and V components.

There is the following formula for conversion:
Please add a picture description

After the conversion at this time, each pixel has complete Y, U, V components. As mentioned earlier, the Y and UV components can be separated, and then through different sampling methods, the Y, U, and V components of the image can be recombined.

The following different sampling formats are all based on the RGB to YUV conversion of all pixels of an image.

YUV sampling format

There are three mainstream sampling methods for YUV images:

  • YUV 4:4:4 sampling
  • YUV 4:2:2 sampling
  • YUV 4:2:0 sampling

YUV 4:4:4 sampling

YUV 4:4:4 sampling means that the sampling ratio of the three components of Y, U, and V is the same, so in the generated image, the information of the three components of each pixel is complete, all of which are 8 bits, that is, one byte .

As shown below:
Please add a picture description

Among them, the Y component is represented by a cross, and the UV component is represented by a circle.

For example: If the image pixels are: [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3] then the sampled code stream is: Y0 U0 V0 Y1 U1 V1 Y2 U2 V2 Y3 U3 The last mapped pixels of V3 are still [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3]

It can be seen that the image size of this sampling method is the same as that of the RGB color model, which does not achieve the purpose of saving bandwidth. When converting an RGB image to a YUV image, it is also first converted to a YUV 4:4:4 sampled image .

YUV 4:2:2 sampling

YUV 4:2:2 sampling means that the UV component is half of the Y component sampling, and the Y component and UV component are sampled at a ratio of 2:1. If there are 10 pixels in the horizontal direction, then 10 Y components are sampled, but only 5 UV components are sampled.

As shown below:
Please add a picture description

Among them, the Y component is represented by a cross, and the UV component is represented by a circle.

For example: If the image pixels are: [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3] then the sampled code stream is: Y0 U0 Y1 V1 Y2 U2 Y3 V3 Among them, each After a pixel is sampled, its Y component will be sampled, and the U and V components will be sampled one at a time. The final mapped pixels are [Y0 U0 V1], [Y1 U0 V1], [Y2 U2 V3], [Y3 U2 V3]

The sampled code stream is mapped to pixels, and each pixel must have three components: Y, U, and V. But it can be seen that the first and second pixels share U0 and V1 components, and the third and fourth pixels share U2 and V3 components, which saves image space.

A 1280 * 720 size picture, the size when sampling in YUV 4:2:2 is:

(1280 * 720 * 8 + 1280 * 720 * 0.5 * 8 * 2)/ 8 / 1024 / 1024 = 1.76 MB 。

It can be seen that the YUV 4:2:2 sampled image saves one-third of the storage space compared with the RGB model image, and the bandwidth occupied during transmission will also be reduced accordingly.

YUV 4:2:0 sampling

YUV 4:2:0 sampling does not mean only sampling the U component but not the V component. Instead, it means that when scanning each line, only one chrominance component (U or V) is scanned, and the Y component is sampled in a 2:1 manner. For example, when the first line is scanned, YU is sampled in a 2:1 manner, then when the second line is scanned, the YV component is sampled in a 2:1 manner. For each chroma component, its horizontal and vertical samples are 2:1 compared to the Y component.

As shown below:
Please add a picture description

Among them, the Y component is represented by a cross, and the UV component is represented by a circle.

Assuming that the U component is scanned in the first line and the V component is scanned in the second line, then two lines need to be scanned to form a complete UV component.

For example: Suppose the image pixels are: [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3] [Y5 U5 V5], [Y6 U6 V6], [Y7 U7 V7], [Y8 U8 V8] Then the sampled code stream is: Y0 U0 Y1 Y2 U2 Y3 Y5 V5 Y6 Y7 V7 Y8 Among them, every time a pixel is sampled, its Y component will be sampled, and the U and V components will be separated by a row according to 2 : 1 for sampling. The final mapped pixels are: [Y0 U0 V5], [Y1 U0 V5], [Y2 U2 V7], [Y3 U2 V7] [Y5 U0 V5], [Y6 U0 V5], [Y7 U2 V7], [ Y8 U2 V7]

It can be seen from the mapped pixels that the four Y components share a set of UV components, and are distributed in the form of 2*2 small squares, compared to the two YUV 4:2:2 sampling Y components share a set of UV components, which can save space.

The size of a 1280 * 720 image when sampling in YUV 4:2:0 is:

(1280 * 720 * 8 + 1280 * 720 * 0.25 * 8 * 2)/ 8 / 1024 / 1024 = 1.32 MB 。

It can be seen that the YUV 4:2:0 sampled image saves half of the storage space than the RGB model image, so it is also a relatively mainstream sampling method.

YUV storage format

After talking about sampling, the next step is how to store the sampled data.

There are two storage formats for YUV:

  • planar flat format
    • It means that the Y components of all pixels are stored continuously first, then the U components, and finally the V components.
  • packed packing mode
    • It means that the Y, U, and V components of each pixel are stored continuously and alternately.

According to different sampling methods and storage formats, there are a variety of YUV formats. These formats are mainly based on YUV 4:2:2 and YUV 4:2:0 sampling.

Common formats based on YUV 4:2:2 sampling are as follows:

YUV 4:2:2 sampling
YUYV format
UYVY format
YUV 422P format

Common formats based on YUV 4:2:0 sampling are as follows:

YUV 4:2:0 sampling YUV 4:2:0 sampling
YUV 420P type YV12 format YU12 format
YUV 420SP type NV12 format NV21 format

Format based on YUV 4:2:2 sampling

YUV 4:2:2 sampling specifies that the Y and UV components are sampled at a ratio of 2:1, and the two Y components share a set of UV components.

YUYV format

The YUYV format is stored in a packed format, which means that each pixel uses the Y component, but its UV component is sampled every other pixel, and the order is as follows:

Y0 UO Y1 V0 Y2 U2 Y3 V2

Y0 and Y1 share U0 V0 component, Y2 and Y3 share U2 V2 component....
Please add a picture description

UYVY format

The UYVY format is also stored in a packed format. Its order is opposite to that of YUYV. The U component is used first and then the Y component is sampled. The order is as follows:

U0 Y0 V0 Y1 U2 Y2 V2 Y3

Y0 and Y1 share U0 V0 component, Y2 and Y3 share U2 V2 component....

There are other formats according to the order of UV and Y, such as YVYU format, VYUY format, etc. The principle is roughly the same.
Please add a picture description

YUV 422P format

The YUV 422P format, also known as I422, uses a flat format for storage, first storing all the Y components, then storing all the U components, and then storing all the V components.

Format based on YUV 4:2:0 sampling

The formats based on YUV 4:2:0 sampling mainly include YUV 420P and YUV 420SP, and each type corresponds to other specific formats.

  • YUV 420P type
    • YU12 format
    • YV12 format
  • YUV 420SP type
    • NV12 format
    • NV21 format

Both YUV 420P and YUV 420SP are stored based on the Planar planar mode. After storing all the Y components first, the YUV420P type will first store all the U components or V components, while YUV420SP stores in the alternating order of UV or VU Yes, see the figure below for details:

Format of YUV420SP:
Please add a picture description

Format of YUV420P:
Please add a picture description

YU12 and YV12 formats

Both YU12 and YV12 formats belong to the YUV 420P type, that is, the Y component is stored first, and then the U and V components are stored. The difference is: YU12 first Y and then U and then V, while YV12 first Y and then V and then U.

The storage format of YV 12 is shown in the figure below:
Please add a picture description

YU 12 is also called I420 format, and its storage format is to reverse V and U.

NV12 and NV21 formats

Both NV12 and NV21 formats belong to the YUV420SP type. It also stores the Y component first, but instead of storing all the U or V components, it stores the UV components alternately and continuously.
Please add a picture description
NV12 is a mode in IOS. Its storage sequence is to store the Y component first, and then store UV alternately.

NV21 is a mode in Android, its storage order is to store Y components first, and store them alternately in VU.

summary

The above is a summary of the knowledge about YUV. Do you understand it? Use a picture as a summary~~
Please add a picture description

Guess you like

Origin blog.csdn.net/qq_41290252/article/details/127728627