美文网首页
【色度学】详解各种RGB与YUV(YCbCr)互转公式

【色度学】详解各种RGB与YUV(YCbCr)互转公式

作者: 罗引杰 | 来源:发表于2020-09-30 18:40 被阅读0次

一、 公式:基于BT.601-6  


BT601 CbCr 的坐标图(量化后): (横坐标为u,纵坐标为v,左下角为原点)

通过坐标图我们可以看到UV并不会包含整个坐标系,而是呈一个旋转了一定角度的八边形,  U越大蓝色越蓝,V越大,红色越红。

名词解释:

**量化后**:   Y~[16,235]   Cb ~[16-240]   Cr~[16-240]     量化就是让通过线性变换让Y 或 U 或V 处于一定的范围内, 比如让Y 【0,1】变到 Y' (16,235)  就这样来实行:  Y' = Y* (235-16)/(1-0)   + 16   即 Y' = 219*Y + 16

**未量化**: Y~ [0,1]   U,V~[-0.5,0.5]

**YUV 和 YCbCr的区别**:  YUV是模拟型号, 取值范围为Y~[0-1]   U/V~[-0.5, 0.5]   , YCbCr 是数字信号, 取值范围为 Y ~[16, 235]  UV~[16, 240]

**关于为什么要量化? **

以下是维基百科摘抄的一段, 意思是量化是为了解决滤波后的过冲现象,

Y′ values are conventionally shifted and scaled to the range [16, 235] (referred to as studio swing or "TV levels") rather than using the full range of [0, 255] (referred to as full swing or "PC levels"). This practice was standardized in SMPTE-125M in order to accommodate signal overshoots ("ringing") due to filtering. The value 235 accommodates a maximal black-to-white overshoot of 255 − 235 = 20, or 20 / (235 − 16) = 9.1%, which is slightly larger than the theoretical maximal overshoot (Gibbs phenomenon) of about 8.9% of the maximal step. The toe-room is smaller, allowing only 16 / 219 = 7.3% overshoot, which is less than the theoretical maximal overshoot of 8.9%. This is why 16 is added to Y′ and why the Y′ coefficients in the basic transform sum to 220 instead of 255.[9]U and V values, which may be positive or negative, are summed with 128 to make them always positive, giving a studio range of 16–240 for U and V. (These ranges are important in video editing and production, since using the wrong range will result either in an image with "clipped" blacks and whites, or a low-contrast image.)

关于如何判断图像是否经过量化?

  在完全黑画面的时候打印出图像的Y数据, 如果Y=16左右    说明Y经过量化 ,如果Y=0左右   说明Y未经过量化 

以下具体为各种转换公式

1.小数形式,未量化   (  U~[-0.5-0.5]  ,   R~[0,1]  ), 其中RGB 是经过Gamma 变换后的非线性信号

R = Y + 1.4075 * V;

G = Y - 0.3455 * U - 0.7169*V;

B = Y + 1.779 * U;

Y = 0.299*R + 0.587*G + 0.114*B;

U = (B-Y)/1.772;    

V = (R-Y)/1.402;

或写为:

Y =  0.299*R + 0.587*G + 0.114*B;

U = -0.169*R - 0.331*G + 0.5  *B ;

V =  0.5  *R - 0.419*G - 0.081*B;

2.整数形式(减少计算量)未量化     R,G,B~[0,255]   U,V~[-128,128]

R= Y + ((360 * (V - 128))>>8) ;

G= Y - (( ( 88 * (U - 128)  + 184 * (V - 128)) )>>8) ;

B= Y +((455 * (U - 128))>>8) ;

Y = (77*R + 150*G + 29*B)>>8;

U = ((-44*R  - 87*G  + 131*B)>>8) + 128;

V = ((131*R - 110*G - 21*B)>>8) + 128 ;

3. 量化后的公式( Y~(16,235)  U/V ~(16,240)  )   量化   ( I420 , YUV422 用改公司转换即可 )

[Y,U,V,1]T=  M[R,G,B,1]T其中 M =  

[ 0.2568, 0.5041, 0.0979, 16    

-0.1479, -0.2896, 0.4375, 128    

0.4375, -0.3666, -0.0709, 128,  

0, 0, 0, 1 ]

[R,G,B,1]T = M[Y,U,V,1]T      M =  

1.1644   0   1.6019   -223.5521   

1.1644   -0.3928   -0.8163   136.1381   

1.1644   2.0253   0   -278.0291   

0.0000   0.0000   0.0000   1.0000   

由此可以得到红色的YUV分量   YUV  = (  81,91,240 )

4  量化后的公式写成整数的形式(减小计算量) ( Y~(16,235)  U/V ~(16,240)  )

 yuv --> rgb

R = (298*Y + 411 * V - 57344)>>8

G = (298*Y - 101* U - 211* V+ 34739)>>8

B = (298*Y + 519* U- 71117)>>8

rgb --> yuv 

Y= (  66*R + 129*G  +  25*B)>>8 + 16 

U= (-38*R  -    74*G  + 112*B)>>8 +128

V= (112*R -    94*G  -   18*B)>>8   + 128

5. YUV量化 与 非量化 互转

YUV 量化 转 非量化

Y=(Y'-16   )*255/219 ; 

U=(U'-128)*128/112;

V=(V'-128)*128/112;

YUV 量化 转  非量化  U~(-128-127)  ----->   U~(16-240)

Y' = ((219*Y)>>8)   +   16;

U' = ((219*U)>>8)   + 128;

V' = ((219*V)>>8)   + 128;

6. YV12 转RGB    (这个有待考证。。!!)

R = Y + 1.370705 * ( V - 128 ) ; // r分量值

G = Y -  0.698001 * ( U - 128 )  - 0.703125 * (V - 128) // g分量值

B = Y + 1.732446 * ( U - 128 ); // b分量值

J420 ->RGB

R = Y                - V * -1.40200

G = Y - U *  0.34414 - V *  0.71414

B = Y - U * -1.77200

=>

[RGB,1]T = M [YUV,1]T

[YUV, 1]T = M-1 * [RGB,1]

M = 

1.0000   0.0000   1.3707   -175.4502   

1.0000   -0.6980   0.0000   89.3441   

1.0000   1.7324   0.0000   -221.7531   

0.0000   0.0000   0.0000   1.0000   

M-1 = 

0.0000   0.7128   0.2872   -0.0000   

0.0000   -0.4114   0.4114   128.0000   

0.7296   -0.5200   -0.2095   128.0000   

0.0000   0.0000   0.0000   1.0000   

7. 矩阵形式(BT601):

 矩阵形式

量化前

[Y,U,V]T=  M[R,G,B]T   其中 M = 0.299 , 0.587, 0.114,  -0.169,   - 0.331,   0.5,       0.5,  - 0.419    - 0.081

[R,G,B]T=  M[Y,U,V]T  其中 M = 1    0   1.4017       1   -0.3437   -0.7142       1   1.7722   0

量化后

[Y,U,V,1]T=  M[R,G,B,1]T 其中 M =  [ 0.2568, 0.5041, 0.0979, 16    -0.1479, -0.2896, 0.4375, 128    0.4375, -0.3666, -0.0709, 128,  0, 0, 0, 1 ]

[R,G,B,1]T = M[Y,U,V,1]T            M =  1.1644   0   1.6019   -223.5521   1.1644   -0.3928   -0.8163   136.1381   1.1644   2.0253   0   -278.0291   0.0000   0.0000   0.0000   1.0000   

量化后的公式写成整数形式

[Y,U,V,1]T=  (M[R,G,B,1]T)>>8其中 M =  66, 129, 25, 4096,    -38, -74, 112, 32768,    112, -94, -18, 32768,    0, 0, 0, 256

[R,G,B,1]T = (M[Y,U,V,1]T)>>8 M = 298, 0, 410, -57229,    298,     -101, -209, 34851,    298, 518, 0, -71175,    0, 0, 0, 256

二、Rec2020 (BT2020) 下的YUV与RGB转换公式 

BT2020 UV 的坐标图(量化后): (横坐标为u,纵坐标为v,左下角为原点)

通过坐标图我们可以看到UV不同于BT601协议,该uv代表的颜色范围更大,该颜色范围呈一个不规则八边形。

1.  BT2020 文档上的公式

即:

Y = 0.2627*R + 0.6780*G + 0.0593*B;

U = -0.1396*R - 0.3604*G + 0.5*B;

V = 0.5*R - 0.4598*G -0.0402*B;

矩阵形式

量化前

[Y,U,V]T=  M[R,G,B]T   其中 M = 0.2627   0.6780   0.0593 ,     -0.1396   -0.3604   0.5000,    0.5000   -0.4598   -0.0402        

[R,G,B]T=  M[Y,U,V]T  其中 M = 1.0000   -0.0000   1.4746   1.0000   -0.1645   -0.5713   1.0000   1.8814   -0.0001

量化后

[Y,U,V,1]T=  M[R,G,B,1]T   其中 M = 0.2256, 0.5823, 0.05093, 16,       -0.1222, -0.3154, 0.4375, 128 ,        0.4375, -0.4023, -0.0352, 128,       0,0,0,1

[R,G,B,1]T = M[Y,U,V,1]T            M =1.1644,   0,   1.6853,   -234.3559,       1.1644,   -0.1881,   -0.6529,   89.0206,       1.1646,   2.1501,   0.0000,   -293.8542,       0.0000,   0.0000,   0.0000,   1.0000

量化后的公式写成整数形式

[Y,U,V,1]T=  (M[R,G,B,1]T)>>8其中 M  =  58, 149, 13, 4096,    -31, -81, 112, 32768,    112, -103, -9, 32768,   0, 0, 0, 256

[R,G,B,1]T = (M[Y,U,V,1]T)>>8            M =  298, 0, 431, -59995,    298, -48, -167, 22789,    298, 550, 0, -75227,    0, 0, 0, 256

2. BT601 转 BT2020

_Y = (256*Y  - 32*U -30*V+ 7826)>>8;

_U = (258*U +17*V - 2208)>>8;

_V =  (22*U + 264*V - 3369)>>8;

3. bt2020 转bt601

YUV_601 = M*[Y,U,V,1]T

M=[

1.0000   0.1157   0.1037   -28.0756   

0.0000   0.9951   -0.0602   8.3197 

-0.0000   -0.0835   0.9767   13.6686   

0.0000   0.0000   0.0000   1.0000 

 ]

相关文章

网友评论

      本文标题:【色度学】详解各种RGB与YUV(YCbCr)互转公式

      本文链接:https://www.haomeiwen.com/subject/fygwxqtx.html