matlab实现调整图片的饱和度

%src为rgb图像,saturation为调节的饱和度值,调节范围为【-100100】
function Image_new = SaturationAdjustment(src,saturation)
Image=src;
Image=double(Image);
R=Image(:,:,1);
G=Image(:,:,2);
B=Image(:,:,3);
[row, col] = size(R);
R_new=R;
G_new=G;
B_new=B;
%%%% Increment, 饱和度调整增量(-100,100)photoshop的范围
Increment=saturation;

%换算成调整比率
Increment=Increment/100;

%利用HSL模式求得颜色的S和L
for i=1:row
    for j=1:col
        rgbMax=max(R(i,j),max(G(i,j),B(i,j)));
        rgbMin=min(R(i,j),min(G(i,j),B(i,j)));
        Delta=(rgbMax-rgbMin)/255;
   
        if(Delta==0)                              %如果delta=0,则饱和度S=0,所以不能调整饱和度
            continue;
        end
        value = (rgbMax + rgbMin)/255;
        L=value/2;                                %Lightness
        
        if(L<0.5)                                 %根据明度L计算饱和度S
            S=Delta/value;
        else
            S =Delta/(2 - value);
        end
        %具体的饱和度调整,Increment为饱和度增减量
        if (Increment>=0)
            if((Increment+S)>=1)
                alpha=S;
            else
                alpha=1-Increment;
            end
          alpha=1/alpha-1;
          R_new(i,j) = R(i,j) + (R(i,j) - L * 255) * alpha;
          G_new(i,j) = G(i,j) + (G(i,j) - L * 255) * alpha;
          B_new(i,j) = B(i,j) + (B(i,j) - L * 255) * alpha;
        else
          alpha=Increment;
          R_new(i,j) = L*255 + (R(i,j) - L * 255) * (1+alpha);
          G_new(i,j) = L*255 + (G(i,j) - L * 255) * (1+alpha);
          B_new(i,j) = L*255 + (B(i,j) - L * 255) * (1+alpha); 
        end
    end
end     
Image_new(:,:,1)=R_new;
Image_new(:,:,2)=G_new;
Image_new(:,:,3)=B_new;
end
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页