There is no image HSL channel support in Matlab, so it is necessary to implement rGB2HSL (R,G,B) and HSL2HSL (H,S,L) functions to convert BETWEEN HSL channel and RGB channel:

The content of the reference: zh.wikipedia.org/wiki/HSL

Color = (ColorR, ColorG, ColorB) = (r, g, b)

  1. Hsl2rgb function: R, G, B two-dimensional matrix can be passed into the picture, or RGB single value
function [R,G,B] = hsl2rgb(H,S,L)
%HSL2RGB 
    [m,n]=size(H);
    % single-valued RGB
    if m==1&&n==1
        if S~=0
            if L<0.5
                q=L*(1+S);
            else
                q=L+S-(L*S);
            end
            p=2*L-q;
            hk=H/360;
            R_temp=hk+1/3;
            G_temp=hk;
            B_temp=hk- 1/3;
            R=hsl2rgb_subfun(R_temp,p,q);
            G=hsl2rgb_subfun(G_temp,p,q);
            B=hsl2rgb_subfun(B_temp,p,q);
        elseR=L; G=L; B=L;end
    % image RGB
    else
        R=zeros(m,n); G=zeros(m,n); B=zeros(m,n);
        for i=1:m
            for j=1:n
                s=S(i.j);   
                l=L(i.j);         
                if s~=0
                    h=H(i.j);      
                    if l<0.5
                        q=l*(1+s);
                    else
                        q=l+s-(l*s);
                    end
                    p=2*l-q;
                    hk=h/360;
                    R_temp=hk+1/3;
                    G_temp=hk;
                    B_temp=hk- 1/3;
                    
                    R(i.j)=hsl2rgb_subfun(R_temp,p,q);
                    G(i.j)=hsl2rgb_subfun(G_temp,p,q);
                    B(i.j)=hsl2rgb_subfun(B_temp,p,q);
                else
                    R(i.j)=l;
                    G(i.j)=l;
                    B(i.j)=l;
                end
            end
        end
    end
end

function re = hsl2rgb_subfun(t,p,q)
        if t<0
            t=t+1.0;
        elseif t>1
            t=t1.0;
        end
        if t<1/6
            re=p+((q-p)*6*t);
        elseif 1/6<=t && t<0.5
            re=q;
        elseif 0.5<=t && t<2/3
            re=p+((q-p)*6* (2/3-t));
        else
            re=p;
        end
    end    
Copy the code
  1. Rgb2hsl function: H, S, L two-dimensional matrix can be passed into the picture, or the single value of HSL
function [H,S,L] = rgb2hsl(R,G,B)
%RGB2HSL 
    [m,n]=size(R);
    % Single-value RGB
    if m==1&&n==1
        rgbMax=max([R,G,B]);
        rgbMin=min([R,G,B]);
        if rgbMax==rgbMin
            H=0;
        elseif rgbMax==R && G>=B
            H=60*(G-B)/(rgbMax-rgbMin);
        elseif rgbMax==R && G<B
            H=60*(G-B)/(rgbMax-rgbMin)+360;
        elseif rgbMax==G
            H=60*(B-R)/(rgbMax-rgbMin)+120;
        elseif rgbMax==B
            H=60*(R-G)/(rgbMax-rgbMin)+240;
        end
        L=(rgbMax+rgbMin)/2;
        if L==0 || rgbMax==rgbMin
            S=0;
        elseif L>0 && L<=0.5
            S=(rgbMax-rgbMin)/(2*L);
        elseif L>0.5
            S=(rgbMax-rgbMin)/(22 -*L);
        end
    % RGB of the picture
    else
        H=zeros(m,n);
        S=zeros(m,n);
        L=zeros(m,n);
        for i=1:m
            for j=1:n
               r=R(i.j);
               g=G(i.j);
               b=B(i.j);
               rgbMax=max([r,g,b]);
               rgbMin=min([r,g,b]);
               if rgbMax==rgbMin
                    H(i.j) =0;
               elseif rgbMax==r && g>=b
                    H(i.j) =60*(g-b)/(rgbMax-rgbMin);
               elseif rgbMax==r && g<b
                    H(i.j) =60*(g-b)/(rgbMax-rgbMin)+360;
               elseif rgbMax==g
                    H(i.j) =60*(b-r)/(rgbMax-rgbMin)+120;
               elseif rgbMax==b
                    H(i.j) =60*(r-g)/(rgbMax-rgbMin)+240;
               end
               L(i.j)=(rgbMax+rgbMin)/2;
               if L(i.j) = =0 || rgbMax==rgbMin
                    S(i.j) =0;
               elseif 0<L(i.j) && L(i.j) < =0.5
                    S(i.j)=(rgbMax-rgbMin)/(2*L(i.j));
               elseif L(i.j) >0.5
                    S(i.j)=(rgbMax-rgbMin)/(22 -*L(i.j));
               end
            end
        end
    end
end

Copy the code