From 8a98327ee937d938d82cda7a207f74f47b4ce185 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 20:58:26 +1000 Subject: [PATCH] fix: index and print MSER features correctly msers[0] left the region's 2xN point array in _points where a list of arrays belongs, so len(msers[0]) was 2 and str() called it "2 regions". It now wraps the array in a list, so the result is one region. str() of an MSERFeature with no regions looked up the first bounding box and raised IndexError. It now prints "MSER features, 0 regions". Also correct a comment that said the point arrays are Nx2, when they are 2xN. The two xfail tests that recorded the bugs are now ordinary tests. Co-Authored-By: Claude Sonnet 5.5 --- .../ImageRegionFeatures.py | 6 +++--- tests/test_image_region_features_extra.py | 18 ++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/machinevisiontoolbox/ImageRegionFeatures.py b/src/machinevisiontoolbox/ImageRegionFeatures.py index 4d8cf753..28e2ce8c 100644 --- a/src/machinevisiontoolbox/ImageRegionFeatures.py +++ b/src/machinevisiontoolbox/ImageRegionFeatures.py @@ -173,7 +173,7 @@ def __init__(self, image: Any = None, **kwargs: Any) -> None: self._points = [ np.asarray(mser).T for mser in msers - ] # transpose point arrays to be Nx2 + ] # transpose point arrays to be 2xN # --- OpenCV 4/5 compat --------------------------------------- # When no regions are found, OpenCV 5's detectRegions() returns # an empty tuple () for bboxes instead of an empty ndarray(0,4) @@ -244,7 +244,7 @@ def __getitem__(self, i: int | slice | np.ndarray | list | tuple) -> "MSERFeatur new = self.__class__() if isinstance(i, int): - new._points = self._points[i] # type: ignore[assignment] + new._points = [self._points[i]] new._bboxes = self._bboxes[np.newaxis, i, :] # result is 2D elif isinstance(i, slice): new._points = self._points[i] @@ -279,7 +279,7 @@ def __str__(self) -> str: >>> str(msers) >>> str(msers[0]) """ - if len(self) > 1: + if len(self) != 1: return f"MSER features, {len(self)} regions" else: s = f"MSER feature: u: {self._bboxes[0,0]} - {self._bboxes[0,2]}, v: {self._bboxes[0,1]} - {self._bboxes[0,3]}" diff --git a/tests/test_image_region_features_extra.py b/tests/test_image_region_features_extra.py index cda71e1c..a95def4c 100644 --- a/tests/test_image_region_features_extra.py +++ b/tests/test_image_region_features_extra.py @@ -84,13 +84,12 @@ def test_several_regions_give_lists(self, msers): assert len(msers.points) == 3 assert msers.bbox.shape == (3, 4) - @pytest.mark.xfail( - strict=True, - reason="an int index leaves the region's 2 x N point array in _points, " - "so len() of the result is 2, not 1", - ) def test_integer_index_is_one_region(self, msers): - assert len(msers[0]) == 1 + one = msers[0] + assert len(one) == 1 + nt.assert_array_equal(one.bbox, [1, 2, 5, 6]) + nt.assert_array_equal(one.points, [[1, 3, 5], [2, 4, 6]]) + assert str(one) == "MSER feature: u: 1 - 5, v: 2 - 6" class TestMSERPrinting: @@ -101,13 +100,8 @@ def test_several(self, msers): def test_single(self, msers): assert str(msers[2:]) == "MSER feature: u: 0 - 1, v: 0 - 1" - @pytest.mark.xfail( - strict=True, - raises=IndexError, - reason="str() of an object with no regions looks up the first bounding box", - ) def test_empty(self): - str(MSERFeature()) + assert str(MSERFeature()) == "MSER features, 0 regions" class TestMSERDetection: