# 色彩空间Lab和RGB互转的Matlab代码

RGB2Lab:

function [L,a,b] = RGB2Lab(R,G,B)
%RGB2LAB Convert an image from RGB to CIELAB
%
% function [L, a, b] = RGB2Lab(R, G, B)
% function [L, a, b] = RGB2Lab(I)
% function I = RGB2Lab(...)
%
% RGB2Lab takes red, green, and blue matrices, or a single M x N x 3 image,
% and returns an image in the CIELAB color space.  RGB values can be
% either between 0 and 1 or between 0 and 255.  Values for L are in the
% range [0,100] while a and b are roughly in the range [-110,110].  The
% output is of type double.
%
% This transform is based on ITU-R Recommendation BT.709 using the D65
% white point reference. The error in transforming RGB -> Lab -> RGB is
% approximately 10^-5.
%

% By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
% Updated for MATLAB 5 28 January 1998.
% Updated for MATLAB 7 30 March 2009.

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 = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end

% Set a threshold
T = 0.008856;

[M, N] = size(R);
s = M * N;
RGB = [reshape(R,1,s); reshape(G,1,s); reshape(B,1,s)];

% RGB to XYZ
MAT = [0.412453 0.357580 0.180423;
0.212671 0.715160 0.072169;
0.019334 0.119193 0.950227];
XYZ = MAT * RGB;

% Normalize for D65 white point
X = XYZ(1,:) / 0.950456;
Y = XYZ(2,:);
Z = XYZ(3,:) / 1.088754;

XT = X > T;
YT = Y > T;
ZT = Z > T;

Y3 = Y.^(1/3);

fX = XT .* X.^(1/3) + (~XT) .* (7.787 .* X + 16/116);
fY = YT .* Y3 + (~YT) .* (7.787 .* Y + 16/116);
fZ = ZT .* Z.^(1/3) + (~ZT) .* (7.787 .* Z + 16/116);

L = reshape(YT .* (116 * Y3 - 16.0) + (~YT) .* (903.3 * Y), M, N);
a = reshape(500 * (fX - fY), M, N);
b = reshape(200 * (fY - fZ), M, N);

if nargout < 2
L = cat(3,L,a,b);
end
Lab2RGB

function [R, G, B] = Lab2RGB(L, a, b)
%LAB2RGB Convert an image from CIELAB to RGB
%
% function [R, G, B] = Lab2RGB(L, a, b)
% function [R, G, B] = Lab2RGB(I)
% function I = Lab2RGB(...)
%
% Lab2RGB takes L, a, and b double matrices, or an M x N x 3 double
% image, and returns an image in the RGB color space.  Values for L are in
% the range [0,100] while a* and b* are roughly in the range [-110,110].
% If 3 outputs are specified, the values will be returned as doubles in the
% range [0,1], otherwise the values will be uint8s in the range [0,255].
%
% This transform is based on ITU-R Recommendation BT.709 using the D65
% white point reference. The error in transforming RGB -> Lab -> RGB is
% approximately 10^-5.
%

% By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
% Updated for MATLAB 5 28 January 1998.
% Fixed a bug in conversion back to uint8 9 September 1999.
% Updated for MATLAB 7 30 March 2009.

if nargin == 1
b = L(:,:,3);
a = L(:,:,2);
L = L(:,:,1);
end

% Thresholds
T1 = 0.008856;
T2 = 0.206893;

[M, N] = size(L);
s = M * N;
L = reshape(L, 1, s);
a = reshape(a, 1, s);
b = reshape(b, 1, s);

% Compute Y
fY = ((L + 16) / 116) .^ 3;
YT = fY > T1;
fY = (~YT) .* (L / 903.3) + YT .* fY;
Y = fY;

% Alter fY slightly for further calculations
fY = YT .* (fY .^ (1/3)) + (~YT) .* (7.787 .* fY + 16/116);

% Compute X
fX = a / 500 + fY;
XT = fX > T2;
X = (XT .* (fX .^ 3) + (~XT) .* ((fX - 16/116) / 7.787));

% Compute Z
fZ = fY - b / 200;
ZT = fZ > T2;
Z = (ZT .* (fZ .^ 3) + (~ZT) .* ((fZ - 16/116) / 7.787));

% Normalize for D65 white point
X = X * 0.950456;
Z = Z * 1.088754;

% XYZ to RGB
MAT = [ 3.240479 -1.537150 -0.498535;
-0.969256  1.875992  0.041556;
0.055648 -0.204043  1.057311];

RGB = max(min(MAT * [X; Y; Z], 1), 0);

R = reshape(RGB(1,:), M, N);
G = reshape(RGB(2,:), M, N);
B = reshape(RGB(3,:), M, N);

if nargout < 2
R = uint8(round(cat(3,R,G,B) * 255));
end


• 本文已收录于以下专栏：

## rgb2lab代码转换

• superdont
• 2008年10月29日 09:39
• 5106

## 颜色空间转换：RGB转换为Lab（Matlab）

Convert RGB image to L*a*b*, assuming input image is sRGB.         rgb = imread('peppers.png');  ...
• chgm_456D
• 2012年10月10日 11:29
• 3375

## MATLAB图像处理_LAB与RGB颜色空间互转

• scottly1
• 2015年01月08日 08:41
• 3007

## rgb2lab程序matlab实现RGB到LAB转换

• 2013年05月08日 09:56
• 1KB
• 下载

## OPENCV3.0 函数学习2——cvtColor

1 .cvtColor cvtColor 是Opencv里的颜色空间转换函数，可以实现RGB颜色向HSV,HSI等颜色空间的转换，也可以转换为灰度图像。 函数参数说明  void cvtColo...
• banjiding8023
• 2016年08月08日 13:07
• 3794

## RGB与Lab色彩空间相互转换c++代码

• u013085897
• 2017年03月02日 10:05
• 2019

## photoshop中RGB与LAB空间的相互转换

• jackiesun
• 2015年09月02日 15:33
• 1960

## 从RGB到Lab色彩空间的转换

• cay22
• 2010年04月27日 16:59
• 10291

## RGB转为Lab空间

• shamaozi
• 2011年03月03日 16:26
• 14297

## RGB与Lab颜色空间互相转换

RGB与Lab颜色空间互相转换 1.Lab颜色空间        同RGB颜色空间相比（见博客《光与色的故事—颜色模型浅析》），Lab是一种不常用的色彩空间。它是在1931年国际照明委员会（CIE...
• lz0499
• 2017年08月17日 23:25
• 972

举报原因： 您举报文章：色彩空间Lab和RGB互转的Matlab代码 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)