matlab实现PS算法之黑白照

%{
    Photoshop CS图像黑白调整功能的计算公式为:

    gray= (max - mid) * ratio_max + (mid - min) * ratio_max_mid + min

    公式中:gray为像素灰度值,max、mid和min分别为图像像素R、G、B分量颜色的最大值、
中间值和最小值,ratio_max为max所代表的分量颜色(单色)比率,ratio_max_mid则为max
与mid两种分量颜色所形成的复色比率。
%}
clear,clc;
[filename,pathname] = uigetfile('*.jpg;*.bmp','选择图片','E:\pictures\For_Project\Matlab');
imgaepath = strcat(pathname,filename);
image = imread(imgaepath);

Image=double(image);
R=Image(:,:,1);
G=Image(:,:,2);
B=Image(:,:,3);
[row, col] = size(R);
Gray_img(1:row,1:col)=0;
Sum_rgb=R+G+B;
%%%% 各种颜色的默认比率
Color_Ratio(1:6)=0;
Color_Ratio(1)=0.4;     %%%% Red
Color_Ratio(2)=0.6;     %%%% Yellow
Color_Ratio(3)=0.4;     %%%% Green
Color_Ratio(4)=0.6;     %%%% Cyan
Color_Ratio(5)=0.2;     %%%% Blue
Color_Ratio(6)=0.8;     %%%% Magenta
for i=1:row
    for j=1:col
        r=R(i,j);
        g=G(i,j);
        b=B(i,j);
        Max_value=max(r,max(g,b));
        Min_value=min(r,min(g,b));
        Mid_value=Sum_rgb(i,j)-Max_value-Min_value;
        if(Min_value==r)
            Index=0;
        elseif(Min_value==g)
            Index=2;
        else
            Index=4;
        end
        ratio_max_mid=Color_Ratio(mod(Index+3,6)+1);
        if(Max_value==r)
            Index=1;
        elseif(Max_value==g)
            Index=3;
        else
            Index=5;
        end
        ratio_max=Color_Ratio(Index);
        Temp=(Max_value-Mid_value)*ratio_max+(Mid_value-Min_value)...
                      *ratio_max_mid+Min_value;
        Gray_img(i,j)=(Max_value-Mid_value)*ratio_max+(Mid_value-Min_value)...
                      *ratio_max_mid+Min_value;
    end
end

imshow(Image/255);
figure, imshow(Gray_img/255);

matlab实现PS算法之黑白照_第1张图片

你可能感兴趣的:(Matlab)