-
Notifications
You must be signed in to change notification settings - Fork 61
/
girl.py
337 lines (273 loc) · 14.7 KB
/
girl.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import random
import time
import os
from LDR import *
from tone import *
from genStroke_origin import *
from drawpatch import rotate
from tools import *
from ETF.edge_tangent_flow import *
from deblue import deblue
from quicksort import *
# args
input_path = './input/ztfn_up.png'
output_path = './output'
np.random.seed(1)
n = 10 # Quantization order
period = 4 # line period
direction = 10 # num of dir
Freq = 100 # save every(freq) lines drawn
deepen = 1 # for edge
transTone = False # for Tone8
kernel_radius = 3 # for ETF
iter_time = 15 # for ETF
background_dir = 45 # for ETF
CLAHE = True
edge_CLAHE = True
draw_new = True
random_order = False
ETF_order = True
process_visible = True
if __name__ == '__main__':
file_name = os.path.basename(input_path)
file_name = file_name.split('.')[0]
print(file_name)
output_path = output_path+"/"+file_name
if not os.path.exists(output_path):
os.makedirs(output_path)
os.makedirs(output_path+"/mask")
os.makedirs(output_path+"/process")
####### ETF #######
time_start=time.time()
ETF_filter = ETF(input_path=input_path, output_path=output_path+'/mask',\
dir_num=direction, kernel_radius=kernel_radius, iter_time=iter_time, background_dir=background_dir)
ETF_filter.forward()
print('ETF done')
input_img = cv2.imread(input_path, cv2.IMREAD_GRAYSCALE)
(h0,w0) = input_img.shape
cv2.imwrite(output_path + "/input_gray.jpg", input_img)
# if h0>w0:
# input_img = cv2.resize(input_img,(int(256*w0/h0),256))
# else:
# input_img = cv2.resize(input_img,(256,int(256*h0/w0)))
# (h0,w0) = input_img.shape
if transTone == True:
input_img = transferTone(input_img)
now_ = np.uint8(np.ones((h0,w0)))*255
step = 0
if draw_new==True:
time_start=time.time()
stroke_sequence=[]
stroke_temp={'angle':None, 'grayscale':None, 'row':None, 'begin':None, 'end':None}
for dirs in range(direction):
angle = -90+dirs*180/direction
print('angle:', angle)
stroke_temp['angle'] = angle
img,_ = rotate(input_img, -angle)
############ Adjust Histogram ############
if CLAHE==True:
img = HistogramEqualization(img)
# cv2.imshow('HistogramEqualization', res)
# cv2.waitKey(0)
# cv2.imwrite(output_path + "/HistogramEqualization.png", res)
print('HistogramEqualization done')
########### gredient #######
img_pad = cv2.copyMakeBorder(img, 2*period, 2*period, 2*period, 2*period, cv2.BORDER_REPLICATE)
img_normal = cv2.normalize(img_pad.astype("float32"), None, 0.0, 1.0, cv2.NORM_MINMAX)
x_der = cv2.Sobel(img_normal, cv2.CV_32FC1, 1, 0, ksize=5)
y_der = cv2.Sobel(img_normal, cv2.CV_32FC1, 0, 1, ksize=5)
x_der = torch.from_numpy(x_der) + 1e-12
y_der = torch.from_numpy(y_der) + 1e-12
gradient_magnitude = torch.sqrt(x_der**2.0 + y_der**2.0)
gradient_norm = gradient_magnitude/gradient_magnitude.max()
############ Quantization ############
ldr = LDR(img, n)
# cv2.imshow('Quantization', ldr)
# cv2.waitKey(0)
cv2.imwrite(output_path + "/Quantization.png", ldr)
# LDR_single(ldr,n,output_path) # debug
############ Cumulate ############
LDR_single_add(ldr,n,output_path)
print('Quantization done')
# get tone
(h,w) = ldr.shape
canvas = Gassian((h+4*period,w+4*period), mean=250, var = 3)
for j in range(n):
# print('tone:',j)
# distribution = ChooseDistribution(period=period,Grayscale=j*256/n)
stroke_temp['grayscale'] = j*256/n
mask = cv2.imread(output_path + '/mask/mask{}.png'.format(j),cv2.IMREAD_GRAYSCALE)/255
dir_mask = cv2.imread(output_path + '/mask/dir_mask{}.png'.format(dirs),cv2.IMREAD_GRAYSCALE)
# if angle==0:
# dir_mask[::] = 255
dir_mask,_ = rotate(dir_mask, -angle, pad_color=0)
dir_mask[dir_mask<128]=0
dir_mask[dir_mask>127]=1
distensce = Gassian((1,int(h/period)+4), mean = period, var = 1)
distensce = np.uint8(np.round(np.clip(distensce, period*0.8, period*1.25)))
raw = -int(period/2)
for i in np.squeeze(distensce).tolist():
if raw < h:
y = raw + 2*period # y < h+2*period
raw += i
for interval in get_start_end(mask[y-2*period]*dir_mask[y-2*period]):
begin = interval[0]
end = interval[1]
# length = end - begin
begin -= 2*period
end += 2*period
length = end - begin
stroke_temp['begin'] = begin
stroke_temp['end'] = end
stroke_temp['row'] = y-int(period/2)
#print(gradient_norm[y,interval[0]+2*period:interval[1]+2*period])
stroke_temp['importance'] = (255-stroke_temp['grayscale'])*torch.sum(gradient_norm[y:y+period,interval[0]+2*period:interval[1]+2*period]).numpy()
stroke_sequence.append(stroke_temp.copy())
# newline = Getline(distribution=distribution, length=length)
# if length<1000 or begin == -2*period or end == w-1+2*period:
# temp = canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin:2*period+end]
# m = np.minimum(temp, newline[:,:temp.shape[1]])
# canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin:2*period+end] = m
# else:
# temp = canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin-2*period:2*period+end+2*period]
# m = np.minimum(temp, newline)
# canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin-2*period:2*period+end+2*period] = m
# if step % Freq == 0:
# if step > Freq: # not first time
# before = cv2.imread(output_path + "/process/{0:04d}.png".format(int(step/Freq)-1), cv2.IMREAD_GRAYSCALE)
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
# now = np.minimum(before,now)
# else: # first time to save
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
# cv2.imwrite(output_path + "/process/{0:04d}.png".format(int(step/Freq)), now)
# # cv2.imshow('step', canvas)
# # cv2.waitKey(0)
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
# now = np.minimum(now,now_)
# step += 1
# cv2.imshow('step', now_)
# cv2.waitKey(1)
# now_ = now
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
# cv2.imwrite(output_path + "/pro/{}_{}.png".format(dirs,j), now)
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
# cv2.imwrite(output_path + "/{:.1f}.png".format(angle), now)
# cv2.destroyAllWindows()
time_end=time.time()
print('total time',time_end-time_start)
print('stoke number',len(stroke_sequence))
# cv2.imwrite(output_path + "/draw.png", now_)
# cv2.imshow('draw', now_)
# cv2.waitKey(0)
if random_order == True:
random.shuffle(stroke_sequence)
if ETF_order == True:
random.shuffle(stroke_sequence)
quickSort(stroke_sequence,0,len(stroke_sequence)-1)
result = Gassian((h0,w0), mean=250, var = 3)
canvases = []
for dirs in range(direction):
angle = -90+dirs*180/direction
canvas,_ = rotate(result, -angle)
# (h,w) = canvas.shape
canvas = np.pad(canvas, pad_width=2*period, mode='constant', constant_values=(255,255))
canvases.append(canvas)
for stroke_temp in stroke_sequence:
angle = stroke_temp['angle']
dirs = int((angle+90)*direction/180)
grayscale = stroke_temp['grayscale']
distribution = ChooseDistribution(period=period,Grayscale=grayscale)
row = stroke_temp['row']
begin = stroke_temp['begin']
end = stroke_temp['end']
length = end - begin
newline = Getline(distribution=distribution, length=length)
canvas = canvases[dirs]
if length<1000 or begin == -2*period or end == w-1+2*period:
temp = canvas[row:row+2*period,2*period+begin:2*period+end]
m = np.minimum(temp, newline[:,:temp.shape[1]])
canvas[row:row+2*period,2*period+begin:2*period+end] = m
# else:
# temp = canvas[row:row+2*period,2*period+begin-2*period:2*period+end+2*period]
# m = np.minimum(temp, newline)
# canvas[row:row+2*period,2*period+begin-2*period:2*period+end+2*period] = m
now,_ = rotate(canvas[2*period:-2*period,2*period:-2*period], angle)
(H,W) = now.shape
now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
result = np.minimum(now,result)
if process_visible == True:
cv2.imshow('step', result)
cv2.waitKey(1)
step += 1
if step % Freq == 0:
# if step > Freq: # not first time
# before = cv2.imread(output_path + "/process/{0:04d}.png".format(int(step/Freq)-1), cv2.IMREAD_GRAYSCALE)
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
# now = np.minimum(before,now)
# else: # first time to save
# now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
# (H,W) = now.shape
# now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(int(step/Freq)), result)
# cv2.imshow('step', canvas)
# cv2.waitKey(0)
if step % Freq != 0:
step = int(step/Freq)+1
cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(step), result)
cv2.destroyAllWindows()
time_end=time.time()
print('total time',time_end-time_start)
print('stoke number',len(stroke_sequence))
cv2.imwrite(output_path + '/draw.jpg', result)
############ gen edge ###########
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# pc = PencilDraw(device=device, gammaS=1)
# pc(input_path)
# edge = cv2.imread('output/Edge.png', cv2.IMREAD_GRAYSCALE)
edge = genStroke(input_img,18)
edge = np.power(edge, deepen)
edge = np.uint8(edge*255)
if edge_CLAHE==True:
edge = HistogramEqualization(edge)
cv2.imwrite(output_path + '/edge.jpg', edge)
cv2.imshow("edge",edge)
############# merge #############
edge = np.float32(edge)
now_ = cv2.imread(output_path + "/draw.jpg", cv2.IMREAD_GRAYSCALE)
result = res_cross= np.float32(now_)
result[1:,1:] = np.uint8(edge[:-1,:-1] * res_cross[1:,1:]/255)
result[0] = np.uint8(edge[0] * res_cross[0]/255)
result[:,0] = np.uint8(edge[:,0] * res_cross[:,0]/255)
result = edge*res_cross/255
result=np.uint8(result)
cv2.imwrite(output_path + '/result.jpg', result)
# cv2.imwrite(output_path + "/process/{0:04d}.png".format(step+1), result)
cv2.imshow("result",result)
# deblue
deblue(result, output_path)
# RGB
img_rgb_original = cv2.imread(input_path, cv2.IMREAD_COLOR)
cv2.imwrite(output_path + "/input.jpg", img_rgb_original)
img_yuv = cv2.cvtColor(img_rgb_original, cv2.COLOR_BGR2YUV)
img_yuv[:,:,0] = result
img_rgb = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imshow("RGB",img_rgb)
cv2.waitKey(0)
cv2.imwrite(output_path + "/result_RGB.jpg",img_rgb)