当前位置:网站首页>YUV format and RGB format

YUV format and RGB format

2022-06-09 22:32:00 StephenBarrnet

One 、RGB Coding format

RGB The image has three channels R、G、B, Corresponding to red 、 green 、 Three components of blue , The color is determined by the values of the three components ; Usually , Will give RGB Image plus a channel alpha, Transparency , So there are four components to control the color .

In general ,RGB Image , Every pixel has red 、 green 、 Blue three primary colors , Each primary color takes up 8 bit, That's one byte , So a pixel is occupied 24 bit, That 's three bytes . If it is argb Format image ,1 Pixels required 32 bit, That is to say 4 Bytes .

In the image display , a sheet 1280 * 720 Size picture , It means it has 1280 * 720 Pixels . The color of each pixel is displayed in RGB coding method . a sheet 1280 * 720 Size picture , Take up 1280 * 720 * 3 / 1024 / 1024 = 2.63 MB Storage space .

Two 、YUV Coding format

YUV Compared with RGB The biggest advantage of the format is that it can achieve without obvious reduction of image quality , Reduce file size .TUV The reason why format can do , Because of the sampling operation .

YUV Color coding uses Brightness and chroma To specify the color of the pixel .. among ,Y Express Brightness (Luminance、Luma), and U and V Express chroma (Chrominance、Chroma).

and RGB Indicates that the image is similar to , Every pixel contains Y、U、V component . But it's Y and UV Components can be separated , without UV The component can also display the complete image , It's just black and white .

about YUV Image , Not every pixel needs to contain Y、U、V The three components , According to different sampling formats , Can each Y Each component corresponds to its own UV component , Or a few Y Share the weight UV component .

 Insert picture description here

YUV Storage format ( classification ):

YUV Two major categories of formats : Plane (plannr) and compact (packed).

  • about planar Of YUV Format , First, store all pixels in a row Y, And then it stores all the pixels U, Along with the storage of all pixels V, Or first v after u
  • about packed Of YUV Format , Every pixel point Y,U,V It's a continuous alternate storage . such as YUV420P, among P It means compact ,YUV420SP Among them SP Express “ Semi compact ”.
YUV420P:YYYYYYYY UU VV 
YUV420: YUV YUV YUV 
  • packed:Y、U、V The components are interspersed , There is one of the three components Byte Type array ;
  • planar:Y、U、V There are three components Byte Type array .

YUV Sampling methods :

YUV There are three main ways of sampling ,YUV4:4:4,YUV4:2:2 ,YVU4:2:0.

  • YUV 4:4:4 sampling :YUV 4:4:4 sampling , signify Y、U、V The sampling scale of the three components is the same , So in the resulting image , The three components of each pixel are complete , All are 8 bit, That's one byte .

As shown in the figure below :
 Insert picture description here
among ,Y The component is represented by a cross ,UV The component is represented by a circle .

for instance :
If the image pixel is :[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 V3
Finally, the mapped pixels are still [Y0 U0 V0]、[Y1 U1 V1]、[Y2 U2 V2]、[Y3 U3 V3]

You can see the images and RGB The image size of the color model is the same , Did not achieve the purpose of saving bandwidth , When will RGB Image to YUV In the picture , It's also a conversion to YUV 4:4:4 Sampled images .

  • YUV4:2:2 sampling :YUV 4:2:2 sampling , signify UV The weight is Y Half of the component sampling ,Y Weight and UV Component according to 2 : 1 Proportional sampling . If there is... In the horizontal direction 10 Pixels , So sampling 10 individual Y component , And only sampled 5 individual UV component .

As shown in the figure below :
 Insert picture description here
among ,Y The component is represented by a cross ,UV The component is represented by a circle .

for instance :
If the image pixel is :[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 , Every pixel sampled , Will sample its Y component , and U、V Components will be collected one at a time .
Finally, the mapped pixels are [Y0 U0 V1]、[Y1 U0 V1]、[Y2 U2 V3]、[Y3 U2 V3]

The sampled code stream is mapped to pixels , It is still necessary to meet that each pixel has Y、U、V The three components . But you can see , The first and second pixels are in common U0、V1 component , The third and fourth pixels are common U2、V3 component , This saves image space .

a sheet 1280 * 720 Size picture , stay YUV 4:2:2 The sample size is :

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

You can see YUV 4:2:2 The sampled image is better than RGB The model image saves one third of the storage space , The bandwidth occupied during transmission will also be reduced .

  • YUV 4:2:0 sampling :YUV 4:2:0 sampling , It's not just sampling U Component without sampling V component . But to , At each line scan , Scan only one chrominance component (U perhaps V), and Y Component according to 2 : 1 The way of sampling . such as , When the first line is scanned ,YU according to 2 : 1 The way of sampling , So when the second line is scanned ,YV Component according to 2:1 The way of sampling . For each chroma component , Its horizontal and vertical sampling and Y The components are all 2:1 .

As shown in the figure below :
 Insert picture description here
among ,Y The component is represented by a cross ,UV The component is represented by a circle .

Suppose the first line is scanned U component , The second line scans V component , Then you need to scan two lines to form a complete UV component .

for instance :
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 sampled code stream is :Y0 U0 Y1 Y2 U2 Y3 Y5 V5 Y6 Y7 V7 Y8
among , Every pixel sampled , Will sample its Y component , and U、V The components are separated by a line according to 2 : 1 sampling .
Finally, the 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]

From the mapped pixels, you can see , four Y The components share a set UV component , And according to 2*2 Distributed in the form of a small square , comparison YUV 4:2:2 Two of the samples Y Components share one set UV component , This will save more space .

a sheet 1280 * 720 Size picture , stay YUV 4:2:0 The sample size is :

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

You can see YUV 4:2:0 The sampled image is better than RGB Model images save half of the storage space , Therefore, it is also a mainstream sampling method .

common YUV Format

  • YUV422:YUYV、YVYU、UYVY、VYUY
    Each of these four formats can be divided into 2 class (packed and planar), With YUYV For example , One 6*4 The image of is stored as follows :
	Y Y Y Y Y Y                   
	Y Y Y Y Y Y                  
	Y Y Y Y Y Y                   
	Y Y Y Y Y Y                    
	U U U U U U                  Y U Y V Y U Y V Y U Y V
	U U U U U U                  Y U Y V Y U Y V Y U Y V
    V V V V V V                  Y U Y V Y U Y V Y U Y V
	V V V V V V                  Y U Y V Y U Y V Y U Y V
	- Planar -                          - Packed - 

- YUV420:I420、YV12、NV12、NV21
YUV420p: I420、YV12
YUV420sp: NV12、NV21

Again , For one 6*4 Image , The four pixel formats are stored as follows :

	Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y
	Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y
	Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y
	Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y      Y Y Y Y Y Y
	U U U U U U      V V V V V V      U V U V U V      V U V U V U
	V V V V V V      U U U U U U      U V U V U V      V U V U V U
     - I420 -          - YV12 -         - NV12 -         - NV21 -

  • I420、YV12 All three components are in plane format , That is, there are three Byte Type array ;
  • NV12、NV21 The storage format of is Y Plane ,UV pack , namely Y Information is stored in an array ,UV Information is stored in a matrix .

3、 ... and 、RGB And YUV Conversion formula

YUV The pixel format comes from RGB Pixel format , Through formula operation ,YUV Three components can be reduced to RGB,YUV turn RGB The formula is as follows :

R = Y + 1.403V
G = Y - 0.344U - 0.714V
B = Y + 1.770U

commonly , take RGB and YUV The scope of is limited to [0,255] between , Then there is the following conversion formula :

R = Y + 1.403(V - 128)
G = Y - 0.344(U - 128) - 0.714(V - 128)
B = Y + 1.770(U - 128)

References:
Common image pixel formats NV12、NV2、I420、YV12、YUYV
Article to read YUV Sampling and format of

原网站

版权声明
本文为[StephenBarrnet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092145294510.html