MATLAB实现将图像转换为素描(简笔画)风格

时间:2024-01-21 12:04:23

代码:

colorgrad.m

function [VG, A, PPG] = colorgrad(f, T)
if (ndims(f)~=) || (size(f,)~=)
error('Input image must be RGB');
end
sh = fspecial('sobel');
sv = sh';
Rx = imfilter(double(f(:,:,)), sh, 'replicate');
Ry = imfilter(double(f(:,:,)), sv, 'replicate');
Gx = imfilter(double(f(:,:,)), sh, 'replicate');
Gy = imfilter(double(f(:,:,)), sv, 'replicate');
Bx = imfilter(double(f(:,:,)), sh, 'replicate');
By = imfilter(double(f(:,:,)), sv, 'replicate'); gxx = Rx.^ + Gx.^ + Bx.^;
gyy = Ry.^ + Gy.^ + By.^;
gxy = Rx.*Ry + Gx.*Gy + Bx.*By;
A = 0.5*(atan(*gxy./(gxx-gyy+eps)));
G1 = 0.5*((gxx+gyy) + (gxx-gyy).*cos(*A) + *gxy.*sin(*A)); A = A + pi/;
G2 = 0.5*((gxx+gyy) + (gxx-gyy).*cos(*A) + *gxy.*sin(*A));
G1 = G1.^0.5;
G2 = G2.^0.5;
VG = mat2gray(max(G1, G2)); RG = sqrt(Rx.^ + Ry.^);
GG = sqrt(Gx.^ + Gy.^);
BG = sqrt(Bx.^ + By.^); PPG = mat2gray(RG + GG + BG); if nargin ==
VG = (VG>T).*VG;
PPG = (PPG>T).*PPG;
end

demo.m

close all;clear all;clc;
f = imread('wo.jpg');
[VG,A,PPG] = colorgrad(f);
ppg = im2uint8(PPG);
ppgf = - ppg;
[M,N] = size(ppgf);
T=;
ppgf1 = zeros(M,N);
for ii = :M
for jj = :N
if ppgf(ii,jj)<T %边缘区域
% ppgf1(ii,jj)=;
ppgf1(ii,jj)=(T-ppgf1(ii,jj))/;
else %平滑区域
ppgf1(ii,jj)=/(-T)*(ppgf(ii,jj)-T);
end
end
end
ppgf1 = uint8(ppgf1);
figure;
subplot();imshow(ppgf);
subplot();imshow(ppgf1);
subplot();imhist(ppgf);
subplot();imhist(ppgf1); figure;imshow(ppgf1);

效果:

MATLAB实现将图像转换为素描(简笔画)风格

MATLAB实现将图像转换为素描(简笔画)风格MATLAB实现将图像转换为素描(简笔画)风格