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
53 changes: 28 additions & 25 deletions src/machinevisiontoolbox/ImagePointFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2285,34 +2285,21 @@ def _image2feature(
sortby: str | None = None,
nfeat: int | None = None,
id: str | int = "image",
scale: bool = False,
orient: bool = False,
**kwargs: Any,
) -> Any:
# https://datascience.stackexchange.com/questions/43213/freak-feature-extraction-opencv
algorithms: dict[str, Any] = {
"SIFT": getattr(cv2, "SIFT_create"),
"SURF": _SURF_create,
"ORB": getattr(cv2, "ORB_create"),
"Harris": _Harris_create,
# resolved only when used, so that a detector missing from this OpenCV
# (OpenCV 5 without the contrib modules) does not break the others
"BRISK": lambda **kw: _resolve_feature_create("BRISK")(**kw),
"AKAZE": lambda **kw: _resolve_feature_create("AKAZE")(**kw),
# 'FREAK': (cv2.FREAK_create, FREAKFeature),
# 'DAISY': (cv2.DAISY_create, DAISYFeature),
}

# check if image is valid
# TODO, MSER can handle color
image = self.mono()

# get a reference to the appropriate detector
algorithm = cls.__name__.replace("Feature", "")
try:
detector = algorithms[algorithm](**kwargs)
factory, has_scale, has_orient = _DETECTORS[algorithm]
except KeyError:
raise ValueError("bad algorithm specified")
raise ValueError("bad algorithm specified") from None

# every keyword argument belongs to the detector, none is claimed here
detector = factory(**kwargs)

kp, des = detector.detectAndCompute(image._A, mask=None)

Expand Down Expand Up @@ -2340,7 +2327,7 @@ def _image2feature(
des = des[:nfeat, :]

# construct a new Feature2DBase subclass
features = cls(kp, des, scale=scale, orient=orient)
features = cls(kp, des, scale=has_scale, orient=has_orient)

# add attributes
features._feature_type = algorithm
Expand Down Expand Up @@ -2383,7 +2370,7 @@ def SIFT(self, **kwargs: Any) -> "SIFTFeature":
:seealso: :class:`SIFTFeature` `cv2.SIFT_create <https://docs.opencv.org/4.5.2/d7/d60/classcv_1_1SIFT.html>`_
"""

return self._image2feature(SIFTFeature, scale=True, orient=True, **kwargs)
return self._image2feature(SIFTFeature, **kwargs)

def SURF(self, **kwargs: Any) -> "SURFFeature":
"""
Expand Down Expand Up @@ -2427,7 +2414,7 @@ def SURF(self, **kwargs: Any) -> "SURFFeature":

.. versionadded:: 2.5.0
"""
return self._image2feature(SURFFeature, scale=True, orient=True, **kwargs)
return self._image2feature(SURFFeature, **kwargs)

def ORB(self, scoreType: str = "harris", **kwargs: Any) -> "ORBFeature":
"""
Expand Down Expand Up @@ -2460,8 +2447,6 @@ def ORB(self, scoreType: str = "harris", **kwargs: Any) -> "ORBFeature":
scoreoptions = {"harris": cv2.ORB_HARRIS_SCORE, "fast": cv2.ORB_FAST_SCORE}
return self._image2feature(
ORBFeature,
scale=True,
orient=True,
scoreType=scoreoptions[scoreType],
**kwargs,
)
Expand Down Expand Up @@ -2499,7 +2484,7 @@ def BRISK(self, **kwargs: Any) -> "BRISKFeature":

:seealso: :class:`BRISKFeature` `cv2.BRISK_create <https://docs.opencv.org/4.5.2/d7/d60/classcv_1_1BRISK.html>`_
"""
return self._image2feature(BRISKFeature, scale=True, orient=True, **kwargs)
return self._image2feature(BRISKFeature, **kwargs)

def AKAZE(self, **kwargs: Any) -> "AKAZEFeature":
"""
Expand Down Expand Up @@ -2535,7 +2520,7 @@ def AKAZE(self, **kwargs: Any) -> "AKAZEFeature":
:class:`~machinevisiontoolbox.ImagePointFeatures.AKAZEFeature`
`cv2.AKAZE <https://docs.opencv.org/4.5.2/d7/d60/classcv_1_1AKAZE.html>`_
"""
return self._image2feature(AKAZEFeature, scale=True, orient=True, **kwargs)
return self._image2feature(AKAZEFeature, **kwargs)

def Harris(self, **kwargs: Any) -> "HarrisFeature":
r"""
Expand Down Expand Up @@ -2718,3 +2703,21 @@ def detectAndCompute(
"-v",
]
)


# What each detector behind Image.SIFT(), Image.ORB() etc. is built from, and what it
# provides. The flags are facts about the algorithm, so they live here rather than
# being passed in by each caller (where a keyword such as ``scale`` would collide with
# an option of the detector itself, as Harris has one). The factories are resolved
# only when used, so that a detector missing from this OpenCV (OpenCV 5 without the
# contrib modules) does not break the others.
#
# name: (factory, has scale, has orientation)
_DETECTORS: dict[str, tuple[Any, bool, bool]] = {
"SIFT": (lambda **kw: getattr(cv2, "SIFT_create")(**kw), True, True),
"SURF": (_SURF_create, True, True),
"ORB": (lambda **kw: getattr(cv2, "ORB_create")(**kw), True, True),
"BRISK": (lambda **kw: _resolve_feature_create("BRISK")(**kw), True, True),
"AKAZE": (lambda **kw: _resolve_feature_create("AKAZE")(**kw), True, True),
"Harris": (_Harris_create, False, False),
}
20 changes: 20 additions & 0 deletions tests/test_image_point_features_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,3 +1130,23 @@ def test_printing_an_orb_feature_shows_scale_and_orientation_in_degrees(
assert "scale=" in s and "orient=" in s
degrees = float(s.split("orient=")[1].split("°")[0])
assert 0 <= degrees < 360


class TestHarrisOptions:
"""Harris has an option called ``scale``, which must reach the detector."""

@pytest.fixture
def image(self):
return Image.Read("eiffel-1.png", mono=True)

def test_scale_is_the_suppression_distance(self, image):
near = image.Harris(scale=7)
far = image.Harris(scale=40)
# corners must be further apart, so there are fewer of them
assert len(far) < len(near)

def test_scale_does_not_claim_a_scale_per_feature(self, image):
f = image.Harris(scale=15)
assert (f._has_scale, f._has_orient) == (False, False)
with pytest.raises(ValueError, match="no scale"):
f.scale
Loading