CSC/CSM正本清源
本文系微信公眾號《大話成像》,知乎專欄《all in camera》原創文章
大話成像讀者QQ交流羣 :237427716 大話計算機視覺QQ交流羣:651805282
本站教學視頻大話成像之《數字成像系統》32講已上線淘寶,搜索關鍵字『大話成像』即可找到,或使用鏈接
PC用戶: https://item.taobao.com/item.htm?id=568925736204:
手機用戶可使用鏈接: 複製這條信息¥adMo0tqbc2N¥後打開手淘
請購買了視頻的讀者加入QQ羣:679050565,羣名稱:數字成像系統課程,驗證消息請輸入購買課程時候的個人ID。
從ISP RGB信號輸出到encoder,中間有一步色彩空間轉換(color space conversion),把信號從RGB變換成YCbCr,我們把這個模塊叫做CSC或者CSM。
一般ISP公司在tuning或者測試驗證的時候都只用RGB ,顯示也是隻用RGB,不會經過CSC,所以CSC的Setting有時候就會處在ISP和encoder兩家的邊界地帶,經常會因為兩邊色彩空間不一致而出現問題。造成這種不一致,有其歷史原因。
ISP本身不是國際標準化的主體,但是數字視頻是很長時間以來都有國際標準化的。1982年bt601/656標準建立,它第一次提出了ISP的基本標準化。
BT601把量化和色彩空間轉換做了標準化,BT656把數據格式化做了標準化。
BT601是針對標清電視提出的,這個標準本來的題目叫做:Studio encoding parameters of digital television for standard 4:3 and wide-screen 16:9 aspect ratios。
這個標準下:視頻解析度是:720 × 480 或者 720 × 576,採樣頻率是:13.5 MHz。
如果輸入為模擬RGB信號,那麼量化後的RGB信號按8bit數字信號表示為16到235的219個數。然後再用一個3x3 CSC矩陣把這個RGB信號變成YCbCr信號。
同樣別的標準:比如針對高清信號的bt709,針對超高清信號的BT.2020都有各自的規定,所以他們的色彩空間轉換也都不一樣,也就是不僅轉換後的圖像是不同的,從同一YCbCr圖像反變換到RGB也將不同。所以,ISP的CSC設定一定要遵循一個標準所推薦的變換公式,否則後邊的顯示或者編碼,反變換都無法對應正確。
有人梳理了bt601,bt709 的幾種常用的變換矩陣,如下。
function [Y,U,V]=rgb2yuv(R,G,B,yuvformat,convmtrx)
%Converts RGB to YUV
%[Y,U,V]=rgb2yuv(R,G,B,yuvformat)
%
%Input:
% R,G,B - R,G and B components of the frame
% yuvformat - YUV format [optional, default = YUV444_8]. Supported YUV
% formats are:
% YUV444_8 = 4:4:4 sampling, 8-bit precision (default)
% YUV420_8 = 4:2:0 sampling, 8-bit precision
% convmtrx - Conversion matrix [optional, default = BT709_l]. The
% following conversions ase defined (see in Notes for more
% details):
% BT601_f = ITU-R BT.601, RGB full [0...255] (in BT601_f.mat)
% BT601_219 = ITU-R BT.601, RGB limited [0...219] (in
% BT601_219.mat)
% BT601_l = ITU-R BT.601, RGB limited [16...235] (in BT601_l.mat)
% BT709_f = ITU-R BT.709, RGB limited [0...255] (in BT709_f.mat)
% BT709_l = ITU-R BT.709, RGB limited [16...235] (in BT709_l.mat)
% SMPTE_240M = SMPTE 240M (almost the same as Rec.709)
%
%Output:
% Y,U,V - Y,U and V components of the frame
%
%Uses:
% imresize.m - Matlab Image Processing Toolbox (when formats other than
% 4:4:4 used)
%
%Note:
% Note that a more correct term for what is here called YUV would be YCbCr,
% since it is used for YUV representation in digital domain. Also, the R, G
% and B components are actually non-linear because of gamma correction, and
% are more correctly denoted as R, G and B. YCbCr is expected to be in
% the range (below that is "footroom" range and above is "headroom"):
% Y = [16...235]
% Cb,Cr = [16...240]
%
% Some more details on the defined conversions follow.
% ITU-R BT.601 - for SD (720x576) and lower resolutions. Three versions are
% available:
% 1) RGB in full range [0...255], rgb2yuvT matrix:
% 0.257 0.504 0.098
% -0.148 -0.291 0.439
% 0.439 -0.368 -0.071
% yuvoffset = [16; 128; 128]
% (Resulting output range is
Y=[16...235];Cb=[16...240];Cr=[16...240]
% (Coefficients taken from [3],[4],[5],[6]. Integer implementation in [7])
%
% 2) RGB limited to [0...219], rgb2yuvT matrix:
% 0.299 0.587 0.114
% -0.173 -0.339 0.511
% 0.511 -0.428 -0.083
% yuvoffset = [16; 128; 128]
% (Resulting output range is
Y=[16...235];Cb=[16...240];Cr=[16...240])
% (Note that in [1] the coeffcients are rounded to the nearest integer,
% while the coefficients here are from [4] and [6]. If original signal is in
% range [16...235] then offset of 16 for Y signal is not necessary, e.g.
% definition from [6])
% 3) RGB limited to [16...235], rgb2yuvT matrix:
% 0.299 0.587 0.114
% -0.169 -0.331 0.500
% 0.500 -0.419 -0.081
% yuvoffset = [0; 128; 128]
% (Resulting output range is
Y=[16...235];Cb=[18.5...237.5];Cr=[18.5...237.5])
% (This conversion is also used in JPEG, which allows the input to be in the
% full [0...255] range, where the output is in range
Y=[0...255];% Cb=[0.5...255.5];Cr=[0.5...255.5])
% (Coefficients taken from [1],[3])
%
% ITU-R BT.709 - for HD resolutions (i.e. higher than SD). Two versions are
% available:
% 1) RGB in full range [0...255], rgb2yuvT matrix:
% 0.1826 0.6142 0.0620
% -0.1006 -0.3386 0.4392
% 0.4392 -0.3989 -0.0403
% yuvoffset = [16; 128; 128]
% (Resulting output range is
Y=[16...235];Cb=[16...240];Cr=[16...240])% (Coefficients taken from [5], less precise version in [6]. Appears to be a
% scaled version of the next one, where RGB is limited.)
%
% 2) RGB limited to [16...235], rgb2yuvT matrix:
% 0.2126 0.7152 0.0722
% -0.1146 -0.3854 0.5000
% 0.5000 -0.4542 -0.0468
% yuvoffset = [0; 128; 128]
% (Resulting output range is
Y=[16...235];Cb=[18.5...237.5];Cr=[18.5...237.5])% (Coefficients taken from [2]. The ones in [6] are slightly different, for no
% obvious reason)
%
前面追溯歷史的目的是說:這些標準都是基於當年的模擬信號所考慮的(模擬信號很難做準確低於16和高於235的信號,所以要留所謂的footroom和headroom,如果你現在的應用其輸入信號和顯示/處理設備都是數字設備,是沒有這種侷限的,就可以都是full range轉換(也是Jpeg encoder的典型格式),input RGB【0,255】,output YCbCr【0,255】。
Full range RGB2YCbCr變換公式:
反變換公式:
歡迎大家關注我們的公眾號大話成像和大話計算機視覺,並加入我們的兩個QQ交流羣交流一些工作學習中的問題 。
推薦閱讀: