Skip to content

Commit 7744712

Browse files
committed
fix: remove overlapping pixels in semantic segmentation masks
1 parent f1608a9 commit 7744712

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

luxonis_ml/data/loaders/luxonis_loader.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,24 @@ def _load_image_with_annotations(
316316
array = anns[0].combine_to_numpy(
317317
anns, self.class_mappings[task], width=width, height=height
318318
)
319-
if self.add_background and task == LabelType.SEGMENTATION:
320-
unassigned_pixels = ~np.any(array, axis=0)
321-
background_idx = self.class_mappings[task]["background"]
322-
array[background_idx, unassigned_pixels] = 1
319+
320+
if task == LabelType.SEGMENTATION:
321+
mask_sum = np.sum(array, axis=0)
322+
323+
if self.add_background:
324+
unassigned_pixels = mask_sum == 0
325+
background_idx = self.class_mappings[task]["background"]
326+
array[background_idx, unassigned_pixels] = 1
327+
328+
# Check if there are overlaps in the masks
329+
if np.any(mask_sum > 1):
330+
max_indices = np.argmax(array, axis=0)
331+
array.fill(0)
332+
array[
333+
max_indices,
334+
np.arange(array.shape[1])[:, None],
335+
np.arange(array.shape[2]),
336+
] = 1
323337

324338
labels[task] = (array, anns[0]._label_type)
325339

0 commit comments

Comments
 (0)