diff --git a/src/machinevisiontoolbox/ImageBlobs.py b/src/machinevisiontoolbox/ImageBlobs.py index 9deb4f86..afcb90ae 100644 --- a/src/machinevisiontoolbox/ImageBlobs.py +++ b/src/machinevisiontoolbox/ImageBlobs.py @@ -637,7 +637,7 @@ def filter( return self[:] m: np.ndarray = np.array(mask).all(axis=0) # type: ignore[assignment] - return self[m] + return self[m.flatten()] def sort(self, by: str = "area", reverse: bool = False) -> "Blobs": # type: ignore[override] """ diff --git a/tests/test_image_blobs.py b/tests/test_image_blobs.py index 38330a12..f3de30a8 100644 --- a/tests/test_image_blobs.py +++ b/tests/test_image_blobs.py @@ -522,6 +522,19 @@ def test_filter_touch(self): removed = self.blobs4.filter(touch=True) self.assertEqual(len(removed), 0) + def test_filter_area_single_blob_keeps_match(self): + # Regression for GH issue #17: a single-blob Blobs mask came out + # 2D (from np.array(mask).all(axis=0)) and indexing self[m] with + # it dropped the blob even though it matched the filter. + area = self.blobs1[0].area + kept = self.blobs1.filter(area=area * 0.5) + self.assertEqual(len(kept), 1) + + def test_filter_area_single_blob_removes_non_match(self): + area = self.blobs1[0].area + removed = self.blobs1.filter(area=area * 2) + self.assertEqual(len(removed), 0) + # --- sort --------------------------------------------------------------- # def test_sort_returns_blobs(self):