RGB空间与HSL空间转换matlab代码
2014-10-20 22:08
369 查看
关于RGB与HSL空间之间转换的原理,在网上很多,也很详细,这里就不做介绍了。
直接给出MATLAB代码。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RGB空间转换到HSL空间
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [H,S,L,hsl]=rgb2hsl(img)
rgb=im2double(img);
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
[m,n]=size(r);
%% 求 L %%
maxcolor=max(max(r,g),b);
mincolor=min(min(r,g),b);
L=(maxcolor+mincolor)/2;
H=zeros(m,n);
S=zeros(m,n);
%% 求 S %%
for i=1:m
for j=1:n
if maxcolor(i,j)==mincolor(i,j)
S(i,j)=0;
else
if L(i,j)<=0.5
S(i,j)=(maxcolor(i,j)-mincolor(i,j))/(2*L(i,j));
else
S(i,j)=(maxcolor(i,j)-mincolor(i,j))/(2-2*L(i,j));
end
end
end
end
%% 求 H %%
for i=1:m
for j=1:n
if maxcolor(i,j)==mincolor(i,j)
H(i,j)=0;
else if r(i,j)==maxcolor(i,j)
if g(i,j)>=b(i,j)
H(i,j)=60*(g(i,j)-b(i,j))/(maxcolor(i,j)-mincolor(i,j));
else
H(i,j)=60*(g(i,j)-b(i,j))/(maxcolor(i,j)-mincolor(i,j))+360;
end
else if g(i,j)==maxcolor(i,j)
H(i,j)=120+60*(b(i,j)-r(i,j))/(maxcolor(i,j)-mincolor(i,j));
else
H(i,j)=240+60*(r(i,j)-g(i,j))/(maxcolor(i,j)-mincolor(i,j));
end
end
end
end
end
%%
hsl=cat(3,H,S,L);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%HSL空间转换到RGB空间:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [R,G,B,rgb]=hsl2rgb(img_hsl)
hsl=img_hsl;
H=hsl(:,:,1);
S=hsl(:,:,2);
L=hsl(:,:,3);
[m,n]=size(H);
R=zeros(m,n);
G=zeros(m,n);
B=zeros(m,n);
%% 求 R %%
for i=1:m
for j=1:n
if S(i,j)==0
R(i,j)=L(i,j);
else if L(i,j)<0.5
temp2=L(i,j)*(1.0+S(i,j));
else
temp2=L(i,j)+S(i,j)-L(i,j)*S(i,j);
end
end
temp1=2*L(i,j)-temp2;
h=H(i,j)/360;
temp3=h+1/3;
if temp3<0
temp3=temp+1;
end
if temp3>1
temp3=temp3-1;
end
if 6*temp3<1
R(i,j)=temp1+(temp2-temp1)*6*temp3;
else if 2*temp3<1
R(i,j)=temp2;
else if 3*temp3<2
R(i,j)=temp1+(temp2-temp1)*(2/3-temp3)*6;
else
R(i,j)=temp1;
end
end
end
end
end
%% 求 G %%
for i=1:m
for j=1:n
if S(i,j)==0
G(i,j)=L(i,j);
else if L(i,j)<0.5
temp2=L(i,j)*(1.0+S(i,j));
else
temp2=L(i,j)+S(i,j)-L(i,j)*S(i,j);
end
end
temp1=2*L(i,j)-temp2;
h=H(i,j)/360;
temp3=h;
if temp3<0
temp3=temp+1;
end
if temp3>1
temp3=temp3-1;
end
if 6*temp3<1
G(i,j)=temp1+(temp2-temp1)*6*temp3;
else if 2*temp3<1
G(i,j)=temp2;
else if 3*temp3<2
G(i,j)=temp1+(temp2-temp1)*(2/3-temp3)*6;
else
G(i,j)=temp1;
end
end
end
end
end
%% 求 B %%
for i=1:m
for j=1:n
if S(i,j)==0
B(i,j)=L(i,j);
else if L(i,j)<0.5
temp2=L(i,j)*(1.0+S(i,j));
else
temp2=L(i,j)+S(i,j)-L(i,j)*S(i,j);
end
end
temp1=2*L(i,j)-temp2;
h=H(i,j)/360;
temp3=h-1/3;
if temp3<0
temp3=temp3+1;
end
if temp3>1
temp3=temp3-1;
end
if 6*temp3<1
B(i,j)=temp1+(temp2-temp1)*6*temp3;
else if 2*temp3<1
B(i,j)=temp2;
else if 3*temp3<2
B(i,j)=temp1+(temp2-temp1)*(2/3-temp3)*6;
else
B(i,j)=temp1;
end
end
end
end
end
%%
rgb=cat(3,R,G,B);
直接给出MATLAB代码。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RGB空间转换到HSL空间
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [H,S,L,hsl]=rgb2hsl(img)
rgb=im2double(img);
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
[m,n]=size(r);
%% 求 L %%
maxcolor=max(max(r,g),b);
mincolor=min(min(r,g),b);
L=(maxcolor+mincolor)/2;
H=zeros(m,n);
S=zeros(m,n);
%% 求 S %%
for i=1:m
for j=1:n
if maxcolor(i,j)==mincolor(i,j)
S(i,j)=0;
else
if L(i,j)<=0.5
S(i,j)=(maxcolor(i,j)-mincolor(i,j))/(2*L(i,j));
else
S(i,j)=(maxcolor(i,j)-mincolor(i,j))/(2-2*L(i,j));
end
end
end
end
%% 求 H %%
for i=1:m
for j=1:n
if maxcolor(i,j)==mincolor(i,j)
H(i,j)=0;
else if r(i,j)==maxcolor(i,j)
if g(i,j)>=b(i,j)
H(i,j)=60*(g(i,j)-b(i,j))/(maxcolor(i,j)-mincolor(i,j));
else
H(i,j)=60*(g(i,j)-b(i,j))/(maxcolor(i,j)-mincolor(i,j))+360;
end
else if g(i,j)==maxcolor(i,j)
H(i,j)=120+60*(b(i,j)-r(i,j))/(maxcolor(i,j)-mincolor(i,j));
else
H(i,j)=240+60*(r(i,j)-g(i,j))/(maxcolor(i,j)-mincolor(i,j));
end
end
end
end
end
%%
hsl=cat(3,H,S,L);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%HSL空间转换到RGB空间:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [R,G,B,rgb]=hsl2rgb(img_hsl)
hsl=img_hsl;
H=hsl(:,:,1);
S=hsl(:,:,2);
L=hsl(:,:,3);
[m,n]=size(H);
R=zeros(m,n);
G=zeros(m,n);
B=zeros(m,n);
%% 求 R %%
for i=1:m
for j=1:n
if S(i,j)==0
R(i,j)=L(i,j);
else if L(i,j)<0.5
temp2=L(i,j)*(1.0+S(i,j));
else
temp2=L(i,j)+S(i,j)-L(i,j)*S(i,j);
end
end
temp1=2*L(i,j)-temp2;
h=H(i,j)/360;
temp3=h+1/3;
if temp3<0
temp3=temp+1;
end
if temp3>1
temp3=temp3-1;
end
if 6*temp3<1
R(i,j)=temp1+(temp2-temp1)*6*temp3;
else if 2*temp3<1
R(i,j)=temp2;
else if 3*temp3<2
R(i,j)=temp1+(temp2-temp1)*(2/3-temp3)*6;
else
R(i,j)=temp1;
end
end
end
end
end
%% 求 G %%
for i=1:m
for j=1:n
if S(i,j)==0
G(i,j)=L(i,j);
else if L(i,j)<0.5
temp2=L(i,j)*(1.0+S(i,j));
else
temp2=L(i,j)+S(i,j)-L(i,j)*S(i,j);
end
end
temp1=2*L(i,j)-temp2;
h=H(i,j)/360;
temp3=h;
if temp3<0
temp3=temp+1;
end
if temp3>1
temp3=temp3-1;
end
if 6*temp3<1
G(i,j)=temp1+(temp2-temp1)*6*temp3;
else if 2*temp3<1
G(i,j)=temp2;
else if 3*temp3<2
G(i,j)=temp1+(temp2-temp1)*(2/3-temp3)*6;
else
G(i,j)=temp1;
end
end
end
end
end
%% 求 B %%
for i=1:m
for j=1:n
if S(i,j)==0
B(i,j)=L(i,j);
else if L(i,j)<0.5
temp2=L(i,j)*(1.0+S(i,j));
else
temp2=L(i,j)+S(i,j)-L(i,j)*S(i,j);
end
end
temp1=2*L(i,j)-temp2;
h=H(i,j)/360;
temp3=h-1/3;
if temp3<0
temp3=temp3+1;
end
if temp3>1
temp3=temp3-1;
end
if 6*temp3<1
B(i,j)=temp1+(temp2-temp1)*6*temp3;
else if 2*temp3<1
B(i,j)=temp2;
else if 3*temp3<2
B(i,j)=temp1+(temp2-temp1)*(2/3-temp3)*6;
else
B(i,j)=temp1;
end
end
end
end
end
%%
rgb=cat(3,R,G,B);
相关文章推荐
- 颜色空间RGB 与 HSL 空间转换代码
- 颜色空间RGB与HSV(HSL)的转换
- RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)
- 颜色空间转换:RGB转换为Lab(Matlab)
- 颜色空间RGB与HSV(HSL)的转换
- 利用Matlab将RGB图像转换到HSI空间
- matlab实现RGB与HSV(HSB)、HSL和HSI的色彩空间互转
- 颜色空间RGB与HSV(HSL)的转换
- 【OpenCV】颜色空间RGB与HSV(HSL)的转换
- 颜色空间RGB与HSV(HSL)的转换
- RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)
- 颜色空间RGB与HSV(HSL)的转换
- 颜色空间RGB与HSV(HSL)的转换
- RGB与Lab色彩空间相互转换c++代码
- 颜色空间RGB与HSV(HSL)的转换
- 【OpenCV】颜色空间RGB与HSV(HSL)的转换
- RGB颜色空间到HSV和YUV的转换关系(含VC代码)
- 颜色空间RGB与HSV(HSL)的转换
- RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)
- RGB颜色空间与Lab颜色空间区别与联系(附转换代码)