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
12 changes: 8 additions & 4 deletions src/machinevisiontoolbox/ImageSpatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_image_spatial_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading