diff --git a/src/machinevisiontoolbox/ImageSpatial.py b/src/machinevisiontoolbox/ImageSpatial.py index 23a98018..b996f6f1 100644 --- a/src/machinevisiontoolbox/ImageSpatial.py +++ b/src/machinevisiontoolbox/ImageSpatial.py @@ -875,7 +875,7 @@ def rankfilter( self, footprint: np.ndarray | None = None, h: int | None = None, - rank: int | str = -1, + rank: int | str = "median", border: str = "replicate", bordervalue: int | float = 0, ) -> Any: @@ -886,8 +886,8 @@ def rankfilter( :type footprint: ndarray(N,M), optional :param h: half width of structuring element :type h: int, optional - :param rank: rank of filter - :type rank: int, str + :param rank: rank of filter, defaults to ``"median"`` + :type rank: int, str, optional :param border: option for boundary handling, defaults to 'replicate' :type border: str, optional :param bordervalue: padding value, defaults to 0 @@ -928,6 +928,10 @@ def rankfilter( :references: - |RVC3|, Section 11.5.3. + .. versionchanged:: 2.5.0 + ``rank`` now defaults to ``"median"``. The previous default of -1 + was always rejected, so ``rank`` had to be given. + :seealso: :obj:`scipy.ndimage.rank_filter` """ if h is not None: @@ -971,7 +975,7 @@ def rank( self, footprint: np.ndarray | None = None, h: int | None = None, - rank: int | str = -1, + rank: int | str = "median", border: str = "replicate", bordervalue: int | float = 0, ) -> Any: diff --git a/tests/test_image_spatial_extra.py b/tests/test_image_spatial_extra.py index d7ff5395..34825335 100644 --- a/tests/test_image_spatial_extra.py +++ b/tests/test_image_spatial_extra.py @@ -347,6 +347,17 @@ def test_named_ranks(self, noisy, rank, reference): expected = reference(noisy.array, size=5, mode="nearest") nt.assert_array_equal(out.array, expected) + def test_default_rank_is_the_median(self, noisy): + out = noisy.rankfilter(h=2) + expected = ndi.median_filter(noisy.array, size=5, mode="nearest") + nt.assert_array_equal(out.array, expected) + nt.assert_array_equal(out.array, noisy.rankfilter(h=2, rank="median").array) + + def test_deprecated_rank_method_has_the_same_default(self, noisy): + with pytest.warns(DeprecationWarning): + out = noisy.rank(h=1) + nt.assert_array_equal(out.array, noisy.rankfilter(h=1).array) + def test_integer_rank_zero_is_the_maximum(self, noisy): # rank 0 selects the largest value of the window out = noisy.rankfilter(h=1, rank=0)