利用另外一种算法完成素描特效的生成。


%%% Sketch
clc;
clear all;
Image=imread('4.jpg');
Image=double(Image);
[row,col,layer]=size(Image);

Filter_size=30;
sigma=Filter_size/6;
F_gaussian=fspecial('gaussian', Filter_size, sigma);

for i=1:row
    for j=1:col
        I(i,j)=max(Image(i,j));
    end
end
I=I/255;
p=1;
Gradient_X=[-1 -p -1
            0   0  0
            1   p  1];
        
Gradient_Y=[-1 0 1
            -p 0 p
            -1 0 1];
Image_y=imfilter(I, Gradient_Y, 'conv');        
Image_x=imfilter(I, Gradient_X, 'conv');

alpha=0.5;
% Image_edge=(abs(Image_x)*alpha+(1-alpha)*abs(Image_y));
Image_edge=(abs(Image_x)+abs(Image_y));

F_image=imfilter(Image,F_gaussian,'conv');
[L,a,b]=Rgb2Lab(F_image);
I=sqrt(L.*L+a.*a+b.*b);
I_mean=mean(mean(I));
F_S=I;
for i=1:row
    for j=1:col
        if(I(i,j)<I_mean)
            F_S(i,j)=(1-I(i,j)/I_mean).^2;
        else
            F_S(i,j)=0;
        end
    end
end

Image_out=(1-Image_edge).*(0.8-F_S);

%% 设置底色层
    Image_Dodge(:,:,1)=Image_out;
    Image_Dodge(:,:,2)=Image_out;
    Image_Dodge(:,:,3)=Image_out;
    Base_layer=Image_out;
    Base_layer(:,:,1)=210/255;
    Base_layer(:,:,2)=225/255;
    Base_layer(:,:,3)=105/255;
   alpha=0.7;
   Image1=alpha*Image_Dodge+(1-alpha)*Base_layer;
   Image3=Image_Dodge.*Image1;
   figure, imshow(Image3);
 

%%%  Rgb2Lab
%%%  
function [L,a,b]=Rgb2Lab(Image)
Image=double(Image)/255;
R=Image(:,:,1);
G=Image(:,:,2);
B=Image(:,:,3);

X=0.5767309*R + 0.1855540*G + 0.1881852*B;
Y=0.2973769*R + 0.6273491*G + 0.0752741*B;
Z=0.0270343*R + 0.0706872*G + 0.9911085*B;
[row, col]=size(R);

L=R;
a=R;
b=R;

for i=1:row
    for j=1:col        
        L(i,j)=116*F_Correction(Y(i,j))-16;
        a(i,j)=500*(F_Correction(X(i,j))-F_Correction(Y(i,j)));
        b(i,j)=200*(F_Correction(Y(i,j))-F_Correction(Z(i,j)));
    end
end



 原图


 效果图


posted on 2014-10-03 10:11  未雨愁眸  阅读(254)  评论(0编辑  收藏  举报