图像铅笔画算法,属于一直是非真实感绘制领域(Non-Photorealistic Rendering,NPR)中很热门的一个课题,但是计算机也很难模拟出像人一样真实的画质,这也显得CG师们的重要性。本文是基于香港中文大学Cewu Lu等人所做的工作《Combining Sketch and Tone for Pencil Drawing Production》,描述计算机生成铅笔画的艺术。本人才疏学浅,描述如有错误,还望指点。
作者基于对日常生活中的人手绘铅笔画的观察,可以分为两个步骤,第一步勾勒出物体的大致轮廓;第二步是对物体进行色调渲染,即用铅笔反复轻轻的划。
通过对图像求其梯度得到,得到轮廓。
人手绘的铅笔画的直方图往往如下
I = imread('texture.jpg');
I = rgb2gray(I);
I02 = ((double(I)/255).^0.2);
I04 = ((double(I)/255).^0.4);
I06 = ((double(I)/255).^0.6);
I08 = ((double(I)/255).^0.8);
I12 = ((double(I)/255).^1.2);
I14 = ((double(I)/255).^1.4);
I16 = ((double(I)/255).^1.6);
I18 = ((double(I)/255).^1.8);
I20 = ((double(I)/255).^2.0);
function S = GenStroke(im, ks, dirNum)
% ==============================================
% Compute the Stroke Structure 'S'
% S = GenStroke(im, ks, dirNum) caculates the direction angle of pixels in
% "im", with kernel size "ks", and number of directions "dirNum".
%
% Paras:
% @im : input image ranging value from 0 to 1.
% @ks : kernel size.
% @dirNum : number of directions.
%
%
% Example
% ==========
% im = im2double(imread('npar12_pencil2.bmp'));
% Iruv = rgb2ycbcr(im);
% S= GenStroke(Iruv(:,:,1),ks,dirNum);
% figure, imshow(S)
%
%
% ==========
% The code is created based on the method described in
% "Combining Sketch and Tone for Pencil Drawing Production" Cewu Lu, Li Xu, Jiaya Jia
% International Symposium on Non-Photorealistic Animation and Rendering (NPAR 2012), June, 2012
% image gradients
[H, W, sc] = size(im);
if sc == 3
im = rgb2gray(im);
end
imX = [abs(im(:,1:(end-1)) - im(:,2:end)),zeros(H,1)];
imY = [abs(im(1:(end-1),:) - im(2:end,:));zeros(1,W)];
imEdge = imX + imY;
% convolution kernel with horizontal direction
kerRef = zeros(ks*2+1);
kerRef(ks+1,:) = 1;
% classification
response = zeros(H,W,dirNum);
for n = 1 : dirNum
ker = imrotate(kerRef, (n-1)*180/dirNum, 'bilinear', 'crop');
response(:,:,n) = conv2(imEdge, ker, 'same');
end
[~ , index] = max(response,[], 3);
% create the sketch
C = zeros(H, W, dirNum);
for n=1:dirNum
C(:,:,n) = imEdge .* (index == n);
end
Spn = zeros(H, W, dirNum);
for n=1:dirNum
ker = imrotate(kerRef, (n-1)*180/dirNum, 'bilinear', 'crop');
Spn(:,:,n) = conv2(C(:,:,n), ker, 'same');
end
Sp = sum(Spn, 3);
Sp = (Sp - min(Sp(:))) / (max(Sp(:)) - min(Sp(:)));
S = 1 - Sp;
function T = texturetransfer(P,J)
% ==============================================
% Texture Rendering
% T = texturetransfer(P,J) pencil texture transfer from P to
J
%
% Paras:
% @P : tonal texture value from 0 to 1.
% @J : pencil tone image value for 0 to 1.
%
%
% Example
% ==========
% P = im2double(rgb2gray(imread('TonalTexture.jpg')));
% J = im2double(rgb2gray(imread('PencilTone.jpg')));
% T = texturetransfer(P,J);
% figure,imshow(T); title('T');
lambda =0.2;
[r,c,~] = size(P);
k = r*c;
% Transform to log domain
logP = log(P + eps);
logP = logP(:);
logJ = log(J + eps);
logJ = logJ(:);
dx = ones(k,1);
dx = dx(:);
dy = ones(k,1);
dy = dy(:);
B(:,1) = dx;
B(:,2) = dy;
d = [-r,-1];
A = spdiags(B,d,k,k);
e = padarray(dx, r,'post'); e = e(r+1:end);
w = padarray(dx, r,'pre'); w = w(1:end-r);
s = padarray(dy, 1,'post'); s = s(2:end);
n = padarray(dy, 1,'pre'); n = n(1:end-1);
D = -(e+w+s+n);
A = lambda*(A + A' + spdiags(D,0,k,k)) + spdiags(logP.*logP,0,k,k);
b = logJ.*logP;
%Solve
beta = pcg(A,b,1e-6,60);
beta = reshape(beta,r,c);
beta = (beta - min(beta(:))) / (max(beta(:)) - min(beta(:))) * 4;
figure,imshow(beta./max(beta(:)));
T = (P).^beta;
注: 这里我给出的只是第一步产生笔画结构的代码,和纹理渲染部分代码,只有色调映射部分未给。若对此算法感兴趣,可以参考作者论文。另外作者主页上有部分代码,可以下载
在此感谢采石工(QQ544617183)指正公式求解过程中几处不正确。
附上采石工的推导证明
感兴趣的朋友可以点此下载,尝试效果。
http://www.cnblogs.com/Imageshop/p/4285566.html
http://www.cse.cuhk.edu.hk/~leojia/projects/pencilsketch/pencil_drawing.htm
作者 | 日期 | 联系方式 |
---|---|---|
风吹夏天 | 2015年5月2日 | wincoder#qq.com |