-
Notifications
You must be signed in to change notification settings - Fork 0
/
inpaint.py
281 lines (221 loc) · 9.05 KB
/
inpaint.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
from math import sqrt as sqrt
import heapq
import numpy as np
# flags
KNOWN = 0
BAND = 1
UNKNOWN = 2
# end values
INF = 1e6 # dont use np.inf to avoid inf * 0
EPS = 1e-6
# One step of eikonal equation
def _solve_eikonal(y1, x1, y2, x2, height, width, dists, flags):
# checking 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]
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
return INF
# only 1st pixel known
if flag1 == KNOWN:
dist1 = dists[y1, x1]
return 1.0 + dist1
# only 2d pixel known
if flag2 == KNOWN:
dist2 = dists[y2, x2]
return 1.0 + dist2
# no of the pixels are known
return INF
# Calculating gradient for one pixel
def _pixel_gradient(y, x, height, width, vals, flags):
val = vals[y, x]
# computing for 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
# computing for 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
# Computing distances between initial mask contour and pixels outside mask using Fast Marching Method
def _compute_outside_dists(height, width, dists, flags, band, radius):
band = band.copy()
orig_flags = flags
flags = orig_flags.copy()
flags[orig_flags == KNOWN] = UNKNOWN
flags[orig_flags == UNKNOWN] = KNOWN
last_dist = 0.0
double_radius = radius * 2
while band:
if last_dist >= double_radius:
break
_, y, x = heapq.heappop(band)
flags[y, x] = KNOWN
# immediate neighbors (top/bottom/left/right)
neighbors = [(y - 1, x), (y, x - 1), (y + 1, x), (y, x + 1)]
for nb_y, nb_x in neighbors:
if nb_y < 0 or nb_y >= height or nb_x < 0 or nb_x >= width:
continue
if flags[nb_y, nb_x] != UNKNOWN:
continue
# Computing neighbor distance to inital mask contour
last_dist = min([
_solve_eikonal(nb_y - 1, nb_x, nb_y, nb_x - 1, height, width, dists, flags),
_solve_eikonal(nb_y + 1, nb_x, nb_y, nb_x + 1, height, width, dists, flags),
_solve_eikonal(nb_y - 1, nb_x, nb_y, nb_x + 1, height, width, dists, flags),
_solve_eikonal(nb_y + 1, nb_x, nb_y, nb_x - 1, height, width, dists, flags)
])
dists[nb_y, nb_x] = last_dist
flags[nb_y, nb_x] = BAND
heapq.heappush(band, (last_dist, nb_y, nb_x))
dists *= -1.0
# Computing 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)
# status of each pixel, ie KNOWN, BAND or UNKNOWN
flags = mask.astype(int) * UNKNOWN
# narrow band, queue of contour pixels
band = []
mask_y, mask_x = mask.nonzero()
for y, x in zip(mask_y, mask_x):
# look for BAND pixels in neighbors (top/bottom/left/right)
neighbors = [(y - 1, x), (y, x - 1), (y + 1, x), (y, x + 1)]
for nb_y, nb_x in neighbors:
# neighbor out of frame
if nb_y < 0 or nb_y >= height or nb_x < 0 or nb_x >= width:
continue
# neighbor already flagged as BAND
if flags[nb_y, nb_x] == BAND:
continue
# neighbor out of mask => mask contour
if mask[nb_y, nb_x] == 0:
flags[nb_y, nb_x] = BAND
dists[nb_y, nb_x] = 0.0
heapq.heappush(band, (0.0, nb_y, nb_x))
# compute distance to inital mask contour for KNOWN pixels
# (by inverting mask/flags and running FFM)
_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 FFM
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(img, mask, radius=5):
if img.shape[0:2] != mask.shape[0:2]:
raise ValueError("Image and mask dimensions do not match")
height, width = img.shape[0:2]
dists, flags, band = _init(height, width, mask, radius)
# find next pixel to inpaint with FFM (Fast Marching Method)
# FFM 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 closest to initial mask contour
_, y, x = heapq.heappop(band)
# flag it as KNOWN
flags[y, x] = KNOWN
# process his immediate neighbors (top/bottom/left/right)
neighbors = [(y - 1, x), (y, x - 1), (y + 1, x), (y, x + 1)]
for nb_y, nb_x in neighbors:
# pixel out of frame
if nb_y < 0 or nb_y >= height or nb_x < 0 or nb_x >= width:
continue
# neighbor outside of initial mask or already processed, nothing to do
if flags[nb_y, nb_x] != UNKNOWN:
continue
# compute neighbor distance to inital mask contour
nb_dist = min([
_solve_eikonal(nb_y - 1, nb_x, nb_y, nb_x - 1, height, width, dists, flags),
_solve_eikonal(nb_y + 1, nb_x, nb_y, nb_x + 1, height, width, dists, flags),
_solve_eikonal(nb_y - 1, nb_x, nb_y, nb_x + 1, height, width, dists, flags),
_solve_eikonal(nb_y + 1, nb_x, nb_y, nb_x - 1, height, width, dists, flags)
])
dists[nb_y, nb_x] = nb_dist
# inpaint neighbor
pixel_vals = _inpaint_pixel(nb_y, nb_x, img, height, width, dists, flags, radius)
img[nb_y, nb_x, 0] = pixel_vals[0]
img[nb_y, nb_x, 1] = pixel_vals[1]
img[nb_y, nb_x, 2] = pixel_vals[2]
# add neighbor to narrow band
flags[nb_y, nb_x] = BAND
# push neighbor on band
heapq.heappush(band, (nb_dist, nb_y, nb_x))