Conversion principle and code example of RGB to YUV

Source: Internet
Author: User
Conversion principle and code example of RGB to YUV

Conversion principle and code example of RGB to YUV

 

Because H.264 and other compression algorithms are performed in the color space of YUV, color space conversion is required before compression. If the resources collected by the camera are RGB, first convert them to YUV. if the resources are YUV, sort the data according to the YUV format supported by the compressors. This article uses rgb24 à yuv420 (yv12) as an example to explain the principle of color space conversion.

Data Representation

The following describes how to arrange a 320*240 frame of image rgb24:

Each pixel consists of three bytes to represent the color values on the R, G, and B components. The representation in the data is represented by one pixel and one pixel. Byte streams can be expressed as follows:

Bgrbgrbgrbgrbgr ......

| ------------- 320*240*3 ------- |

Each letter represents a byte, that is, the value of the color component. The adjacent three BGR bytes represent a pixel. In our computing, we usually take three bytes at a time, that is, a pixel.

Shows how yv12 is arranged:

Each pixel has a Y component, and each column has a U or V component, which alternate with v. The byte stream representation of yv12 is very different from that of rgb24. yv12 is not arranged in order of pixels, but is placed in the Y space first, then the entire v space, and finally the U space, the byte stream is as follows:

Yyyyyyy ...... Vvvv ...... UUUU ......

| ----- 320*240 ---- |-320*240/4-|-320*240/4-|

After 320*240 bytes of Y, follow 320*240/4 V and 320*240/4 U.

Yv12 and rgb24 both have 320x240 pixels, but they differ greatly in data structure and word throttling. From the data size perspective, the data size of rgb24 is 320*240*3 bytes, while that of yv12 is 320*240*1.5 bytes. It can be seen that the data size of yv12 is half of that of rgb24.

Conversion Formula

After understanding the data expression method, you only need to calculate the RGB values of the corresponding pixels into the corresponding YUV values, and then express the data through the YUV byte stream style. Here, first, we will introduce the conversion formula from RGB to YUV.

Y = 0.3 * r + 0.59 * g + 0.11 * B

U = (B-Y) * 0.493

V = (R-Y) * 0.877

In turn, the YUV formula for converting to RGB is as follows:

R = Y + 1.14 V

G = Y-0.39u- 0.58 V

B = Y + 2.03u

Sample Code

The following is an example of the conversion code from rgb24 to yv12 (yuv420) (C ++ ):

Uint_8_t * psrc =; // This is RGB bit stream

Uint_8_t * yuv_image = new uint_8 [320*240*3/2]; // yuv420 bit stream

Int I = 0, j = 0;

Int width = 320; // width of the RGB image

Int Height = 240; // height of the RGB image

Int upos = 0, VPOs = 0;

For (I = 0; I

Bool ISU = false;

If (I % 2 = 0) ISU = true; // This is a U line

For (j = 0; j

Int Pos = width * I + J; // pixel position

Uint_8_t B = psrc [POS * 3];

Uint_8_t G = psrc [POS * 3 + 1];

Uint_8_t r = psrc [POS * 3 + 2];

 

Uint8_t y = (uint8_t) (0.3 * r + 0.59 * g + 0.11 * B );

Uint8_t u = (uint8_t) (B-Y) * 0.493 );

Uint8_t v = (uint8_t) (R-Y) * 0.877 );

 

Yuv_image [POS] = y;

Bool ischr = false; // is this a Chroma point

If (J % 2 = 0) ischr = true;

If (ischr & ISU ){

Yuv_image [plane + (plane> 2) + upos] = u;

}

If (ischr &&! ISU ){

Yuv_image [plane + VPOs] = V;

}

}

}

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.