Spaces:
Paused
Paused
Update utils/fill_regions.py
Browse files- utils/fill_regions.py +23 -36
utils/fill_regions.py
CHANGED
|
@@ -1,43 +1,30 @@
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
def fill_regions(binary):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
flood
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
255
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
internal = cv2.bitwise_not(
|
| 29 |
-
flood
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
canvas[
|
| 33 |
-
internal > 0
|
| 34 |
-
] = [255, 0, 0]
|
| 35 |
-
|
| 36 |
return canvas
|
| 37 |
|
| 38 |
-
|
| 39 |
-
def overlay_boundaries(
|
| 40 |
-
filled,
|
| 41 |
-
binary
|
| 42 |
-
):
|
| 43 |
return filled
|
|
|
|
| 1 |
+
# utils/fill_regions.py — replace fill_regions with this
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from utils.gap_closer import close_small_gaps # actually use it now
|
| 5 |
+
from config.settings import MIN_REGION_AREA
|
| 6 |
|
| 7 |
def fill_regions(binary):
|
| 8 |
+
# Close gaps MUCH more aggressively before flooding — this is the actual fix
|
| 9 |
+
strong_kernel = np.ones((7, 7), np.uint8)
|
| 10 |
+
closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, strong_kernel, iterations=3)
|
| 11 |
+
|
| 12 |
+
h, w = closed.shape
|
| 13 |
+
canvas = np.ones((h, w, 3), dtype=np.uint8) * 255
|
| 14 |
+
flood = closed.copy()
|
| 15 |
+
mask = np.zeros((h + 2, w + 2), np.uint8)
|
| 16 |
+
cv2.floodFill(flood, mask, (0, 0), 255)
|
| 17 |
+
internal = cv2.bitwise_not(flood)
|
| 18 |
+
|
| 19 |
+
# NEW: drop noise specks smaller than MIN_REGION_AREA (fixes the scattered dots)
|
| 20 |
+
num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(internal, connectivity=8)
|
| 21 |
+
clean = np.zeros_like(internal)
|
| 22 |
+
for i in range(1, num_labels):
|
| 23 |
+
if stats[i, cv2.CC_STAT_AREA] >= MIN_REGION_AREA:
|
| 24 |
+
clean[labels == i] = 255
|
| 25 |
+
|
| 26 |
+
canvas[clean > 0] = [255, 0, 0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return canvas
|
| 28 |
|
| 29 |
+
def overlay_boundaries(filled, binary):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return filled
|