Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/machinevisiontoolbox/ImageRegionFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]}"
Expand Down
18 changes: 6 additions & 12 deletions tests/test_image_region_features_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading