matlab实现PS算法之自动色阶

来源:互联网 发布:知止而有得 编辑:程序博客网 时间:2024/05/18 02:24
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)))endfor 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('自动色阶');

0 0