-
Notifications
You must be signed in to change notification settings - Fork 3
/
util_trappedball_fill.py
511 lines (388 loc) · 15.2 KB
/
util_trappedball_fill.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# taken from LineFiller and slightly modified https://github.com/hepesu/LineFiller
import cv2
import numpy as np
from img_utils import read_image
import matplotlib.pyplot as plt
def get_ball_structuring_element(radius):
"""Get a ball shape structuring element with specific radius for morphology operation.
The radius of ball usually equals to (leaking_gap_size / 2).
# Arguments
radius: radius of ball shape.
# Returns
an array of ball structuring element.
"""
return cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * radius + 1, 2 * radius + 1))
def get_unfilled_point(image):
"""Get points belong to unfilled(value==255) area.
# Arguments
image: an image.
# Returns
an array of points.
"""
y, x = np.where(image == 255)
return np.stack((x.astype(int), y.astype(int)), axis=-1)
def exclude_area(image, radius):
"""Perform erosion on image to exclude points near the boundary.
We want to pick part using floodfill from the seed point after dilation.
When the seed point is near boundary, it might not stay in the fill, and would
not be a valid point for next floodfill operation. So we ignore these points with erosion.
# Arguments
image: an image.
radius: radius of ball shape.
# Returns
an image after dilation.
"""
return cv2.morphologyEx(image, cv2.MORPH_ERODE, get_ball_structuring_element(radius), anchor=(-1, -1), iterations=1)
def trapped_ball_fill_single(image, seed_point, radius):
"""Perform a single trapped ball fill operation.
# Arguments
image: an image. the image should consist of white background, black lines and black fills.
the white area is unfilled area, and the black area is filled area.
seed_point: seed point for trapped-ball fill, a tuple (integer, integer).
radius: radius of ball shape.
# Returns
an image after filling.
"""
ball = get_ball_structuring_element(radius)
pass1 = np.full(image.shape, 255, np.uint8)
pass2 = np.full(image.shape, 255, np.uint8)
im_inv = cv2.bitwise_not(image)
# Floodfill the image
mask1 = cv2.copyMakeBorder(im_inv, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0)
_, pass1, _, _ = cv2.floodFill(pass1, mask1, seed_point, 0, 0, 0, 4)
# Perform dilation on image. The fill areas between gaps became disconnected.
pass1 = cv2.morphologyEx(pass1, cv2.MORPH_DILATE, ball, anchor=(-1, -1), iterations=1)
mask2 = cv2.copyMakeBorder(pass1, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0)
# Floodfill with seed point again to select one fill area.
_, pass2, _, rect = cv2.floodFill(pass2, mask2, seed_point, 0, 0, 0, 4)
# Perform erosion on the fill result leaking-proof fill.
pass2 = cv2.morphologyEx(pass2, cv2.MORPH_ERODE, ball, anchor=(-1, -1), iterations=1)
return pass2
def trapped_ball_fill_multi(image, radius, method='mean', max_iter=1000):
"""Perform multi trapped ball fill operations until all valid areas are filled.
# Arguments
image: an image. The image should consist of white background, black lines and black fills.
the white area is unfilled area, and the black area is filled area.
radius: radius of ball shape.
method: method for filtering the fills.
'max' is usually with large radius for select large area such as background.
max_iter: max iteration number.
# Returns
an array of fills' points.
"""
print('trapped-ball ' + str(radius))
unfill_area = image
filled_area, filled_area_size, result = [], [], []
myimg = np.zeros((image.shape[0], image.shape[1]), dtype=int)
for _ in range(max_iter):
points = get_unfilled_point(exclude_area(unfill_area, radius))
if not len(points) > 0:
break
fill = trapped_ball_fill_single(unfill_area, (points[0][0], points[0][1]), radius)
unfill_area = cv2.bitwise_and(unfill_area, fill)
filled_area.append(np.where(fill == 0))
filled_area_size.append(len(np.where(fill == 0)[0]))
myimg[fill==0] = _
# plt.figure()
# plt.imshow(myimg, cmap="tab20", interpolation="nearest")
# plt.imsave("manyregions.png", myimg)
# plt.show()
filled_area_size = np.asarray(filled_area_size)
if method == 'max':
area_size_filter = np.max(filled_area_size)
elif method == 'median':
area_size_filter = np.median(filled_area_size)
elif method == 'mean':
area_size_filter = np.mean(filled_area_size)
else:
area_size_filter = 0
result_idx = np.where(filled_area_size >= area_size_filter)[0]
for i in result_idx:
result.append(filled_area[i])
return result
def flood_fill_single(im, seed_point):
"""Perform a single flood fill operation.
# Arguments
image: an image. the image should consist of white background, black lines and black fills.
the white area is unfilled area, and the black area is filled area.
seed_point: seed point for trapped-ball fill, a tuple (integer, integer).
# Returns
an image after filling.
"""
pass1 = np.full(im.shape, 255, np.uint8)
im_inv = cv2.bitwise_not(im)
mask1 = cv2.copyMakeBorder(im_inv, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0)
_, pass1, _, _ = cv2.floodFill(pass1, mask1, seed_point, 0, 0, 0, 4)
return pass1
def flood_fill_multi(image, max_iter=20000):
"""Perform multi flood fill operations until all valid areas are filled.
This operation will fill all rest areas, which may result large amount of fills.
# Arguments
image: an image. the image should contain white background, black lines and black fills.
the white area is unfilled area, and the black area is filled area.
max_iter: max iteration number.
# Returns
an array of fills' points.
"""
print('floodfill')
unfill_area = image
filled_area = []
for _ in range(max_iter):
points = get_unfilled_point(unfill_area)
if not len(points) > 0:
break
fill = flood_fill_single(unfill_area, (points[0][0], points[0][1]))
unfill_area = cv2.bitwise_and(unfill_area, fill)
filled_area.append(np.where(fill == 0))
return filled_area
def mark_fill(image, fills):
"""Mark filled areas with 0.
# Arguments
image: an image.
fills: an array of fills' points.
# Returns
an image.
"""
result = image.copy()
for fill in fills:
result[fill] = 0
return result
def build_fill_map(image, fills):
"""Make an image(array) with each pixel(element) marked with fills' id. id of line is 0.
# Arguments
image: an image.
fills: an array of fills' points.
# Returns
an array.
"""
result = np.zeros(image.shape[:2], int)
for index, fill in enumerate(fills):
result[fill] = index + 1
return result
def show_fill_map(fillmap):
"""Mark filled areas with colors. It is useful for visualization.
# Arguments
image: an image.
fills: an array of fills' points.
# Returns
an image.
"""
# Generate color for each fill randomly.
colors = np.random.randint(0, 255, (np.max(fillmap) + 1, 3))
# Id of line is 0, and its color is black.
colors[0] = [0, 0, 0]
return colors[fillmap]
def get_bounding_rect(points):
"""Get a bounding rect of points.
# Arguments
points: array of points.
# Returns
rect coord
"""
x1, y1, x2, y2 = np.min(points[1]), np.min(points[0]), np.max(points[1]), np.max(points[0])
return x1, y1, x2, y2
def get_border_bounding_rect(h, w, p1, p2, r):
"""Get a valid bounding rect in the image with border of specific size.
# Arguments
h: image max height.
w: image max width.
p1: start point of rect.
p2: end point of rect.
r: border radius.
# Returns
rect coord
"""
x1, y1, x2, y2 = p1[0], p1[1], p2[0], p2[1]
x1 = x1 - r if 0 < x1 - r else 0
y1 = y1 - r if 0 < y1 - r else 0
x2 = x2 + r + 1 if x2 + r + 1 < w else w
y2 = y2 + r + 1 if y2 + r + 1 < h else h
return x1, y1, x2, y2
def get_border_point(points, rect, max_height, max_width):
"""Get border points of a fill area
# Arguments
points: points of fill .
rect: bounding rect of fill.
max_height: image max height.
max_width: image max width.
# Returns
points , convex shape of points
"""
# Get a local bounding rect.
border_rect = get_border_bounding_rect(max_height, max_width, rect[:2], rect[2:], 2)
# Get fill in rect.
fill = np.zeros((border_rect[3] - border_rect[1], border_rect[2] - border_rect[0]), np.uint8)
# Move points to the rect.
fill[(points[0] - border_rect[1], points[1] - border_rect[0])] = 255
# Get shape.
contours, hierarchy = cv2.findContours(fill, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
approx_shape = cv2.approxPolyDP(contours[0], 0.02 * cv2.arcLength(contours[0], True), True)
# Get border pixel.
# Structuring element in cross shape is used instead of box to get 4-connected border.
cross = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
border_pixel_mask = cv2.morphologyEx(fill, cv2.MORPH_DILATE, cross, anchor=(-1, -1), iterations=1) - fill
border_pixel_points = np.where(border_pixel_mask == 255)
# Transform points back to fillmap.
border_pixel_points = (border_pixel_points[0] + border_rect[1], border_pixel_points[1] + border_rect[0])
return border_pixel_points, approx_shape
def merge_fill(fillmap, max_iter=10):
"""Merge fill areas.
# Arguments
fillmap: an image.
max_iter: max iteration number.
# Returns
an image.
"""
max_height, max_width = fillmap.shape[:2]
result = fillmap.copy()
for i in range(max_iter):
print('merge ' + str(i + 1))
result[np.where(fillmap == 0)] = 0
fill_id = np.unique(result.flatten())
fills = []
for j in fill_id:
point = np.where(result == j)
fills.append({
'id': j,
'point': point,
'area': len(point[0]),
'rect': get_bounding_rect(point)
})
for j, f in enumerate(fills):
# ignore lines
if f['id'] == 0:
continue
border_points, approx_shape = get_border_point(f['point'], f['rect'], max_height, max_width)
border_pixels = result[border_points]
pixel_ids, counts = np.unique(border_pixels, return_counts=True)
ids = pixel_ids[np.nonzero(pixel_ids)]
new_id = f['id']
if len(ids) == 0:
# points with lines around color change to line color
# regions surrounded by line remain the same
if f['area'] < 5:
new_id = 0
else:
# region id may be set to region with largest contact
new_id = ids[0]
# a point
if len(approx_shape) == 1 or f['area'] == 1:
result[f['point']] = new_id
#
if len(approx_shape) in [2, 3, 4, 5] and f['area'] < 500:
result[f['point']] = new_id
if f['area'] < 250 and len(ids) == 1:
result[f['point']] = new_id
if f['area'] < 50:
result[f['point']] = new_id
if len(fill_id) == len(np.unique(result.flatten())):
break
return result
def remove_small_regions(
fillmap
):
values = sorted(np.unique(fillmap.flatten()))
for idx, val in enumerate(values):
if np.sum(fillmap == val) < 20:
fillmap[fillmap == val] = 0
return fillmap
def simplify_fillmap(
fillmap
):
values = sorted(np.unique(fillmap.flatten()))
for idx, val in enumerate(values):
fillmap[fillmap == val] = idx
return fillmap
def fillmap_sort_by_area(
fillmap,
):
region_idx = set(np.unique(fillmap.flatten()))
region_idx.remove(0)
region_idx.remove(1)
old_regions = sorted(list(region_idx))
old_regions_with_area = [
(i, np.sum(fillmap == i))
for i in old_regions
]
sorted_old_regions = sorted(old_regions_with_area, key=lambda x: x[1], reverse=True)
print("sorted regions: ", sorted_old_regions)
newfillmap = np.copy(fillmap)
for i_sorted in range(len(sorted_old_regions)):
new_id = 2 + i_sorted
old_id = sorted_old_regions[i_sorted][0]
newfillmap[fillmap == old_id] = new_id
print(old_id, " -> ", new_id)
return newfillmap
def my_trapped_ball(
img: np.array,
first_ball=4,
):
"""
given input image as flat HxW, outputs same size integer mask with region correspondance
:param img: np.array, 255 - white, 0 - black (lines)
:return:
"""
assert np.max(img) > 1
assert np.median(img) > 1
ret, binary = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY)
fills = []
result = binary
# fill = trapped_ball_fill_multi(result, first_ball, method=None)
fill = trapped_ball_fill_multi(result, first_ball, method='mean')
fills += fill
result = mark_fill(result, fill)
fill = trapped_ball_fill_multi(result, 2, method=None)
fills += fill
result = mark_fill(result, fill)
fill = trapped_ball_fill_multi(result, 1, method=None)
fills += fill
result = mark_fill(result, fill)
fill = flood_fill_multi(result)
fills += fill
fillmap = build_fill_map(result, fills)
fillmap = merge_fill(fillmap)
fillmap = remove_small_regions(fillmap)
fillmap = simplify_fillmap(fillmap)
fillmap = fillmap_sort_by_area(fillmap)
return fillmap
def my_line_thinner(fillmap):
"""
@param fillmap:
@return:
"""
newfillmap = np.copy(fillmap)
U, V = np.indices(dimensions=fillmap.shape, dtype=int)
line_points_mask = fillmap == 0
junction_points_u = U[line_points_mask]
junction_points_v = V[line_points_mask]
for i_point in range(len(junction_points_v)):
window_size = 1
u_point = junction_points_u[i_point]
v_point = junction_points_v[i_point]
window = fillmap[
u_point-window_size:u_point+window_size+1,
v_point-window_size:v_point+window_size+1
]
uniques = set(np.unique(window))
uniques.discard(0)
uniques.discard(1) # if near background, keep it
if len(uniques) == 1:
newfillmap[u_point, v_point] = uniques.pop()
return newfillmap
def build_fillmap_dicts(
fillmap,
):
n_patches = np.max(fillmap)
def dict_patch_template(): return dict({i: np.array([], dtype=int) for i in range(n_patches + 1)})
if __name__ == "__main__":
name = "chair37"
im_path = f"results/{name}/{name}_resized512.png"
input_image = read_image(im_path)[...,0]
print(input_image.shape)
image_for_trapped_ball = ((1 - input_image) * 255).astype(np.uint8)
input_fillmap = my_trapped_ball(image_for_trapped_ball)
plt.imsave("test.png", input_fillmap, cmap="tab20b", vmin=-0.5, vmax=19.5)
plt.figure()
plt.imsave('manyregions2.png', input_fillmap, cmap="tab20b")
plt.imshow(input_fillmap, cmap="tab20b", vmin=-0.5, vmax=19.5, interpolation="nearest")
plt.show()