色彩空間轉換:RGB轉換為Lab(續)

來源:互聯網
上載者:User

1. RGB轉換為Lab的原理[1]

RGB=>Lab

X = [0.4339100.376220 0.189860] * [R/255]

Y = [0.2126490.715169 0.072182] * [G/255]

Z = [0.0177560.109478 0.872915] * [B/255]

L的計算:

如果 Y>0.008856

    L = 116*Y*(1/3) ;

否則

    L = 903.3*Y.

a, b的計算:

 a =500*(f(X)-f(Y))

b =200*(f(Y)-f(Z))

其中,

如果 t>0.008856

     f(t)=t*(1/3);

否則

     f(t)=7.787*t+16/116.

 

2. Matlab實現的代碼(摘自[2])

function [L,a,b] =RGB2Lab(R,G,B)

% function [L, a,b] = RGB2Lab(R, G, B)

% RGB2Lab takesmatrices corresponding to Red, Green, and Blue, and

% transforms theminto CIELab.  This transform is based onITU-R

%Recommendation  BT.709 using the D65white point reference.

% The error intransforming RGB -> Lab -> RGB is approximately

% 10^-5.  RGB values can be either between 0 and 1 orbetween 0 and 255. 

% By Mark Ruzonfrom C code by Yossi Rubner, 23 September 1997.

% Updated forMATLAB 5 28 January 1998.

 

if (nargin == 1)

  B = double(R(:,:,3));

  G = double(R(:,:,2));

  R = double(R(:,:,1));

end

 

if ((max(max(R))> 1.0) | (max(max(G)) > 1.0) | (max(max(B)) > 1.0))

  R = R/255;

  G = G/255;

  B = B/255;

end

 [M, N] = size(R);

s = M*N;

 % Set a threshold

T = 0.008856;

 RGB =[reshape(R,1,s); reshape(G,1,s); reshape(B,1,s)];

 % RGB to XYZ

MAT = [0.4124530.357580 0.180423;

       0.212671 0.715160 0.072169;

       0.019334 0.119193 0.950227];

XYZ = MAT * RGB;

X = XYZ(1,:) /0.950456;

Y = XYZ(2,:);

Z = XYZ(3,:) /1.088754;

XT = X > T;

YT = Y > T;

ZT = Z > T;

fX = XT .*X.^(1/3) + (~XT) .* (7.787 .* X + 16/116);

% Compute L

Y3 = Y.^(1/3);

fY = YT .* Y3 +(~YT) .* (7.787 .* Y + 16/116);

L  = YT .* (116 * Y3 - 16.0) + (~YT) .* (903.3 *Y);

fZ = ZT .*Z.^(1/3) + (~ZT) .* (7.787 .* Z + 16/116);

% Compute a and b

a = 500 * (fX -fY);

b = 200 * (fY -fZ);

 

L = reshape(L, M,N);

a = reshape(a, M,N);

b = reshape(b, M,N);

if ((nargout == 1)| (nargout == 0))

  L = cat(3,L,a,b);

end

註:這裡L,a,b的計算有點巧妙,利用bool型變數將條件陳述式綜合在一條式子裡。

參考資料:

[1] OpenCV手冊.chm 中的“CvtColor”部分.

[2] http://www.mathworks.com/matlabcentral/fileexchange/22274-hill-climbing-color-image-segmentation/content/RGB2Lab.m

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.