-
Notifications
You must be signed in to change notification settings - Fork 61
/
pencil_draw.m
163 lines (147 loc) · 5.55 KB
/
pencil_draw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function Ipencil = pencil_draw(I)
% The algorithm is as follows:
% 1. Compute the outline sketch 'S'
% * Prepare the 8 directional line segment images, 'L',
% all with width and height 'line_len', which is around 1/30 of the
% input image I's width
% * Compute the gradient of the input image which is 'Imag'
% * Generate the pixel classification 'C' of the 8 directions
% * Convolute the 8 'C's and 'L's to obtain our final outline sketch 'S'
% 2. Compute the texture tone drawing 'T'
% * Prepare 'Jadjusted' which is the adjusted the raw image 'J'
% against the natural histogram of typical pencil drawings.
% * Repetitively stitch our base texture image to the size of our raw image.
% The result is 'Jtexture', our base texture background.
% * Solve for 'beta', the number of times we should draw the texture.
% 'beta' should have the same width and height as our raw image 'J':
% * Although beta is two dimensional, see it as an
% one dimensional column 'b' with length size(J,1)*size(J,2).
% The same goes for 'Jadjusted', whose one dimensional column
% representation is 'Ja'.
% * Our minimization objective function is:
% |log(Jtexture_diag)*b - log(Ja)|^2 + lambda*(|dx*b|^2+|dy*b|^2)
% Where 'Jtexture_diag' is a diagonal square matrix whose
% width equals the length of 'b' and with
% Jtexture's column representation at its diagonals.
% 'dx' and 'dy' are square matrices that represent
% image pixel differences at the x and y directions.
% * Apparently, 'Jtexture_diag', 'dx' and 'dy' are all matrices
% of extreme size, therefore they should be sparse matrices.
% * Our desired texture tone drawing 'T' is Jtexture.^beta
% 3. Combine 'S' and 'T'
%
% Usage: imshow(pencil_draw(imread('img/sign.png')))
% Constants:
line_len_divisor = 40; % larger for a shorter line fragment
line_thickness_divisor = 8; % larger for thiner outline sketches
lambda = 2; % larger for smoother tonal mappings
texture_resize_ratio = 0.2;
texture_file_name = 'textures/texture.jpg';
if length(size(I)) == 3
J = rgb2gray(I);
type = 'black';
else
J = I;
type = 'colour';
end
% ================================================
% Compute the outline sketch 'S'
% ================================================
% calculate 'line_len', the length of the line segments
line_len_double = min([size(J,1), size(J,2)]) / line_len_divisor;
if mod(floor(line_len_double), 2)
line_len = floor(line_len_double);
else
line_len = floor(line_len_double) + 1;
end
half_line_len = (line_len + 1) / 2;
% compute the image gradient 'Imag'
Ix = conv2(im2double(J), [1,-1;1,-1], 'same');
Iy = conv2(im2double(J), [1,1;-1,-1], 'same');
Imag = sqrt(Ix.*Ix + Iy.*Iy);
% create the 8 directional line segments L
L = zeros(line_len, line_len, 8);
for n=0:7
if n == 0 || n == 1 || n == 2 || n == 7
for x=1:line_len
y = half_line_len - round((x-half_line_len)*tan(pi/8*n));
if y > 0 && y <= line_len
L(y, x, n+1) = 1;
end
end
if n == 0 || n == 1 || n == 2
L(:,:,n+5) = rot90(L(:,:,n+1));
end
end
end
L(:,:,4) = rot90(L(:,:,8), 3);
% add some thickness to L
valid_width = size(conv2(L(:,:,1),ones(round(line_len/line_thickness_divisor)),'valid'), 1);
Lthick = zeros(valid_width, valid_width, 8);
for n=1:8
Ln = conv2(L(:,:,n),ones(round(line_len/line_thickness_divisor)), 'valid');
Lthick(:,:,n) = Ln / max(max(Ln));
end
% create the sketch
G = zeros(size(J,1), size(J,2), 8);
for n=1:8
G(:,:,n) = conv2(Imag, L(:,:,n), 'same');
end
[Gmax, Gindex] = max(G, [], 3);
C = zeros(size(J,1), size(J,2), 8);
for n=1:8
C(:,:,n) = Imag .* (Gindex == n);
end
Spn = zeros(size(J,1), size(J,2), 8);
for n=1:8
Spn(:,:,n) = conv2(C(:,:,n), Lthick(:,:,n), 'same');
end
Sp = sum(Spn, 3);
Sp = (Sp - min(Sp(:))) / (max(Sp(:)) - min(Sp(:)));
S = 1 - Sp;
% ==============================================
% Compute the texture tone drawing 'T'
% ==============================================
% new tone adjusted image
Jadjusted = natural_histogram_matching(J,type);
% stich pencil texture image
texture = imread(texture_file_name);
texture = texture(100:size(texture,1)-100,100:size(texture,2)-100);
texture = im2double(imresize(texture, texture_resize_ratio*min([size(J,1),size(J,2)])/1024));
Jtexture = vertical_stitch(horizontal_stitch(texture,size(J,2)), size(J,1));
% solve for beta
sizz = size(J,1)*size(J,2); % width of big matrix
nzmax = 2*(sizz-1);
i = zeros( nzmax, 1 );
j = zeros( nzmax, 1 );
s = zeros( nzmax, 1 );
for m=1:nzmax
i(m) = ceil((m+0.1) / 2);
j(m) = ceil((m-0.1) / 2);
s(m) = -2*mod(m,2) + 1;
end
dx = sparse(i,j,s,sizz,sizz,nzmax);
nzmax = 2*(sizz - size(J,2));
i = zeros( nzmax, 1 );
j = zeros( nzmax, 1 );
s = zeros( nzmax, 1 );
for m=1:nzmax
if mod(m,2)
i(m) = ceil((m+0.1) / 2);
else
i(m) = ceil((m-1+0.1) / 2) + size(J,2);
end
j(m) = ceil((m-0.1) / 2);
s(m) = -2*mod(m,2) + 1;
end
dy = sparse(i,j,s,sizz,sizz,nzmax);
Jtexture1d = log(reshape(Jtexture',1,[]));
Jtsparse = spdiags(Jtexture1d',0,sizz,sizz);
Jadjusted1d = log(reshape(Jadjusted',1,[])');
beta1d = (Jtsparse'*Jtsparse + lambda*(dx'*dx + dy'*dy))\(Jtsparse'*Jadjusted1d);
beta = reshape(beta1d, size(J,2), size(J,1))';
% compute the texture tone image 'T' and combine it with the outline sketch
% to come out with the final result 'Ipencil'
T = Jtexture .^ beta;
T = (T - min(T(:))) / (max(T(:)) - min(T(:)));
Ipencil = S.*T;