YUV format Analysis

Source: Internet
Author: User

Andrew Huang <bluedrum@163.com> reprinted please indicate the author and contact information

Programming like cameras often encounters the YUV format, rather than the RGB format that everyone is familiar. we can understand YUV as an RGB variant. the principle of YUV is to separate the brightness from the color. Studies have proved that the sensitivity of human eyes to the brightness exceeds the color. Using this principle, we can reduce the color information by a bit, and the human eyes cannot check this. Among the three YUV letters, "Y" indicates the brightness (Lumina nce or Luma), that is, the gray scale value; "u" and "V" represent chrominance or chroma, which describes the image color and saturation and is used to specify the pixel color. The three letters seem to be the channel command. YUV has two advantages: 1. Converting color YUV images to black and white YUV images is very simple, and this feature is used on TV signals. Ii. YUV: the total data size is smaller than the YUV format described in the RGB format. This file <video rendering with 8-bit YUV formats>

Http://msdn.microsoft.com/en-us/library/aa904813 (vs.80). aspx1. YUV format and RGB format conversion
For information on YUV and RGB conversion, see

Http://www.fourcc.org/fccyvrgb.php uses the following standard formula:

  1. Convert RGB to YUV
  2. Y = (0.257 * r) + (0.504 * g) + (0.098 * B) + 16
  3. Cr = V = (0.439 * r)-(0.368 * g)-(0.071 * B) + 128
  4. CB = u =-(0.148 * r)-(0.291 * g) + (0.439 * B) + 128
  5. Convert YUV to RGB
  6. B = 1.164 (Y-16) + 2.018 (u-128)
  7. G = 1.164 (Y-16)-0.813 (V-128)-0.391 (u-128)
  8. R = 1.164 (Y-16) + 1.596 (V-128)

The RGB value ranges from 0 ~ 255, y = 0 ~ 255, u =-122 ~ + 122, V =-157 ~ + 157
The following is a simplified formula that requires less computing than the preceding formula.

  1. Conversion from RGB to YUV
  2. Y = 0.299r + 0.587G + 0.114b
  3. U' = (by) x 0.565
  4. V' = (RY) x 0.713
  5. Convert YUV to RGB
  6. R = Y + 1.403v'
  7. G = Y-0.344u '-0.714v'
  8. B = Y + 1.770u'

If only the Y Signal component is used, but not the U or V component, the black and white gray images are displayed. Therefore, the conversion from color to black and white signals in YUV format is quite simple. in the technical documents, YUV often has another name, YCbCr, where Y and Y in YUV have the same meaning, CB and Cr both refer to colors, but they only differ in the representation methods, CB Cr is the identifier of "component/chromatic aberration" in theory. C Indicates the component (abbreviated to component) Cr and CB correspond to the R (red) and B (blue) component signals respectively. y except g (green) component signals, the Brightness Signal is also superimposed. Another format is ypbpr, which differs from ycbpr in that YCbCr is a line-by-line signal and ypbpr is a line-by-line signal. All digital signals are YCbCr, which is widely used. JPEG and MPEG use this format. If not specified, YUV indicates the YCbCr format. Ypbpr is generally a simulated signal. I reference two sections to illustrate the difference between the two.
  1.   What is YCbCr?
  2. YCbCr indicates the barrier component terminal, which is a version of YUV that has been scaled and offset. YUV is also called YCbCr. Among them, Y has the same meaning as Y in YUV. CB and Cr both refer to colors, but they are different in the representation method. CB Cr is the identifier of the "component/chromatic aberration" in theory. C Indicates the component (abbreviated to component) Cr and CB correspond to the R (red) and B (blue) component signals respectively. y except g (green) component signals, the Brightness Signal is also superimposed.
  3. In this example, YCbCr is a line-by-line signal and ypbpr is a line-by-line signal. YCbCr is the most widely used signal in computer systems. It is widely used in JPEG and MPEG formats.
  4.   What is ypbpr?
  5. 'Cbcr is also known as ypbpr in Analog Component Video (Analog Component Video). ypbpr separates analog y, Pb, and PR signals and uses three cables for independent transmission, this ensures the accuracy of color reproduction, and ypbpr indicates a line-by-line scanning color difference output. the ypbpr interface can be seen as an extension of the S terminal. Compared with the S terminal, it needs to transmit more PB and PR signals, avoiding the process of two-way chromatic aberration mixed decoding and re-separation, it also maintains the maximum bandwidth of the chromium channel, and can be restored to RGB three primary color signals after the inverse matrix decoding circuit, this minimizes the video signal channel between the video source and the display imaging, avoids the image distortion caused by the tedious transmission process, and ensures the accuracy of color reproduction, currently, almost all large screen TVs support chromatic aberration input.

2. In the RGB format of YUV, A 24bpp pixel occupies 4 bytes of space. In YUV format, data of UV components can be compressed, but it has little impact on the overall quality of the image, in this way, the space occupied by YUV is smaller than that occupied by RGB, but in the 565 format of 16 BPP in RGB, each vertex occupies only two bytes. From this point of view, it does not save much. However, video applications are all YUV applications. Therefore, YUV processing is still an important issue. The biggest difference between YUV and RGB is that the data of each vertex in RGB format is continuously stored. That is, R, G, and B are stored in a 2-4 byte space at intervals. In YUV data, U and V components are reduced to save space. The Y component of each vertex is stored independently, but the u and v components of several consecutive vertices are stored together (the human eyes generally cannot tell the difference ). these vertices are collectively called macro-pixel, which is called the packed format. Another storage format is to represent y, U, and V in an image with three independent arrays. This mode is called the planar mode. Here we mainly use the first packed format. 2.1 sample formatYUV format. Generally, the ratio of Y, U, and V is used to represent different formats. For example, yuv420 indicates that the ratio of the three is, that is, a point data point, Y, U, the space of V is the same size. At present, there are mainly the following proportions. Note that the Y ratio in all formats is 4, which occupies one byte, indicating that the sampling is not reduced. In different formats, only the UV sample value is reduced.
  • Indicates that the color value (UV) does not reduce sampling. That is, Y, U, and V occupy one byte, And the alpha channel occupies four bytes in total. This format is actually the RGB format of 24bpp.
  • : 2 indicates that UV sampling is halved. For example, the first pixel samples y, U, and the second pixel samples y, V, and so on, so that each vertex occupies 2 bytes. two pixels form a macro pixel.
  • This sampling does not mean that only y and CB have no Cr components. Here, 0 indicates that the u and v components are sampled only once in the line. For example, sampling at on the first line, sampling at on the second line, and so on... In this sampling mode, each pixel occupies 16 bits or 10 bits space.
  • Can be referred to as, which is further compressed and the u and v components are collected once every four points. Generally, Y, U, and Y are collected at, YV is collected at, Y is collected at, and Y is collected at, and so on.
Except for sampling at, after the signal is restored and displayed, some UV data is lost and only the adjacent data can be used. However, the human eyes are not sensitive to UV, so the overall loss is not great. ExampleIf the three pixels of the original data are y0 U0 v0, Y1 U1 V1, Y2 U2 V2, and Y3 U3 V3, after sampling, the data becomes y0 U0, Y1 V1, Y2 U2, after Y3 V3 is restored, it will be replaced with y0 U0 due to some data loss. V1, Y1 U0V1, Y2 U2
V3, Y3 U3 Y2
Example The original Four pixels are: [y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]. The bit streams are: y0 U0, Y1, Y2 V2, y3 restores the pixel: [y0 U0 V2] [Y1 U0 V2] [Y2 U0 V2] [Y3 U0 V2] ExampleThe 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] Storage the code stream is: y0 U0, Y1, Y2 U2, Y3, Y5 V5, y6, y7 V7, and Y8 map the following pixels: [y0 U0 V5] [Y1 U0 V5] [Y2 U2 V7] [Y3 U2 V7] [Y5 U0 V5] [y6 U0 V5] [y7u2 V7] [Y8 U2 V7] YUV 4: 4 Memory layout, each pixel has a complete YUV, plus a (alpha)
YUV in byte order
2.2 four cc codeFor the names of yuv444, yuv422, and yuv420, there is another naming method, the fourcc code, which is used in the preceding command. The four cc uses four letters for naming, fourcc is the four-character codes. It consists of 4 characters (4 bytes) and is a four-byte format that uniquely identifies the video data stream, there will be a fourcc section in the WAV and AVI files to describe the AVI files, which are encoded using codec. Therefore, WAV and AVI have a large number of fourcc equal to "idp3 ".
Http://zh.wikipedia.org/wiki/fourccname by fourcc. fourcc of http://www.fourcc.org/yuv.phpYUV444 is called
The fourccc of ayuvyuv422 is divided into byte order.
The fourccc values of yuy2 and uyvyyuv420 are sorted in byte order.
Imc1 and imc2 2.3 v4l2 adopts EncodingWe generally use this encoding in the v4l driver, and v4l2 also has a class of corresponding encoding. see vedio2dev. h. in programming, it must correspond to the actual arrangement. the defined value after it is actually the fourcc code. # define v4l2_fourcc ('y', 'V', '1', '2')/* 12 yvu */# define v4l2_pix_fmt_yuyv v4l2_fourcc ('y ', 'U', 'y', 'V')/* 16 YUV */# define v4l2_pix_fmt_uyvy v4l2_fourcc ('U', 'y', 'V', 'y ') /* 16 YUV */# define v4l2_pix_fmt_yuv422p v4l2_fourcc ('4', '2', '2', 'P ') /* 16 yvu422 planar */# define v4l2_pix_fmt_yuv411p v4l2_fourcc ('4', '1', '1', 'P ') /* 16 yvu411 planar */# define v4l2_pix_fmt_y41p v4l2_fourcc ('y', '4', '1', 'P ') /* 12 YUV */# define v4l2_pix_fmt_yuv444 v4l2_fourcc ('y', '4', '4', '4 ') /* 16 xxxxyyyy uuuuvvvv */# define v4l2_pix_fmt_yuv555 v4l2_fourcc ('y', 'U', 'V', and 'O'). For example, in a CMOS camera, refer is supported, it corresponds to the yv12 format and queries the corresponding documents. It is in plane format, that is, Y, U, and V are arranged in three zones.

Yv12 Zero X 32315659 12 8 bit Y plane followed by 8 bit 2x2 subsampled V and U planes.

This is the format of choice for your software MPEG codecs. It comprises an nxm Y plane followed by (n/2) x (M/2) V and U planes.

  Horizontal Vertical
Y sample period 1 1
V sample period 2 2
U sample period 2 2


Positive biheight implies top-down image (top line first)

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.