【matlab图像处理】图片生成油画效果(选择路径-交互)

%油画效果
function [I_painted]=OilPaint(I_rgb, radius, intensity_level)
[filename pathname]=uigetfile({'*.jpg';'*.bmp';'*.*'},'读入文件');
pathfile=[pathname filename];
handles.pic=imread(pathfile);
intensity_level = 180;
radius = 5;
s1=size(handles.pic);
width=s1(1);
height=s1(2); 
I_painted=uint8(zeros(width,height));
I=rgb2gray(handles.pic);
% down-sampling the intensity image
I= floor(I* (intensity_level/255));
tic
for x = 1:width
    for y = 1:height
        clear intensity_counter RGB max_instance index
        intensity_counter=uint8(zeros(1,intensity_level+1));
        R=uint8(zeros(1,intensity_level+1));
        G=uint8(zeros(1,intensity_level+1));
        B=uint8(zeros(1,intensity_level+1));
        for i = (x-radius):(x+radius)
            for j = (y-radius):(y+radius)
                if i>0 && i<=width && j>0 && j<=height 
                    intensity = I(i,j)+1;
                    intensity_counter(intensity)=intensity_counter(intensity)+1;
                    R(intensity) = handles.pic(i,j,1);
                    G(intensity) = handles.pic(i,j,2);
                    B(intensity) = handles.pic(i,j,3);
                end
            end
        end
        % finding the maximum occuring intensity in downsampled image
        [max_instance index] = max(intensity_counter);
        I_painted(x, y, 1)= R(index);
        I_painted(x, y, 2)= G(index);
        I_painted(x, y, 3)= B(index);
    end
end
toc 
figure
imshow(I_painted,[]);
title('油画效果');
【matlab图像处理】图片生成油画效果(选择路径-交互)_第1张图片


【matlab图像处理】图片生成油画效果(选择路径-交互)_第2张图片

你可能感兴趣的:(图像处理,matlab图像处理)