-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmosaic.py
425 lines (288 loc) · 13.5 KB
/
mosaic.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import argparse
import cv2
import os
import numpy as np
from random import randint
from params import Params
index_matrix = None
def compute_dimensions():
# compute dimensions of generate image,
# based of image scale and Params.number_mosaic_parts_horizontal
[pieseH, pieseW, _] = \
np.shape(Params.mosaic_pieces[0])
[refH, refW, _] = np.shape(Params.refImage)
rezW = Params.number_mosaic_parts_horizontal * pieseW
scara = rezW / refW
rezH = refH * scara
Params.number_mosaic_parts_vertical = int(np.floor(rezH / pieseH))
H = Params.number_mosaic_parts_vertical * pieseH
W = Params.number_mosaic_parts_horizontal * pieseW
Params.ref_image_resized = cv2.resize(Params.refImage, (W, H))
def add_mosaic_piece_krodo():
global index_matrix
img_mosaic = np.uint8(np.zeros(np.shape(Params.ref_image_resized)))
index_matrix = [[-1 for x in range(Params.number_mosaic_parts_horizontal + 2)] for y in
range(Params.number_mosaic_parts_vertical + 2)]
index_matrix = np.asarray(index_matrix)
img_mosaic = np.asarray(img_mosaic)
[N, H, W, C] = np.shape(Params.mosaic_pieces)
[h, w, c] = np.shape(Params.ref_image_resized)
total_pieces_no = Params.number_mosaic_parts_horizontal * Params.number_mosaic_parts_vertical
add_pieces_no = 0
avg_color_list = compute_average_color_list()
for i in range(1, Params.number_mosaic_parts_vertical + 1):
for j in range(1, Params.number_mosaic_parts_horizontal + 1):
if Params.identical_matching_pieces == 1:
indice = find_average_color(
Params.ref_image_resized[(i - 1) * H: i * H, (j - 1) * W: j * W, :]
, avg_color_list)
else:
indice = find_average_color(
Params.ref_image_resized[(i - 1) * H: i * H, (j - 1) * W: j * W, :]
, avg_color_list, i - 1, j - 1)
img_mosaic[(i - 1) * H: i * H, (j - 1) * W: j * W, :] = \
Params.mosaic_pieces[indice][:][:][:]
add_pieces_no += 1
print("Building mosaic. {}% ready ".format(int(100 * add_pieces_no / total_pieces_no)))
return img_mosaic
def add_mosaic_piece_random_position():
img_mosaic = np.uint8(np.zeros(np.shape(Params.ref_image_resized)))
img_mosaic = np.asarray(img_mosaic)
[N, H, W, C] = np.shape(Params.mosaic_pieces)
[h, w, c] = np.shape(Params.ref_image_resized)
add_pieces_no = 0
avg_color_list = compute_average_color_list()
crossings_no = Params.crossings_no
rangeH = h - H
rangeW = w - W
if Params.rand_criterion == 'try_hard':
empty_mozaic = {(row * rangeW + col): (row, col) for row in range(0, rangeH) for col in range(0, rangeW)}
pixels_to_fill = rangeH * rangeW
while empty_mozaic:
# get a random block to fill from the empty blocks list
randomIndex = randint(0, pixels_to_fill)
if randomIndex not in empty_mozaic:
continue
(i, j) = empty_mozaic[randomIndex]
index = find_average_color(Params.ref_image_resized
[i: i + H, j: j + W, :], avg_color_list)
img_mosaic[i: i + H, j: j + W, :] = \
Params.mosaic_pieces[index][:][:][:]
# mark the previously empty block as full
for row in range(i, (i + H + 1)):
for col in range(j, (j + W + 1)):
if randomIndex in empty_mozaic:
del (empty_mozaic[randomIndex])
print("Building mosaic. {} pieces to add".format(len(empty_mozaic)))
elif Params.rand_criterion == 'stochastic':
total_pieces_no = Params.number_mosaic_parts_horizontal * Params.number_mosaic_parts_vertical
total_generated_no = total_pieces_no * crossings_no
for i in range(0, total_generated_no):
i = randint(0, rangeH)
j = randint(0, rangeW)
index = find_average_color(Params.ref_image_resized
[i: i + H, j: j + W, :], avg_color_list)
img_mosaic[i: i + H, j: j + W, :] = \
Params.mosaic_pieces[index][:][:][:]
add_pieces_no += 1
print("Building mosaic. {}% ready", format(int(100 * add_pieces_no / total_generated_no)))
return img_mosaic
def find_average_color(img, avg_img_list, index_h=-1, index_w=-1):
_index = 1
distance = float('inf')
average_color_img = [img[:, :, i].mean() for i in range(img.shape[-1])]
if index_w == -1 & index_h == -1:
id = 0
for avg_color in avg_img_list:
dist = np.linalg.norm(np.array(average_color_img) - np.array(avg_color))
if dist < distance:
_index = id
distance = dist
id += 1
return _index
else:
id = 0
for avg_color in avg_img_list:
dist = np.linalg.norm(np.array(average_color_img) - np.array(avg_color))
if dist < distance:
if index_h == 0 and index_w == 0:
_index = id
distance = dist
if index_h > 0 and index_w == 0 and index_matrix[index_h - 1][index_w] != id:
_index = id
distance = dist
elif index_h == 0 and index_w > 0 \
and index_matrix[index_h][index_w - 1] != id:
_index = id
distance = dist
elif index_h > 0 and index_w > 0 \
and index_matrix[index_h - 1][index_w] != id \
and index_matrix[index_h][index_w - 1] != id:
_index = id
distance = dist
id += 1
index_matrix[index_h][index_w] = _index
return _index
def compute_average_color_list():
[N, _, _, _] = np.shape(Params.mosaic_pieces)
average_color = []
for i in range(0, N - 1):
image = Params.mosaic_pieces[i][:][:][:]
avg_color = [image[:, :, i].mean() for i in range(image.shape[-1])]
average_color.append(avg_color)
return average_color
def load_mosaic_parts():
print('Load mosaic pieces into memory')
dirPath = os.path.join('dataset/cifar-images/', str(Params.category))
files = os.listdir(dirPath)
im_piese = cv2.imread(dirPath + '/' + files[0])
[H, W, C] = np.shape(im_piese)
mosaic_pieces = np.zeros([len(files), H, W, C], np.uint8)
index = 0
for myFile in files:
image = cv2.imread(dirPath + '/' + myFile)
assert np.shape(image) == (H, W, C), "img %s has shape %r" % (myFile, image.shape)
image = np.asarray(image)
mosaic_pieces[index][:][:][:] = image
index += 1
Params.mosaic_pieces = mosaic_pieces
def add_mosaic_piece():
global index_matrix
img_mosaic = np.uint8(np.zeros(np.shape(Params.ref_image_resized)))
index_matrix = [[-1 for x in range(Params.number_mosaic_parts_horizontal + 2)] for y in
range(Params.number_mosaic_parts_vertical + 2)]
index_matrix = np.asarray(index_matrix)
img_mosaic = np.asarray(img_mosaic)
[_, H, W, _] = np.shape(Params.mosaic_pieces)
[h, w, c] = np.shape(Params.ref_image_resized)
total_pieces_no = Params.number_mosaic_parts_horizontal * Params.number_mosaic_parts_vertical
add_pieces_no = 0
avgColorList = compute_average_color_list()
for i in range(1, Params.number_mosaic_parts_vertical + 1):
for j in range(1, Params.number_mosaic_parts_horizontal + 1):
if Params.identical_matching_pieces == 1:
indice = find_average_color(
Params.ref_image_resized[(i - 1) * H: i * H, (j - 1) * W: j * W, :]
, avgColorList)
else:
indice = find_average_color(
Params.ref_image_resized[(i - 1) * H: i * H, (j - 1) * W: j * W, :]
, avgColorList, i - 1, j - 1)
img_mosaic[(i - 1) * H: i * H, (j - 1) * W: j * W, :] = \
Params.mosaic_pieces[indice][:][:][:]
add_pieces_no = add_pieces_no + 1
print("Building mosaic. {}% ready ".format(int(100 * add_pieces_no / total_pieces_no)))
return img_mosaic
def add_mosaic_piece_random():
img_mosaic = np.uint8(np.zeros(np.shape(Params.ref_image_resized)))
img_mosaic = np.asarray(img_mosaic)
[N, H, W, C] = np.shape(Params.mosaic_pieces)
[h, w, c] = np.shape(Params.ref_image_resized)
added_piece_no = 0
avg_color_list = compute_average_color_list()
crossing_no = Params.crossings_no
rangeH = h - H
rangeW = w - W
if Params.rand_criterion == 'try_hard':
empty_mosaic = {(row * rangeW + col): (row, col) for row in range(0, rangeH) for col in range(0, rangeW)}
pixels_to_fill = rangeH * rangeW
while empty_mosaic:
# get a random block to fill from the empty blocks list
randomIndex = randint(0, pixels_to_fill)
if randomIndex not in empty_mosaic:
continue
(i, j) = empty_mosaic[randomIndex]
indice = find_average_color(Params.ref_image_resized
[i: i + H, j: j + W, :], avg_color_list)
img_mosaic[i: i + H, j: j + W, :] = \
Params.mosaic_pieces[indice][:][:][:]
# mark the previously empty block as full
for row in range(i, (i + H + 1)):
for col in range(j, (j + W + 1)):
if randomIndex in empty_mosaic:
del (empty_mosaic[randomIndex])
print("Building mosaic. {} pieces left".format(len(empty_mosaic)))
elif Params.rand_criterion == 'stochastic':
total_no_pieces = Params.number_mosaic_parts_horizontal * Params.number_mosaic_parts_vertical
total_gen_number = total_no_pieces * crossing_no
for i in range(0, total_gen_number):
i = randint(0, rangeH)
j = randint(0, rangeW)
indice = find_average_color(Params.ref_image_resized
[i: i + H, j: j + W, :], avg_color_list)
img_mosaic[i: i + H, j: j + W, :] = \
Params.mosaic_pieces[indice][:][:][:]
added_piece_no = added_piece_no + 1
print("Building mosaic, {}% ready".format(int(100 * added_piece_no / total_gen_number)))
return img_mosaic
def find_average_color(img, avg_img_list, index_h=-1, index_w=-1):
_index = 1
distance = float('inf')
average_color_img = [img[:, :, i].mean() for i in range(img.shape[-1])]
if index_w == -1 & index_h == -1:
id = 0
for avg_color in avg_img_list:
dist = np.linalg.norm(np.array(average_color_img) - np.array(avg_color))
if dist < distance:
_index = id
distance = dist
id += 1
return _index
else:
id = 0
for avg_color in avg_img_list:
dist = np.linalg.norm(np.array(average_color_img) - np.array(avg_color))
if dist < distance:
if index_h == 0 and index_w == 0:
_index = id
distance = dist
if index_h > 0 and index_w == 0 and index_matrix[index_h - 1][index_w] != id:
_index = id
distance = dist
elif index_h == 0 and index_w > 0 \
and index_matrix[index_h][index_w - 1] != id:
_index = id
distance = dist
elif index_h > 0 and index_w > 0 \
and index_matrix[index_h - 1][index_w] != id \
and index_matrix[index_h][index_w - 1] != id:
_index = id
distance = dist
id += 1
index_matrix[index_h][index_w] = _index
return _index
# def computeAverageColorForAList():
# [N, _, _, _] = np.shape(Params.mosaic_pieces)
# average_color = []
#
# for i in range(0, N - 1):
# image = Params.mosaic_pieces[i][:][:][:]
# avg_color = [image[:, :, i].mean() for i in range(image.shape[-1])]
#
# average_color.append(avg_color)
#
# return average_color
def build_mosaic():
load_mosaic_parts()
compute_dimensions()
if args.arranging_way == 'krado':
img_mosaic = add_mosaic_piece_krodo()
elif args.arranging_way == 'random':
img_mosaic = add_mosaic_piece_random_position()
return img_mosaic
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Script to build a mosaic from pictures')
parser.add_argument('--ref_image_path', '-ref', type=str)
parser.add_argument('--save_path', type=str, default='mozaic.jpg')
parser.add_argument('--number_parts_horizontal', type=int, default=50)
parser.add_argument('--arranging_way', type=str, default='krado', choices=['krado', 'random'])
parser.add_argument('--random_criterion', type=str, default='stochastic', choices=['stochastic', 'try_hard'])
parser.add_argument('--crossings_no', type=int, default=2)
parser.add_argument('--identical_matching_pieces', type=int, default=10)
parser.add_argument('--category', type=int, default=1, choices=[x + 1 for x in range(10)])
args = parser.parse_args()
Params.update_params(args)
Params.refImage = cv2.imread(Params.ref_image_path, cv2.IMREAD_COLOR)
img_mozaic = build_mosaic()
print('Saving image to ' + args.save_path)
cv2.imwrite(args.save_path, img_mozaic)