Skip to content

Commit

Permalink
Fix bug in median filter with non-uniform footprint (#521)
Browse files Browse the repository at this point in the history
closes #520

#485 Introduced a bug in rank filtering kernels that was not caught by our current test cases
(so this bug is only present in the 23.02 release)

This MR fixes the bug and adds test coverage for this case.

Authors:
  - Gregory Lee (https://github.com/grlee77)
  - https://github.com/jakirkham

Approvers:
  - https://github.com/jakirkham

URL: #521
  • Loading branch information
grlee77 committed Mar 8, 2023
1 parent 26283ce commit 33e59ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion python/cucim/src/cucim/skimage/_vendored/_ndimage_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,9 @@ def _rank_filter(input, get_rank, size=None, footprint=None, output=None,
sizes = footprint.shape
has_weights = False

if not has_weights:
footprint = None

rank = get_rank(filter_size)
if rank < 0 or rank >= filter_size:
raise RuntimeError('rank not within filter footprint size')
Expand All @@ -1126,7 +1129,7 @@ def _rank_filter(input, get_rank, size=None, footprint=None, output=None,
kernel = _get_rank_kernel(filter_size, rank, mode, footprint_shape,
offsets, float(cval), int_type,
has_weights=has_weights)
return _filters_core._call_kernel(kernel, input, None, output,
return _filters_core._call_kernel(kernel, input, footprint, output,
weights_dtype=bool)


Expand Down
15 changes: 15 additions & 0 deletions python/cucim/src/cucim/skimage/filters/tests/test_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cupyx.scipy import ndimage
from skimage import data

from cucim.skimage import morphology
from cucim.skimage._shared.testing import expected_warnings
from cucim.skimage.filters import median

Expand Down Expand Up @@ -249,3 +250,17 @@ def test_median_preserve_dtype(image, dtype):
)
def test_median(img, behavior):
median(img, behavior=behavior)


def test_median_nonsquare():
"""Test non-uniform footprint.
https://github.com/rapidsai/cucim/issues/520
"""
rng = cp.random.default_rng()
img = rng.integers(0, 256, (128, 128), dtype=cp.uint8)
footprint = morphology.disk(5)
mode = 'nearest'
out = median(img, footprint, mode=mode, behavior='ndimage')
expected = ndimage.median_filter(img, footprint=footprint, mode=mode)
cp.testing.assert_array_equal(out, expected)

0 comments on commit 33e59ca

Please sign in to comment.