算法参考自 阿发伯 的博客.
饱和度调整
图像的饱和度调整有很多方法,最简单的就是判断每个象素的R、G、B值是否大于或小于128,大于加上调整值,小于则减去调整值;也可将象素RGB转换为HSV或者HSL,然后调整其S部分,从而达到线性调整图象饱和度的目的。这几种方法我都测试过,效果均不太好,简单的就不说了,利用HSV和HSL调整饱和度,其调节范围很窄,饱和度没达到,难看的色斑却出现了。而Photoshop的饱和度调整调节范围大多了,效果也好多了。
Photoshop的色相/饱和度的调整还是转换为HSL颜色模式进行的,只是饱和度的增减调节却是“独立”于SHL模式的另外一套算法,如果不是需要HSL的S和L部分进行饱和度的上下限控制,它也和明度调整一样,可以独立进行!
%%%% Increment, 饱和度调整增量(-100,100)
Increment=-20/100;
rgbMax=max(R,G,B);
rgbMin=min(R,G,B);
Delta=(rgbMax-rgbMin)/255;
if(Delta==0)
continue;
end
value=(rgbMax + rgbMin)/255;
L=value/2;
if(L<0.5)
S=Delta/value;
else
S =Delta/(2 - value);
end
if (Increment>=0) //如果饱和度增量大于0,
if((Increment+S)>=1)
alpha=S;
else
alpha=1-Increment;
end
alpha=1/alpha-1;
R_new = R+(R- L * 255) * alpha;
G_new =G+(G - L * 255) *alpha;
B_new = B+(B - L * 255) *alpha;
else // 饱和度增量小于0
alpha=Increment;
R_new = L*255 + (R- L * 255) *(1+alpha);
G_new = L*255 +(G- L * 255) * (1+alpha);
B_new = L*255 +(B- L * 255) * (1+alpha);
end
Program:
%%% 程序实现图像的饱和度调整
clc;
clear all;
close all;
Image=imread('4.jpg');
Image=double(Image);
R=Image(:,:,1);
G=Image(:,:,2);
B=Image(:,:,3);
I=0.299*R+0.587*G+0.114*B;
[row, col] = size(R);
R_new=R;
G_new=G;
B_new=B;
%%%% Increment, 饱和度调整增量(-100,100)
Increment=50/100;
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)
continue;
end
value = (rgbMax + rgbMin)/255;
L=value/2;
if(L<0.5)
S=Delta/value;
else
S =Delta/(2 - value);
end
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;
imshow(Image/255);
figure, imshow(Image_new/255);
原图:
效果图:饱和度增加 50%