计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式_bt2020 rgb转xyz-CSDN博客 (2024)

关于写这篇文章的原因:

  本人也是摸索了很长时间才弄懂其中的原理,里面涉及的知识点太多了,比如色彩空间,比如Gamma,里面还会涉及到很多的协议,比如BT601 BT709,BT2020,RP177等,一不小心就会写错,然后色彩可能就不准确了,可能偏白偏黑或者偏色

自己也踩过很多坑,然后把这些经验写出来,防止大家踩重复的坑,如果文章有不够严谨的地方,请及时指出。

不同的色彩空间的转换公式是不一样的

  YUV与RGB互转的公式有很多,不同的色彩空间的转换公式是不一样的,不同色彩空间的转换公式可以看我写的这篇文章:计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式 - 简书

YUV有多种表现形式

  除了色彩空间,还需要注意YUV的多种表现形式,比如:

  YUV: YUV是一种模拟型号, Y∈ [0,1] U,V∈[-0.5,0.5]

  YCbCr:也叫YCC或者Y'CbCr YCbCr 是数字信号, 它包含两种形式, 分别为TV range 和 full range, TV range 主要是广播电视采用的标准, full range 主要是pc 端采用的标准, 所以full range 有时也叫 pc range

      TV range的各个分量的范围为:YUVY∈[16,235] Cb∈[16-240] Cr∈[16-240]

      full range的各个分量的范围均为: 0-255

  我们平时接触到的绝大多数都是YCbCr (tv range), ffmpeg 解码出来的数据绝大多数也是这个, 虽然ffmpeg里面将它的格式描述成YUV420P ,实际上它是YCbCr420p tvrange

  YUV转tvrange: Y' = 219.0*Y + 16 ; Cb = U * 224.0 + 128;Cr = V * 224.0 + 128;

关于为什么要将YUV量化为tv range 16-235 ?

  以下是维基百科摘抄的一段, 意思是tv range是为了解决滤波(模数转换)后的过冲现象,

  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.)

关于如何判断像素格式是否为tvrange Y(16-235)?

  1. 常见的一些解码帧结构体里面有color_range参数,如果为MPEG或者 LIMITED则表示为tv_range

  2. 在完全黑画面的时候打印出图像的Y数据, 如果Y=16左右 说明YCbCr 为tv range ,如果Y=0左右 说明YCbCr为 full range

以下是我推导的BT601与BT2020的公式,包括各种形式,比如浮点整形 等等。

一、 公式:基于BT.601-6 (NTSC色域)

计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式_bt2020 rgb转xyz-CSDN博客 (1)

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

通过坐标图我们可以看到UV(YUV六面体投影到UV坐标系)是一个旋转了一定角度的八边形(实际上是一个六面体在UV平面的投影),U越大蓝色越蓝,V越大,红色越红。

以下具体为各种转换公式(该转换公式基于BT601 ,NTSC色域 )

1.小数形式,YUV ( U∈[-0.5-0.5] , R,G,B∈[0,1] )

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. 量化为 tv range 后的公式( Y∈(16,235) U/V∈(16,240) )

  [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

4 tv range的公式写成整数的形式(减小计算量) ( 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量化 与 非量化 互转

tvrange 转fullrange

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

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

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

full range转tvrange U(-128-127) ==> U(16-240)

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

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

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

二、.Rec2020 (BT2020) 下的YUV与RGB转换公式 (写成矩阵形式)

计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式_bt2020 rgb转xyz-CSDN博客 (2)

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

1. BT2020 文档上的公式

计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式_bt2020 rgb转xyz-CSDN博客 (3)

计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式_bt2020 rgb转xyz-CSDN博客 (4)

即:

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;

矩阵形式

YUVRGB互转公式

[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

YCbCr(tv range) RGB互转公式

  [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

tv range互转公式写成整数形式

  [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

计算不同色域下的RGB与YUV 以及 RGB与XYZ互转公式_bt2020 rgb转xyz-CSDN博客 (2024)

References

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5517

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.