diff --git a/src/machinevisiontoolbox/ImagePointFeatures.py b/src/machinevisiontoolbox/ImagePointFeatures.py index 96db9b28..771ae8dd 100644 --- a/src/machinevisiontoolbox/ImagePointFeatures.py +++ b/src/machinevisiontoolbox/ImagePointFeatures.py @@ -2285,24 +2285,8 @@ 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() @@ -2310,9 +2294,12 @@ def _image2feature( # 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) @@ -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 @@ -2383,7 +2370,7 @@ def SIFT(self, **kwargs: Any) -> "SIFTFeature": :seealso: :class:`SIFTFeature` `cv2.SIFT_create `_ """ - return self._image2feature(SIFTFeature, scale=True, orient=True, **kwargs) + return self._image2feature(SIFTFeature, **kwargs) def SURF(self, **kwargs: Any) -> "SURFFeature": """ @@ -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": """ @@ -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, ) @@ -2499,7 +2484,7 @@ def BRISK(self, **kwargs: Any) -> "BRISKFeature": :seealso: :class:`BRISKFeature` `cv2.BRISK_create `_ """ - return self._image2feature(BRISKFeature, scale=True, orient=True, **kwargs) + return self._image2feature(BRISKFeature, **kwargs) def AKAZE(self, **kwargs: Any) -> "AKAZEFeature": """ @@ -2535,7 +2520,7 @@ def AKAZE(self, **kwargs: Any) -> "AKAZEFeature": :class:`~machinevisiontoolbox.ImagePointFeatures.AKAZEFeature` `cv2.AKAZE `_ """ - return self._image2feature(AKAZEFeature, scale=True, orient=True, **kwargs) + return self._image2feature(AKAZEFeature, **kwargs) def Harris(self, **kwargs: Any) -> "HarrisFeature": r""" @@ -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), +} diff --git a/tests/test_image_point_features_extra.py b/tests/test_image_point_features_extra.py index 5567b370..3eda2500 100644 --- a/tests/test_image_point_features_extra.py +++ b/tests/test_image_point_features_extra.py @@ -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