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
29 changes: 21 additions & 8 deletions src/machinevisiontoolbox/ImagePointFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,12 @@ def octave(self) -> "builtins.list[int]": # Octave of feature
return [kp.octave for kp in self._kp]

@property
@array_result
def descriptor(self) -> np.ndarray | None: # Descriptor of feature
"""
Descriptor of feature

:return: Descriptor
:rtype: ndarray(N,M)
:return: Descriptor, or None if the features have no descriptors
:rtype: ndarray(N,M) or None

Example:

Expand All @@ -566,8 +565,17 @@ def descriptor(self) -> np.ndarray | None: # Descriptor of feature
>>> orb[:5].descriptor.shape

.. note:: For single feature return a 1D array vector, for multiple features return a set of column vectors.

.. versionchanged:: 2.5.0
Returns None, instead of raising ``TypeError``, if the features have
no descriptors.
"""
return self._descriptor
des = self._descriptor
if des is None:
return None
# a single feature gives a 1D vector, as array_result does for the
# other properties
return des[0] if len(des) == 1 else des

@property
@array_result
Expand Down Expand Up @@ -731,8 +739,13 @@ def match(
# sorting
# return

des1 = self.descriptor
des2 = other.descriptor
if des1 is None or des2 is None:
raise ValueError("features have no descriptors")

if metric is None:
metric = "hamming" if self.descriptor.dtype == np.uint8 else "L2"
metric = "hamming" if des1.dtype == np.uint8 else "L2"

metricdict = {
"L1": cv2.NORM_L1,
Expand All @@ -755,7 +768,7 @@ def match(
else:
k = 2

matches0 = bf.knnMatch(self.descriptor, other.descriptor, k=k)
matches0 = bf.knnMatch(des1, des2, k=k)

# the elements of matches are:
# queryIdx: first feature set (self)
Expand Down Expand Up @@ -1837,7 +1850,7 @@ def p2(self) -> "builtins.list[np.ndarray]":

@property
@array_result
def descriptor1(self) -> "builtins.list[np.ndarray]":
def descriptor1(self) -> "builtins.list[np.ndarray | None]":
"""
Feature descriptor in first image

Expand All @@ -1859,7 +1872,7 @@ def descriptor1(self) -> "builtins.list[np.ndarray]":

@property
@array_result
def descriptor2(self) -> "builtins.list[np.ndarray]":
def descriptor2(self) -> "builtins.list[np.ndarray | None]":
"""
Feature descriptor in second image

Expand Down
14 changes: 8 additions & 6 deletions tests/test_image_point_features_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ def test_without_descriptors(self):
assert len(f) == 2
assert f._descriptor is None

@pytest.mark.xfail(
strict=True,
raises=TypeError,
reason="descriptor is typed as ndarray | None but array_result calls "
"len() on it, so it raises when there are no descriptors",
)
def test_descriptor_property_without_descriptors(self):
assert make_features(descriptors=False).descriptor is None

Expand Down Expand Up @@ -340,6 +334,14 @@ def test_different_sizes(self):
assert D.shape == (3, 4)


class TestMatchNeedsDescriptors:
def test_no_descriptors_is_a_clear_error(self):
with pytest.raises(ValueError, match="no descriptors"):
make_features(descriptors=False).match(make_features())
with pytest.raises(ValueError, match="no descriptors"):
make_features().match(make_features(descriptors=False))


class TestMatch:
@pytest.fixture
def pair(self):
Expand Down
Loading