MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / remove_small_regions

Function remove_small_regions

tasks/interactive_idino_m2m.py:108–132  ·  view source on GitHub ↗

Removes small disconnected regions and holes in a mask. Returns the mask and an indicator of if the mask has been modified.

(
    mask: np.ndarray, area_thresh: float, mode: str
)

Source from the content-addressed store, hash-verified

106 return reses,[reses[i] for i in ids]
107
108def remove_small_regions(
109 mask: np.ndarray, area_thresh: float, mode: str
110) -> Tuple[np.ndarray, bool]:
111 """
112 Removes small disconnected regions and holes in a mask. Returns the
113 mask and an indicator of if the mask has been modified.
114 """
115 import cv2 # type: ignore
116
117 assert mode in ["holes", "islands"]
118 correct_holes = mode == "holes"
119 working_mask = (correct_holes ^ mask).astype(np.uint8)
120 n_labels, regions, stats, _ = cv2.connectedComponentsWithStats(working_mask, 8)
121 sizes = stats[:, -1][1:] # Row 0 is background label
122 small_regions = [i + 1 for i, s in enumerate(sizes) if s < area_thresh]
123 if len(small_regions) == 0:
124 return mask, False
125 fill_labels = [0] + small_regions
126 if not correct_holes:
127 fill_labels = [i for i in range(n_labels) if i not in fill_labels]
128 # If every region is below threshold, keep largest
129 if len(fill_labels) == 0:
130 fill_labels = [int(np.argmax(sizes)) + 1]
131 mask = np.isin(regions, fill_labels)
132 return mask, True

Callers 1

interactive_infer_imageFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected