颜色空间转换:RGB转换为Lab(续)

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





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值