-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarve.py
349 lines (274 loc) · 10.2 KB
/
carve.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
import sys
import threading
from multiprocessing import Pool
import cv2
import fire
import numpy as np
from PIL import Image
from skimage.filters.rank import entropy as skimage_entropy
from skimage.morphology import disk as skimage_disk
from tqdm import tqdm
def entropy_simple(pixels):
"""Calculate the entropy for a given image in grayscale."""
footprint = skimage_disk(3)
r = pixels[:, :, 0] * 0.2989
g = pixels[:, :, 1] * 0.5870
b = pixels[:, :, 2] * 0.1140
grayscale = np.array(r + g + b, dtype="uint8")
ent = skimage_entropy(grayscale, footprint)
scale = 255 / np.max(ent)
return ent * scale
def entropy_3ch(pixels):
"""Calculate the entropy for a given image on 3 channels."""
footprint = skimage_disk(3)
r = pixels[:, :, 0].astype("uint8")
g = pixels[:, :, 1].astype("uint8")
b = pixels[:, :, 2].astype("uint8")
with Pool(processes=3) as pool:
ret_r = pool.apply_async(skimage_entropy, (r, footprint))
ret_g = pool.apply_async(skimage_entropy, (g, footprint))
ret_b = pool.apply_async(skimage_entropy, (b, footprint))
entropy_r = ret_r.get()
entropy_g = ret_g.get()
entropy_b = ret_b.get()
ent = np.array(entropy_r + entropy_g + entropy_b)
scale = 255 / np.max(ent)
return ent * scale
def saliency_spectral(pixels):
"""Calculate the saliency map for a given image using OpenCV."""
saliency = cv2.saliency.StaticSaliencySpectralResidual_create()
(success, saliencyMap) = saliency.computeSaliency(pixels.astype("uint8"))
if not success:
raise ValueError("Cannot compute saliency.")
return saliencyMap * 255
def saliency_fine(pixels):
"""Calculate the saliency map for a given image using OpenCV."""
saliency = cv2.saliency.StaticSaliencyFineGrained_create()
(success, saliencyMap) = saliency.computeSaliency(pixels.astype("uint8"))
if not success:
raise ValueError("Cannot compute saliency.")
return saliencyMap * 255
def gradient_magnitude(pixels):
"""Calculate the gradient magnitude for each pixel in an image.
energy of (x, y) = sqrt(dx + dy)
dx = sum(((x+1, y)[c] - (x-1, y)[r])**2 for c in [r, g, b])
dy = sum(((x, y+1)[c] - (x, y-1)[r])**2 for c in [r, g, b])
in other terms
dx = sum((left-right)**2 for c in [r, g, b])
dy = sum((up-down)**2 for c in [r, g, b])
"""
# shifted images
up = np.roll(pixels, -1, axis=0)
down = np.roll(pixels, +1, axis=0)
left = np.roll(pixels, -1, axis=1)
right = np.roll(pixels, +1, axis=1)
# handle edges
up[-1] = up[-2].copy()
down[0] = down[1].copy()
left[:, -1] = left[:, -2].copy()
right[:, 0] = right[:, 1].copy()
dx = np.sum((left - right) ** 2, axis=2)
dy = np.sum((up - down) ** 2, axis=2)
return np.sqrt(dx + dy)
def saliency_plus_gradient(pixels):
return saliency_spectral(pixels) / 2 + gradient_magnitude(pixels) / 2
def get_seam(distances):
"""Return a list of column indices so that seam[row]=seam_column."""
height, width = distances.shape
if distances.shape[0] == 1:
return [int(np.argmin(distances))]
last_row = distances[-1]
second_last_row = distances[-2]
last_row_C = np.roll(last_row, 0, axis=0)
last_row_L = np.roll(last_row, +1, axis=0)
last_row_R = np.roll(last_row, -1, axis=0)
# disable the first and last element of the rolled arrays because they were wrapped
infty = np.max(last_row + second_last_row)
last_row_L[0] = infty
last_row_R[-1] = infty
distances[-2] = np.amin(
[
second_last_row + last_row_C,
second_last_row + last_row_L,
second_last_row + last_row_R,
],
axis=0,
)
try:
seam = get_seam(distances[:-1])
except RecursionError:
print("Recursion depth exceeded.")
print("Image height must not exceed max recursion depth.")
sys.exit(0)
last_index = seam[-1]
candidate_indices = [last_index, last_index - 1, last_index + 1]
if -1 in candidate_indices:
candidate_indices.remove(-1)
if width in candidate_indices:
candidate_indices.remove(width)
candidate_values = [last_row[c] for c in candidate_indices]
best_candidate_idx = np.argmin(candidate_values)
next_index = candidate_indices[best_candidate_idx]
seam.append(int(next_index))
return seam
def mark_seam(image, seam, mark_colour=(255, 255, 255)):
"""Colour all pixels in a seam, given an array as returned by get_seam."""
pixels = image.load()
for row in range(len(seam)):
pixels[seam[row], row] = mark_colour
def remove_seam(pixels, seam):
"""Create a copy of an image without the seam."""
height, old_width, _ = pixels.shape
new_width = old_width - 1
arr = np.zeros((height, new_width, 3))
for row in range(height):
new_row = (pixels[row][: seam[row]], pixels[row][seam[row] + 1 :])
arr[row] = np.concatenate(new_row, axis=0)
return Image.fromarray(np.uint8(arr))
def insert_seam(pixels, seam):
"""Create a copy of an image with an extra seam."""
raise NotImplementedError("oops")
height, old_width, _ = pixels.shape
new_width = old_width + 1
arr = np.zeros((height, new_width, 3))
for row in range(height):
new_row = (pixels[row][: seam[row]], """TODO""", pixels[row][seam[row] + 1 :])
arr[row] = np.concatenate(new_row, axis=0)
return Image.fromarray(np.uint8(arr))
def save_seam(image, seam, filename):
mark_seam(image, seam)
save_image(image, filename)
def save_image(img, filename):
img.save(filename)
def carve(
in_filename,
out_filename,
*_,
iteration_count=1000,
save_shrunk=True,
save_seamed=False,
save_energy=False,
energy_function=0,
silent=False,
):
get_energy = energy_functions[energy_function]
image = Image.open(in_filename).convert("RGB")
# height of the image must not exceed the recursion limit
sys.setrecursionlimit(np.max([image.size[1], 1500]))
threads = []
iterator = range(iteration_count) if silent else tqdm(range(iteration_count))
for i in iterator:
seam_filename = f"{out_filename}_{str(i).zfill(4)}_seamed.png"
shrunk_filename = f"{out_filename}_{str(i).zfill(4)}_shrunk.png"
energy_filename = f"{out_filename}_{str(i).zfill(4)}_energy.png"
img_arr = np.array(image, dtype=np.longlong)
energy = get_energy(img_arr)
# save the energy map
if save_energy:
e_image = Image.fromarray(energy).convert("RGB")
x = threading.Thread(target=save_image, args=(e_image, energy_filename))
x.start()
threads.append(x)
seam = get_seam(energy)
# mark seam and save
if save_seamed:
x = threading.Thread(target=save_seam, args=(image, seam, seam_filename))
x.start()
threads.append(x)
shrunk = remove_seam(img_arr, seam)
# remove seam and save
if save_shrunk:
x = threading.Thread(target=save_image, args=(shrunk, shrunk_filename))
x.start()
threads.append(x)
image = shrunk
# wait for everything to finish saving
for thread in threads:
thread.join()
save_image(image, f"{out_filename}_final.png")
return image
def amplify(
in_filename,
out_filename,
*_,
iteration_count=1000,
step=1,
save_intermediate=True,
energy_function=0,
silent=False,
):
"""Content amplification."""
r = range(0, iteration_count, step)
iterator = r if silent else tqdm(r)
for i in iterator:
img = carve(
in_filename=in_filename,
out_filename=out_filename,
iteration_count=step,
save_shrunk=False,
save_seamed=False,
save_energy=False,
energy_function=energy_function,
silent=True,
)
new_size = np.sum((img.size, (step, 0)), axis=0)
img = img.resize(new_size)
suffix = f"_{str(i).zfill(3)}.png" if save_intermediate else ".png"
img.save(out_filename + suffix)
in_filename = out_filename + suffix
# TODO: rm out_filename + "_final.png"
return img
def deamplify():
"""Content de-amplification."""
raise NotImplementedError("oops")
def insert(*_, energy_function=0):
"""Seam insertion."""
raise NotImplementedError("oops")
def energy_demo(in_filename, out_filename, *_, energy_function=0):
"""Test an energy function on an image. Output only the energy function."""
carve(
in_filename=in_filename,
out_filename=out_filename,
iteration_count=1,
save_seamed=False,
save_shrunk=False,
save_energy=True,
energy_function=energy_function,
)
def help():
print(f"Usage: {sys.argv[0]} [command] [in_filename] [out_filename]")
print(" command is one of: [carve, test, enlarge]")
print(" in_filename is a path to the input image")
print(" out_filename is a path to the output image, without the extension")
print()
print("Optional parameters:")
for cmd_name, cmd_function in commands.items():
print(f"* {cmd_name}")
if cmd_function.__kwdefaults__ is None:
print(" (no additional parameters)")
else:
for name, value in cmd_function.__kwdefaults__.items():
print(" --", name, "=", value, sep="")
print()
print()
print(f"energy_function is the index of the energy function:")
for i, f in enumerate(energy_functions):
print("", i, f.__name__)
energy_functions = [
gradient_magnitude,
saliency_spectral,
saliency_fine,
saliency_plus_gradient,
entropy_3ch,
entropy_simple,
]
commands = {
"carve": carve,
"test": energy_demo,
"amplify": amplify,
"deamplify": deamplify,
"insert": insert,
"help": help,
}
if __name__ == "__main__":
fire.Fire(commands)