一篇文章搞清楚編程中YUV和RGB間的相互轉換

YUV/RGB色彩空間的相互轉換在算法上是很簡單的,都是線性變換。但是對相關領域不熟悉的同學在第一次着手做時,往往會非常迷惑,因爲網上的資料往往帶着各種相似卻不相同的術語,比如YUV,YCbCr,Y′CbCr,BT601/709/2020,full-range,studio-swing等等,不同術語描述的轉換公式和常數又都不一樣,給選擇帶來極大困擾。本文的目的就是儘可能簡單明瞭地說清楚這些術語表示的意義,讓我們能夠正確選擇轉換用的公式,甚至可以根據需求自行推導。

着急的或者圖方便的同學可以直接拉到本文最下面去查公式
YUV相關的術語。

YUV是一大類色彩空間的統稱。由沒有經過伽馬矯正的RGB信號轉化來的YUV色彩空間,模擬信號的稱作YCC,數字信號的稱作YCbCr;由經過了伽馬矯正的RGB信號(記作R′G′B′)轉化來的YUV色彩空間,模擬信號的稱作Y′PbPr,數字信號的稱作Y′CbCr。同時,作爲模擬信號的RGB和R′G′B′對應的數字信號記作RdGdBd和R′dG′dB′d:

YUV
無伽馬矯正 有伽馬矯正
模擬信號 YCC Y′PbPr
數字信號 YCbCr Y′CbCr
RGB
無伽馬矯正 有伽馬矯正
模擬信號 RGB R′G′B′
數字信號 RdGdBd R′dG′dB′d

而在計算機程序中所進行的YUV/RGB轉換,大部分情況都是指Y′CbCr和R′dG′dB′d間的轉換。在Y′CbCr的三個分量中,Y′被稱作luma,即進行過伽馬校正過的亮度值。另外的兩個分量Cb表示blue-difference,即亮度與藍色間的差別;Cr表示red-difference,即亮度與紅色間的差別。Cb和Cr統稱爲chroma,即色度。

Y′CbCr和R′dG′dB′d間的轉換,是由Y′PbPr和R′G′B′間的轉換推導而來的。大體思路是R′dG′dB′d<=>R′G′B′<=>Y′PbPr<=>Y′CbCr。接下來從左到右分步介紹。

R′dG′dB′d<=>R′G′B′

首先是數字信號的RGB和模擬信號間的相互轉換,模擬信號RGB三個分量範圍是[0,1],數字信號是[0,255],相互之間的轉換就是線性拉伸,因此:

let f=1255[RGB]=[RDGDBD]f[RDGDBD]=[RGB]1f \mathit{let}~ f = \frac{1}{255} \\ \begin{bmatrix} R'\\ G'\\ B' \end{bmatrix}= \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \cdot f \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} R'\\ G'\\ B' \end{bmatrix} \cdot \frac{1}{f}

R′G′B′<=>Y′PbPr

國際標準ITU-R定義了其所推薦的模擬信號Y′PbPr色彩空間,以及它和模擬信號的R′G′B′色彩空間的相互轉換。隨着技術進步,ITU-R標準推出了BT.601,BT.709,BT.2020等版本,其中BT.601用於SDTV,BT.709用於HDTV,BT.2020用於UHDTV。
各版本的轉換公式都是相同的,即向量(R′, G′, B′)乘以一個3x3的轉換矩陣M(後文簡稱M)便得出(Y′, Pb, Pr),而一個(Y′, Pb, Pr)向量乘以M的逆矩陣則得到對應的(R′, G′, B′)向量。R′G′B′三個分量的範圍都是[0, 1],乘以M後得到的luma的範圍是[0, 1],chroma的範圍則是[-0.5, +0.5]:

[YPBPR]=M[RGB][RGB]=M1[YPBPR] \begin{bmatrix} Y'\\ P_{B}\\ P_{R} \end{bmatrix} =M\cdot \begin{bmatrix} R'\\ G'\\ B' \end{bmatrix} \\ \begin{bmatrix} R'\\ G'\\ B' \end{bmatrix} =M^{-1}\cdot \begin{bmatrix} Y'\\ P_{B}\\ P_{R} \end{bmatrix}

對於矩陣M,ITU-R標準定義了Kr,Kg,Kb三個量,由這三個量就可以推導出M和它的逆矩陣:

M=[KRKGKB12KR1KB12KG1KB121212KG1KR12KB1KR]M1=[1022KR1KBKG(22KB)KRKG(22KR)122KB0] M= \begin{bmatrix} K_{R} & K_{G} & K_{B}\\ -\frac{1}{2}\cdot \frac{K_{R}}{1-K_{B}} & -\frac{1}{2}\cdot \frac{K_{G}}{1-K_{B}} & \frac{1}{2}\\ \frac{1}{2} & -\frac{1}{2}\cdot \frac{K_{G}}{1-K_{R}} & -\frac{1}{2}\cdot \frac{K_{B}}{1-K_{R}} \end{bmatrix} \\ M^{-1}= \begin{bmatrix} 1 & 0 & 2-2\cdot K_{R}\\ 1 & -\frac{K_{B}}{K_{G}}\cdot \left ( 2-2\cdot K_{B} \right ) & -\frac{K_{R}}{K_{G}}\cdot \left ( 2-2\cdot K_{R} \right )\\ 1 & 2-2\cdot K_{B} & 0 \end{bmatrix}

同時由於Kr + Kg + Kb = 1必須恆成立,因此只要定義其中兩個值即可:

Kr Kb
BT.601 0.299 0.114
BT.709 0.2126 0.0722
BT.2020 0.2627 0.0593
Y′PbPr<=>Y′CbCr

ITU-R標準同樣定義了Y′CbCr,同時其他需要使用數字YUV信號的標準也可自行定義,一般都需要將Y′PbPr進行拉伸和移位,使得新的三個分量落在對應位深度所表示的取值範圍(對8bit位深度,取值範圍即[0, 255])內的某一部分中。由此引出了range(也叫swing,後文統一稱爲range)的概念。

ITU-R標準定義了8bit位深度的Y′CbCr:Y′PbPr首先要笛卡爾乘scale向量(219, 224, 224),然後加上offset向量(16, 128, 128)。求出的Y′CbCr中luma範圍是[16, 235],chroma的範圍是[16, 240]。這樣的range由於沒有佔滿[0, 255]的取值範圍,被稱爲narrow range(也叫mpeg range,video range,studio range等等,後文統一爲narrow range);而對於需要佔滿整個位深度取值範圍的Y′CbCr,Y′PbPr首先要笛卡爾乘scale向量(255, 255, 255),然後加上offset向量(0, 128, 128)。這樣一來Y′CbCr三個分量取值範圍都落在了[0, 255],該range被成爲full range。

從Y′CbCr轉換爲Y′PbPr的過程是上述過程的反轉,即先減去offset向量,再笛卡爾除scale向量。

let Scale={[219224224]Ton narrow range[255255255]Ton full rangelet Offset={[16128128]Ton narrow range[0128128]Ton full range[YCBCR]=[YPBPR]×Scale+Offset[YPBPR]=([YCBCR]Offset)×Scale1 \mathit{let}~\mathit{Scale}= \left\{\begin{matrix} \begin{bmatrix} 219 & 224 & 224 \end{bmatrix}^{T} & \mathit{on~narrow~range}\\ \begin{bmatrix} 255 & 255 & 255 \end{bmatrix}^{T} & \mathit{on~full~range} \end{matrix}\right. \\ \mathit{let}~\mathit{Offset}= \left\{\begin{matrix} \begin{bmatrix} 16 & 128 & 128 \end{bmatrix}^{T} & \mathit{on~narrow~range}\\ \begin{bmatrix} 0 & 128 & 128 \end{bmatrix}^{T} & \mathit{on~full~range} \end{matrix}\right. \\ \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} Y'\\ P_{B}\\ P_{R} \end{bmatrix} \times \mathit{Scale} + \mathit{Offset} \\ \begin{bmatrix} Y'\\ P_{B}\\ P_{R} \end{bmatrix}= \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \mathit{Offset} \right ) \times \mathit{Scale}^{-1}

相互轉換最後需要進行四捨五入和邊界剪裁(round and clamp),因此不可避免地會造成一定程度上的精度損失。也有人爲此對轉換矩陣進行了微調,但無法徹底避免。

總體流程

綜合上面三個步驟,可得到R′dG′dB′d<=>Y′CbCr的總體流程:

let f=1255let Scale={[219224224]Ton narrow range[255255255]Ton full rangelet Offset={[16128128]Ton narrow range[0128128]Ton full range[YCBCR]=Offset+Mf[RDGDBD]×Scale[RDGDBD]=1fM1([YCBCR]Offset)×Scale1 \mathit{let}~ f = \frac{1}{255} \\ \mathit{let}~\mathit{Scale}= \left\{\begin{matrix} \begin{bmatrix} 219 & 224 & 224 \end{bmatrix}^{T} & \mathit{on~narrow~range}\\ \begin{bmatrix} 255 & 255 & 255 \end{bmatrix}^{T} & \mathit{on~full~range} \end{matrix}\right. \\ \mathit{let}~\mathit{Offset}= \left\{\begin{matrix} \begin{bmatrix} 16 & 128 & 128 \end{bmatrix}^{T} & \mathit{on~narrow~range}\\ \begin{bmatrix} 0 & 128 & 128 \end{bmatrix}^{T} & \mathit{on~full~range} \end{matrix}\right. \\ \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \mathit{Offset} + M \cdot f \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \times \mathit{Scale} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \frac{1}{f} \cdot M^{-1} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \mathit{Offset} \right ) \times \mathit{Scale}^{-1}

在實際計算中,會把f、M、scale結合到一起成爲一個新矩陣,這樣可以省略若干計算步驟。可以看到在full range下f和scale相互抵消。

同時,這裏也給出簡單的整數的近似公式,基本上就是把f、M、scale結合而成的矩陣各項乘以256取整,然後矩陣乘法的計算結果加128再右移8位(相當於定點數rounding)。 最後同樣要經行clamp(公式中省略了):

let M=int{256Scale×Mf}let M=int{256Scale1×1fM1}[YCBCR]=Offset+(M[RDGDBD]+[128128128])8[RDGDBD]=(M([YCBCR]Offset)+[128128128])8 \mathit{let} ~M'= \mathit{int} \left \{ 256 \cdot \mathit{Scale} \times M \cdot f \right \} \\ \mathit{let} ~M''= \mathit{int} \left \{ 256 \cdot \mathit{Scale}^{-1} \times \frac{1}{f} \cdot M^{-1} \right \} \\ \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \mathit{Offset} + \left ( M' \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} + \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( M'' \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \mathit{Offset} \right ) + \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8

至此,我們可以推導出所有的情況下的公式。

那麼公式的選擇對編程到底有怎樣的影響呢?舉一個最簡單的例子:現在需要用FFmpeg載入一個視頻然後用OpenGL渲染,因此必須轉換爲RGB空間。如果視頻是BT.709 narrow range的,編程中使用BT.601 full range的公式就會出錯,渲染的色彩空間會出現扭曲。不過視頻的信息中一般都會標明自己採用的色彩空間和range,FFmpeg中也有編程接口可以獲取這些信息,所以這種用況下一定不要把轉換公式寫死,而要動態地判斷。

着急的或者圖方便的同學可以忽略上面所有內容,只看下面的公式列表就行(只適用於8bit)

BT.601 full range

[YCBCR]=[0128128]+[0.2990000.5870000.1140000.1687360.3312640.5000000.5000000.4186880.081312][RDGDBD][RDGDBD]=[1.0000000.0000001.4020001.0000000.3441360.7141361.0000001.7720000.000000]([YCBCR][0128128]) \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix}+ \begin{bmatrix} 0.299000 & 0.587000 & 0.114000\\ -0.168736 & -0.331264 & 0.500000\\ 0.500000 & -0.418688 & -0.081312 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} 1.000000 & 0.000000 & 1.402000\\ 1.000000 & -0.344136 & -0.714136\\ 1.000000 & 1.772000 & 0.000000 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix} \right ) \\

BT.601 full range 整數近似

[YCBCR]=[0128128]+([7715029438512812810721][RDGDBD]+[128128128])8[RDGDBD]=([2560359256881832564540]([YCBCR][0128128])+[128128128])8 \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix}+ \left ( \begin{bmatrix} 77 & 150 & 29\\ -43 & -85 & 128\\ 128 & -107 & -21 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( \begin{bmatrix} 256 & 0 & 359\\ 256 & -88 & -183\\ 256 & 454 & 0 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix} \right )+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\

BT.601 narrow range

[YCBCR]=[16128128]+[0.2567880.5156390.1001410.1449140.2909930.4392160.4294120.3677880.071427][RDGDBD][RDGDBD]=[1.1643840.0000001.5960271.1643840.3917620.8129681.1643842.0172320.000000]([YCBCR][16128128]) \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix}+ \begin{bmatrix} 0.256788 & 0.515639 & 0.100141\\ -0.144914 & -0.290993 & 0.439216\\ 0.429412 & -0.367788 & -0.071427 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} 1.164384 & 0.000000 & 1.596027\\ 1.164384 & -0.391762 & -0.812968\\ 1.164384 & 2.017232 & 0.000000 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix} \right ) \\

BT.601 narrow range 整數近似

[YCBCR]=[16128128]+([661322637741121109418][RDGDBD]+[128128128])8[RDGDBD]=([29804092981002082985160]([YCBCR][16128128])+[128128128])8 \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix}+ \left ( \begin{bmatrix} 66 & 132 & 26\\ -37 & -74 & 112\\ 110 & -94 & -18 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( \begin{bmatrix} 298 & 0 & 409\\ 298 & -100 & -208\\ 298 & 516 & 0 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix} \right )+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\

BT.709 full range

[YCBCR]=[0128128]+[0.2126000.7152000.0722000.1145720.3854280.5000000.5000000.4541530.045847][RDGDBD][RDGDBD]=[1.0000000.0000001.5748001.0000000.1873240.4681241.0000001.8556000.000000]([YCBCR][0128128]) \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix}+ \begin{bmatrix} 0.212600 & 0.715200 & 0.072200\\ -0.114572 & -0.385428 & 0.500000\\ 0.500000 & -0.454153 & -0.045847 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} 1.000000 & 0.000000 & 1.574800\\ 1.000000 & -0.187324 & -0.468124\\ 1.000000 & 1.855600 & 0.000000 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix} \right ) \\

BT.709 full range 整數近似

[YCBCR]=[0128128]+([5418318299912812811612][RDGDBD]+[128128128])8[RDGDBD]=([2560403256481202564750]([YCBCR][0128128])+[128128128])8 \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix}+ \left ( \begin{bmatrix} 54 & 183 & 18\\ -29 & -99 & 128\\ 128 & -116 & -12 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( \begin{bmatrix} 256 & 0 & 403\\ 256 & -48 & -120\\ 256 & 475 & 0 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix} \right )+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\

BT.709 narrow range

[YCBCR]=[16128128]+[0.1825860.6282540.0634230.0983970.3385720.4392160.4294120.3989420.040274][RDGDBD][RDGDBD]=[1.1643840.0000001.7927411.1643840.2132490.5329091.1643842.1124020.000000]([YCBCR][16128128]) \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix}+ \begin{bmatrix} 0.182586 & 0.628254 & 0.063423\\ -0.098397 & -0.338572 & 0.439216\\ 0.429412 & -0.398942 & -0.040274 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} 1.164384 & 0.000000 & 1.792741\\ 1.164384 & -0.213249 & -0.532909\\ 1.164384 & 2.112402 & 0.000000 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix} \right ) \\

BT.709 narrow range 整數近似

[YCBCR]=[16128128]+([4716116258711211010210][RDGDBD]+[128128128])8[RDGDBD]=([2980459298551362985410]([YCBCR][16128128])+[128128128])8 \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix}+ \left ( \begin{bmatrix} 47 & 161 & 16\\ -25 & -87 & 112\\ 110 & -102 & -10 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( \begin{bmatrix} 298 & 0 & 459\\ 298 & -55 & -136\\ 298 & 541 & 0 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix} \right )+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\

BT.2020 full range

[YCBCR]=[0128128]+[0.2627000.6780000.0593000.1396300.3603700.5000000.5000000.4597860.040214][RDGDBD][RDGDBD]=[1.0000000.0000001.4746001.0000000.1645530.5713531.0000001.8814000.000000]([YCBCR][0128128]) \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix}+ \begin{bmatrix} 0.262700 & 0.678000 & 0.059300\\ -0.139630 & -0.360370 & 0.500000\\ 0.500000 & -0.459786 & -0.040214 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} 1.000000 & 0.000000 & 1.474600\\ 1.000000 & -0.164553 & -0.571353\\ 1.000000 & 1.881400 & 0.000000 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix} \right ) \\

BT.2020 full range 整數近似

[YCBCR]=[0128128]+([6717415369212812811810][RDGDBD]+[128128128])8[RDGDBD]=([2560377256421462564820]([YCBCR][0128128])+[128128128])8 \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix}+ \left ( \begin{bmatrix} 67 & 174 & 15\\ -36 & -92 & 128\\ 128 & -118 & -10 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( \begin{bmatrix} 256 & 0 & 377\\ 256 & -42 & -146\\ 256 & 482 & 0 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 0\\ 128\\ 128 \end{bmatrix} \right )+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\

BT.2020 narrow range

[YCBCR]=[16128128]+[0.2256130.5955760.0520910.1199180.3165600.4392160.4294120.4038900.035325][RDGDBD][RDGDBD]=[1.1643840.0000001.6786741.1643840.1873260.6504241.1643842.1417720.000000]([YCBCR][16128128]) \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix}+ \begin{bmatrix} 0.225613 & 0.595576 & 0.052091\\ -0.119918 & -0.316560 & 0.439216\\ 0.429412 & -0.403890 & -0.035325 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix} \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \begin{bmatrix} 1.164384 & 0.000000 & 1.678674\\ 1.164384 & -0.187326 & -0.650424\\ 1.164384 & 2.141772 & 0.000000 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix} \right ) \\

BT.2020 narrow range 整數近似

[YCBCR]=[16128128]+([581521331811121101039][RDGDBD]+[128128128])8[RDGDBD]=([2980430298481672985480]([YCBCR][16128128])+[128128128])8 \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}= \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix}+ \left ( \begin{bmatrix} 58 & 152 & 13\\ -31 & -81 & 112\\ 110 & -103 & -9 \end{bmatrix} \cdot \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\ \begin{bmatrix} R'_{D}\\ G'_{D}\\ B'_{D} \end{bmatrix}= \left ( \begin{bmatrix} 298 & 0 & 430\\ 298 & -48 & -167\\ 298 & 548 & 0 \end{bmatrix} \cdot \left ( \begin{bmatrix} Y'\\ C_{B}\\ C_{R} \end{bmatrix}- \begin{bmatrix} 16\\ 128\\ 128 \end{bmatrix} \right )+ \begin{bmatrix} 128\\ 128\\ 128 \end{bmatrix} \right ) \gg 8 \\

參考資料:

[1] 維基百科
[2] BT.601標準spec

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章