matlab实现PS算法之自动色阶

function I_out=F_color(I, percent)
%%% the tonal range of the input image is 0-1.
[row, col]=size(I);
I_sort=sort(I(:));
I_out=I;

%%% based on the clipping percentage, 
%%% compute the upper and lower boundaries 
if (percent==0)
    I_min=min(I_sort)
    I_max=max(I_sort)
else
    I_min=I_sort(floor(row*col*percent))
    I_max=I_sort(floor(row*col*(1-percent)))
end

for i=1:row
    for j=1:col
            if(I(i,j)<I_min)
                I_out(i,j)=I_min;
            elseif(I(i,j)>I_max)
                I_out(i,j)=1;
            else
                I_out(i,j)=(I(i,j)-I_min)*(1-I_min)/(I_max-I_min)+I_min;
            end
    end 
end

%{
    对R,G,B三个通道的动态范围分别进行
    拉伸。这个算法还要考虑一个 clipping percentage, 
    默认值是 0.1%。根据这个定义,可以先编写一个拉伸动态范围的函数
%}
clear,clc;
[filename,pathname] = uigetfile('*.jpg;*.bmp','选择图片','E:\pictures\For_Project\Matlab');
imgaepath = strcat(pathname,filename);
image = imread(imgaepath);

Image = double(image)/255;

r = Image(:,:,1);
g = Image(:,:,2);
b = Image(:,:,3);
image_out = Image;
percent = 0.01;
image_out(:,:,1)  = F_color(r,percent);
image_out(:,:,2)  = F_color(g,percent);
image_out(:,:,3)  = F_color(b,percent);
figure(1),subplot(1,2,1),imshow(image),title('原图');
subplot(1,2,2),imshow(image_out),title('自动色阶');

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值