您的位置:首页 > 编程语言 > MATLAB

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);  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rgb hsl matlab