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

RGB color coding

RGB three letters represent Red, Green and Blue respectively. These three colors are called primary colors, and they can be added in different proportions to produce a variety of colors.

In image display, a 1280 × 720 image means it has 1280 × 720 pixels. RGB coding method is adopted for the color display of each pixel, and different colors will be displayed by taking different VALUES of RGB.

In RGB images, each pixel has three primary colors, red, green and blue. Each primary color occupies 8 bits, that is, one byte, so a pixel will occupy 24 bits, that is, three bytes.

A 1280 * 720 image occupies 1280 * 720 * 3/1024/1024 = 2.63 MB of storage space.

YUV color coding

YUV color coding uses brightness and chroma to specify the color of a pixel.

Y represents Luminance (Luma) and U and V represent Chrominance (Chroma).

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

Using YUV color coding to represent an image, it should look like this:

Similar to RGB images, each pixel contains Y, U and V components. But its Y and UV components can be separated, without the UV component can display a complete image, but in black and white.

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

RGB to YUV conversion

For image display, RGB model is used to display images, and YUV model is used when transmitting image data, because YUV model can save bandwidth. Therefore, it is necessary to convert RGB model to YUV model during image collection, and then convert YUV model to RGB model during display.

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

There is the following formula for conversion:

After the conversion, each pixel has a complete Y, U and V component. As mentioned above, the Y and UV components can be separated. Then, the Y, U and V components of the image can be recombined through different sampling methods.

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

YUV sampling format

The main sampling methods of YUV images are as follows:

  • 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 Y, U and V components is the same. Therefore, in the generated image, the information of the three components of each pixel is complete, which is 8 bits, that is, one byte.

As shown below:

The Y component is represented by a cross and the UV component by a circle.

For example, if the image pixel is: [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3], then the sampling code stream is: The final mapped pixels of Y0 U0 V0 Y1 U1 V1 Y2 U2 V2 Y3 U3 V3 are still [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3].Copy the code

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

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 in a ratio of 2:1. If there are 10 pixels in the horizontal direction, then 10 Y components are sampled and only 5 UV components are sampled.

As shown below:

The Y component is represented by a cross and the UV component by a circle.

For example, if the image pixel is: [Y0 U0 V0], [Y1 U1 V1], [Y2 U2 V2], [Y3 U3 V3], then the sampling code stream is: Y0 U0 Y1 V1 Y2 U2 Y3 V3 Where, every pixel sampled, its Y component will be sampled, while U and V components will be collected one at an interval. Finally, the mapped pixels are [Y0 U0 V1], [Y1 U0 V1], [Y2 U2 V3], [Y3 U2 V3].Copy the code

The sampled codestream is mapped to pixels, and each pixel has Y, U, and V components. However, it can be seen that the first and second pixels share U0 and V1 components, while the third and fourth pixels share U2 and V3 components, thus saving image space.

The size of a 1280 * 720 image sampled at 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 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 sampling only the U component but not the V component. Instead, only one chromaticity component (U or V) is scanned for each line, and the Y component is sampled in a 2:1 fashion. For example, YU samples the first row in a 2:1 fashion, while YV components are sampled in a 2:1 fashion in the second row. For each chromaticity component, its horizontal and vertical samples are 2:1 relative to the Y component.

As shown below:

The Y component is represented by a cross and the UV component by a circle.

Assuming that the first row scans the U component and the second row scans the V component, two rows need to be scanned to complete the UV component.

For example, suppose the image pixel is: [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 sampling code stream is: Y0 U0 Y1 Y2 U2 Y3 Y5 V5 Y6 Y7 V7 Y8 Where, every sampled pixel point will sample its Y component, while U and V components will be sampled according to 2:1 in an interval. The final mapped pixels are: [Y0 U0 V5], [Y1 U0 V5], [Y2 U2 V7], [Y3 U2 V7] [Y5 U0 V5], [Y6 U0 V6], [Y7 U2 V7], [Y8 U2 V8]Copy the code

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 with YUV 4:2:2 sampling, the two Y components share a set of UV components, which can save more space.

The size of a 1280 * 720 image sampled at 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 YUV 4:2:0 sampling image saves half of the storage space compared with RGB model image, so it is also a relatively mainstream sampling method.

YUV storage format

Having said sampling, the next step is to store the sampled data.

There are two storage formats of YUV:

  • Planar scheme
    • Refers to storing the Y component of all pixels consecutively first, then the U component, and finally the V component.
  • Packed mode
    • It means that Y, U and V components of each pixel point are stored continuously and alternately.

Depending on the sampling method and storage format, there are various YUV formats. These formats are mainly based on YUV 4:2:2 and YUV 4:2:0 samples.

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

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

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

YUV 4:2:0 sampling YUV 4:2:0 sampling
YUV 420 p type YV12 format YU12 format
YUV 420 sp type NV12 format NV21 format

For more information on YUV formats, see here: YUV Pixel formats

Based on YUV 4:2:2 sampling format

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

YUYV format

YUYV format is stored in package format, which means that Y component is used for each pixel, but its UV component is sampled every other pixel, and the sequence is as follows:

Y0 UO Y1 V0 Y2 U2 Y3 V2

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

UYVY format

UYVY format is also stored in package format, and its sequence is opposite to YUYV. U component is adopted first and then Y component is sampled, and the sequence is as follows:

U0 Y0 V0 Y1 U2 Y2 V2 Y3

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

There are other formats based on the order of UV and Y, such as YVYU format, VYUY format and so on, the principle is roughly the same.

YUV 422 p format

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

Based on YUV 4:2:0 sampling format

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 420 p type
    • YU12 format
    • YV12 format
  • YUV 420 sp type
    • NV12 format
    • NV21 format

YUV420P and YUV 420SP are stored in Planar mode. After storing all Y components, YUV420P stores all U or V components. YUV420SP is stored in alternating order of UV or VU, see the following figure for details:

YUV420SP format:

YUV420P format:

YU12 and YV12 formats

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

The storage format of YV 12 is as follows:

YU 12, also known as the I420 format, stores the V and U in reverse.

NV12 or NV21

Both NV12 and NV21 formats belong to YUV420SP. It also stores the Y component first, but then instead of storing all the U or V components, it stores the UV component alternately and consecutively.

NV12 is a mode in IOS, which is stored in the order of Y component first, then UV for alternate storage.

NV21 is an Android mode, which stores Y components first and stores them alternately in VU.

summary

The above is the knowledge summary about YUV, do you understand?

reference

  1. Blog.csdn.net/grow_mature…
  2. www.cnblogs.com/azraelly/ar…
  3. Blog.csdn.net/MrJonathan/…
  4. Blog.csdn.net/wudebao5220…
  5. Msdn.microsoft.com/zh-cn/libra…
  6. www.fourcc.org/yuv.php

Welcome to pay attention to wechat public number: [paper talk], get the latest article push ~~~