-
Notifications
You must be signed in to change notification settings - Fork 1
/
inpanting_pyheal.py
303 lines (248 loc) · 10.5 KB
/
inpanting_pyheal.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
# # # # # # # # # # # HEADER # # # # # # # # # # #
# #
# STUDENT: Amanda Carrijo Viana Figur #
# N. USP: 8937736 #
# STUDENT: Luiz Augusto Vieira Manoel #
# N. USP: 8937308 #
# COURSE: Mestrado em Ciências de Computação e #
# Matemática Computacional (PPG-CCMC) #
# YEAR OF ENTRY: 2020/2019 #
# FINAL PROJECT #
# INPAINTING FROM PYHEAL #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # ##
from math import sqrt as sqrt
import heapq
import numpy as np
# flags
KNOWN = 0
BAND = 1
UNKNOWN = 2
# extremity values
INF = 1e6 # dont use np.inf to avoid inf * 0
EPS = 1e-6
# solves a step of the eikonal equation in order to find closest quadrant
def _solve_eikonal(y1, x1, y2, x2, height, width, dists, flags):
# check image frame
if y1 < 0 or y1 >= height or x1 < 0 or x1 >= width:
return INF
if y2 < 0 or y2 >= height or x2 < 0 or x2 >= width:
return INF
flag1 = flags[y1, x1]
flag2 = flags[y2, x2]
# both pixels are known
if flag1 == KNOWN and flag2 == KNOWN:
dist1 = dists[y1, x1]
dist2 = dists[y2, x2]
d = 2.0 - (dist1 - dist2) ** 2
if d > 0.0:
r = sqrt(d)
s = (dist1 + dist2 - r) / 2.0
if s >= dist1 and s >= dist2:
return s
s += r
if s >= dist1 and s >= dist2:
return s
# unsolvable
return INF
# only 1st pixel is known
if flag1 == KNOWN:
dist1 = dists[y1, x1]
return 1.0 + dist1
# only 2d pixel is known
if flag2 == KNOWN:
dist2 = dists[y2, x2]
return 1.0 + dist2
# no pixel is known
return INF
# returns gradient for one pixel, computed on 2 pixel range if possible
def _pixel_gradient(y, x, height, width, vals, flags):
val = vals[y, x]
# compute grad_y
prev_y = y - 1
next_y = y + 1
if prev_y < 0 or next_y >= height:
grad_y = INF
else:
flag_prev_y = flags[prev_y, x]
flag_next_y = flags[next_y, x]
if flag_prev_y != UNKNOWN and flag_next_y != UNKNOWN:
grad_y = (vals[next_y, x] - vals[prev_y, x]) / 2.0
elif flag_prev_y != UNKNOWN:
grad_y = val - vals[prev_y, x]
elif flag_next_y != UNKNOWN:
grad_y = vals[next_y, x] - val
else:
grad_y = 0.0
# compute grad_x
prev_x = x - 1
next_x = x + 1
if prev_x < 0 or next_x >= width:
grad_x = INF
else:
flag_prev_x = flags[y, prev_x]
flag_next_x = flags[y, next_x]
if flag_prev_x != UNKNOWN and flag_next_x != UNKNOWN:
grad_x = (vals[y, next_x] - vals[y, prev_x]) / 2.0
elif flag_prev_x != UNKNOWN:
grad_x = val - vals[y, prev_x]
elif flag_next_x != UNKNOWN:
grad_x = vals[y, next_x] - val
else:
grad_x = 0.0
return grad_y, grad_x
# compute distances between initial mask contour and pixels outside mask, using FMM (Fast Marching Method)
def _compute_outside_dists(height, width, dists, flags, band, radius):
band = band.copy()
orig_flags = flags
flags = orig_flags.copy()
# swap INSIDE / OUTSIDE
flags[orig_flags == KNOWN] = UNKNOWN
flags[orig_flags == UNKNOWN] = KNOWN
least_dist = 0.0
while band:
# reached radius limit, stop FMM
if least_dist >= radius * 2:
break
# pop BAND pixel closest to initial mask contour and flag it as KNOWN
_, x, y = heapq.heappop(band)
flags[x, y] = KNOWN
# process immediate neighbors (top/bottom/left/right)
neighbors = [(x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)]
for x_nb, y_nb in neighbors:
# skip out of frame
if x_nb < 0 or x_nb >= height or y_nb < 0 or y_nb >= width:
continue
# neighbor already processed, nothing to do
# search for no-eyeglasses pixels
if flags[x_nb, y_nb] != UNKNOWN:
continue
# compute neighbor distance to inital mask contour
least_dist = min([
_solve_eikonal(x_nb - 1, y_nb, x_nb, y_nb - 1, height, width, dists, flags),
_solve_eikonal(x_nb + 1, y_nb, x_nb, y_nb + 1, height, width, dists, flags),
_solve_eikonal(x_nb - 1, y_nb, x_nb, y_nb + 1, height, width, dists, flags),
_solve_eikonal(x_nb + 1, y_nb, x_nb, y_nb - 1, height, width, dists, flags)
])
dists[x_nb, y_nb] = least_dist
# add neighbor to narrow band
flags[x_nb, y_nb] = BAND
heapq.heappush(band, (least_dist, x_nb, y_nb))
# distances are opposite to actual FMM propagation direction, fix it
dists *= -1.0
# computes pixels distances to initial mask contour, flags, and narrow band queue
def _init(height, width, mask, radius):
# init all distances to infinity
dists = np.full((height, width), INF, dtype=float)
# sets the eyeglasses pixels as UNKNOWN and black region of mask as KNOWN
flags = mask.astype(int) * UNKNOWN
# narrow band, queue of contour pixels
band = []
# get all indices of pixels that represents the eyeglasses on mask
x_mask, y_mask = mask.nonzero()
# set the KWON neighbors pixels as BAND and its distance from UNKNOWN pixels as 0
for x, y in zip(x_mask, y_mask):
# look for BAND pixels in neighbors (top/bottom/left/right)
neighbors = [(x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)]
for x_nb, y_nb in neighbors:
# neighbor out of frame
if x_nb < 0 or x_nb >= height or y_nb < 0 or y_nb >= width:
continue
# neighbor already flagged as BAND
if flags[x_nb, y_nb] == BAND:
continue
# neighbor out of mask => mask contour
if mask[x_nb, y_nb] == 0:
flags[x_nb, y_nb] = BAND
dists[x_nb, y_nb] = 0.0
heapq.heappush(band, (0.0, x_nb, y_nb))
# compute distance to inital mask contour for KNOWN pixels (black region)
# (by inverting mask/flags and running FMM)
_compute_outside_dists(height, width, dists, flags, band, radius)
return dists, flags, band
# returns RGB values for pixel to by inpainted, computed for its neighborhood
def _inpaint_pixel(y, x, img, height, width, dists, flags, radius):
dist = dists[y, x]
# normal to pixel, ie direction of propagation of the FMM
dist_grad_y, dist_grad_x = _pixel_gradient(y, x, height, width, dists, flags)
pixel_sum = np.zeros((3), dtype=float)
weight_sum = 0.0
# iterate on each pixel in neighborhood (nb stands for neighbor)
for nb_y in range(y - radius, y + radius + 1):
# pixel out of frame
if nb_y < 0 or nb_y >= height:
continue
for nb_x in range(x - radius, x + radius + 1):
# pixel out of frame
if nb_x < 0 or nb_x >= width:
continue
# skip unknown pixels (including pixel being inpainted)
if flags[nb_y, nb_x] == UNKNOWN:
continue
# vector from point to neighbor
dir_y = y - nb_y
dir_x = x - nb_x
dir_length_square = dir_y ** 2 + dir_x ** 2
dir_length = sqrt(dir_length_square)
# pixel out of neighborhood
if dir_length > radius:
continue
# compute weight
# neighbor has same direction gradient => contributes more
dir_factor = abs(dir_y * dist_grad_y + dir_x * dist_grad_x)
if dir_factor == 0.0:
dir_factor = EPS
# neighbor has same contour distance => contributes more
nb_dist = dists[nb_y, nb_x]
level_factor = 1.0 / (1.0 + abs(nb_dist - dist))
# neighbor is distant => contributes less
dist_factor = 1.0 / (dir_length * dir_length_square)
weight = abs(dir_factor * dist_factor * level_factor)
pixel_sum[0] += weight * img[nb_y, nb_x, 0]
pixel_sum[1] += weight * img[nb_y, nb_x, 1]
pixel_sum[2] += weight * img[nb_y, nb_x, 2]
weight_sum += weight
return pixel_sum / weight_sum
# main inpainting function
def inpaint(original_img, mask, radius=5):
#set all black pixels to 0 and white pixels to 1
mask = mask[:, :, 0].astype(bool, copy=False)
img = original_img.copy()
height, width = img.shape[0:2]
dists, flags, band = _init(height, width, mask, radius)
# find next pixel to inpaint with FMM (Fast Marching Method)
# FMM advances the band of the mask towards its center,
# by sorting the area pixels by their distance to the initial contour
while band:
# pop band pixel
# get all indices of pixels that represents the eyeglasses on mask closest to initial mask contour
_, x, y = heapq.heappop(band)
# flag it as KNOWN
flags[x, y] = KNOWN
# process his immediate neighbors (top/bottom/left/right)
neighbors = [(x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)]
for x_nb, y_nb in neighbors:
# pixel out of frame
if x_nb < 0 or x_nb >= height or y_nb < 0 or y_nb >= width:
continue
# neighbor outside of initial mask or already processed, nothing to do
if flags[x_nb, y_nb] != UNKNOWN:
continue
# compute neighbor distance to inital mask contour
nb_dist = min([
_solve_eikonal(x_nb - 1, y_nb, x_nb, y_nb - 1, height, width, dists, flags),
_solve_eikonal(x_nb + 1, y_nb, x_nb, y_nb + 1, height, width, dists, flags),
_solve_eikonal(x_nb - 1, y_nb, x_nb, y_nb + 1, height, width, dists, flags),
_solve_eikonal(x_nb + 1, y_nb, x_nb, y_nb - 1, height, width, dists, flags)
])
dists[x_nb, y_nb] = nb_dist
# inpaint neighbor
pixel_vals = _inpaint_pixel(x_nb, y_nb, img, height, width, dists, flags, radius)
img[x_nb, y_nb, 0] = pixel_vals[0]
img[x_nb, y_nb, 1] = pixel_vals[1]
img[x_nb, y_nb, 2] = pixel_vals[2]
# add neighbor to narrow band
flags[x_nb, y_nb] = BAND
# push neighbor on band
heapq.heappush(band, (nb_dist, x_nb, y_nb))
return img