Spaces:
Paused
Paused
Update utils/fill_regions.py
Browse files- utils/fill_regions.py +14 -13
utils/fill_regions.py
CHANGED
|
@@ -6,28 +6,24 @@ def fill_regions(binary):
|
|
| 6 |
|
| 7 |
h, w = binary.shape
|
| 8 |
|
| 9 |
-
|
| 10 |
-
(h, w, 3),
|
| 11 |
-
dtype=np.uint8
|
| 12 |
-
) * 255
|
| 13 |
-
|
| 14 |
-
# Close tiny gaps in hand-drawn lines
|
| 15 |
kernel = np.ones((3, 3), np.uint8)
|
| 16 |
|
| 17 |
-
|
| 18 |
binary,
|
|
|
|
| 19 |
kernel,
|
| 20 |
-
iterations=
|
| 21 |
)
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
mask = np.zeros(
|
| 26 |
(h + 2, w + 2),
|
| 27 |
np.uint8
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# Fill from outside
|
| 31 |
cv2.floodFill(
|
| 32 |
flood,
|
| 33 |
mask,
|
|
@@ -35,10 +31,16 @@ def fill_regions(binary):
|
|
| 35 |
255
|
| 36 |
)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
internal = cv2.bitwise_not(flood)
|
| 40 |
|
| 41 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
canvas[internal > 0] = [255, 0, 0]
|
| 43 |
|
| 44 |
return canvas
|
|
@@ -48,5 +50,4 @@ def overlay_boundaries(
|
|
| 48 |
filled,
|
| 49 |
binary
|
| 50 |
):
|
| 51 |
-
# No black outlines required
|
| 52 |
return filled
|
|
|
|
| 6 |
|
| 7 |
h, w = binary.shape
|
| 8 |
|
| 9 |
+
# Make boundaries thicker and close tiny gaps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
kernel = np.ones((3, 3), np.uint8)
|
| 11 |
|
| 12 |
+
closed = cv2.morphologyEx(
|
| 13 |
binary,
|
| 14 |
+
cv2.MORPH_CLOSE,
|
| 15 |
kernel,
|
| 16 |
+
iterations=2
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Fill outside background
|
| 20 |
+
flood = closed.copy()
|
| 21 |
|
| 22 |
mask = np.zeros(
|
| 23 |
(h + 2, w + 2),
|
| 24 |
np.uint8
|
| 25 |
)
|
| 26 |
|
|
|
|
| 27 |
cv2.floodFill(
|
| 28 |
flood,
|
| 29 |
mask,
|
|
|
|
| 31 |
255
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Internal regions only
|
| 35 |
internal = cv2.bitwise_not(flood)
|
| 36 |
|
| 37 |
+
# Create white canvas
|
| 38 |
+
canvas = np.ones(
|
| 39 |
+
(h, w, 3),
|
| 40 |
+
dtype=np.uint8
|
| 41 |
+
) * 255
|
| 42 |
+
|
| 43 |
+
# Fill internal regions with RED
|
| 44 |
canvas[internal > 0] = [255, 0, 0]
|
| 45 |
|
| 46 |
return canvas
|
|
|
|
| 50 |
filled,
|
| 51 |
binary
|
| 52 |
):
|
|
|
|
| 53 |
return filled
|