Spaces:
Paused
Paused
Update utils/fill_regions.py
Browse files- utils/fill_regions.py +22 -12
utils/fill_regions.py
CHANGED
|
@@ -6,23 +6,33 @@ def fill_regions(binary):
|
|
| 6 |
|
| 7 |
h, w = binary.shape
|
| 8 |
|
|
|
|
| 9 |
canvas = np.ones(
|
| 10 |
(h, w, 3),
|
| 11 |
dtype=np.uint8
|
| 12 |
) * 255
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
kernel =
|
| 16 |
-
|
| 17 |
-
|
| 18 |
)
|
| 19 |
|
| 20 |
-
walls = cv2.
|
| 21 |
binary,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
kernel,
|
| 23 |
iterations=1
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
flood = walls.copy()
|
| 27 |
|
| 28 |
mask = np.zeros(
|
|
@@ -37,9 +47,8 @@ def fill_regions(binary):
|
|
| 37 |
255
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
|
| 44 |
num_labels, labels, stats, _ = \
|
| 45 |
cv2.connectedComponentsWithStats(
|
|
@@ -54,12 +63,14 @@ def fill_regions(binary):
|
|
| 54 |
cv2.CC_STAT_AREA
|
| 55 |
]
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
continue
|
| 59 |
|
|
|
|
| 60 |
canvas[
|
| 61 |
labels == label
|
| 62 |
-
] = [
|
| 63 |
|
| 64 |
return canvas
|
| 65 |
|
|
@@ -69,6 +80,5 @@ def overlay_boundaries(
|
|
| 69 |
binary
|
| 70 |
):
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
return filled
|
|
|
|
| 6 |
|
| 7 |
h, w = binary.shape
|
| 8 |
|
| 9 |
+
# White background
|
| 10 |
canvas = np.ones(
|
| 11 |
(h, w, 3),
|
| 12 |
dtype=np.uint8
|
| 13 |
) * 255
|
| 14 |
|
| 15 |
+
# Connect small gaps
|
| 16 |
+
kernel = cv2.getStructuringElement(
|
| 17 |
+
cv2.MORPH_ELLIPSE,
|
| 18 |
+
(3, 3)
|
| 19 |
)
|
| 20 |
|
| 21 |
+
walls = cv2.morphologyEx(
|
| 22 |
binary,
|
| 23 |
+
cv2.MORPH_CLOSE,
|
| 24 |
+
kernel,
|
| 25 |
+
iterations=2
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Slightly thicken the boundaries
|
| 29 |
+
walls = cv2.dilate(
|
| 30 |
+
walls,
|
| 31 |
kernel,
|
| 32 |
iterations=1
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Flood fill from outside
|
| 36 |
flood = walls.copy()
|
| 37 |
|
| 38 |
mask = np.zeros(
|
|
|
|
| 47 |
255
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# Internal regions only
|
| 51 |
+
internal = cv2.bitwise_not(flood)
|
|
|
|
| 52 |
|
| 53 |
num_labels, labels, stats, _ = \
|
| 54 |
cv2.connectedComponentsWithStats(
|
|
|
|
| 63 |
cv2.CC_STAT_AREA
|
| 64 |
]
|
| 65 |
|
| 66 |
+
# Ignore tiny specks
|
| 67 |
+
if area < 10:
|
| 68 |
continue
|
| 69 |
|
| 70 |
+
# PURE RED (RGB)
|
| 71 |
canvas[
|
| 72 |
labels == label
|
| 73 |
+
] = [255, 0, 0]
|
| 74 |
|
| 75 |
return canvas
|
| 76 |
|
|
|
|
| 80 |
binary
|
| 81 |
):
|
| 82 |
|
| 83 |
+
# No black outlines
|
|
|
|
| 84 |
return filled
|