matlab转换hsl,RGB空间与HSL空间转换matlab代码

关于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);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值