Skip to content

Commit

Permalink
Handle NaNs when clipping in transform.resize (#6852)
Browse files Browse the repository at this point in the history
resize(img, clip=True, ...) didn't account for NaN in the image when 
computing the image bounds. This can be achieved by simply doing what 
the `warp` function does, by passing it to `_clip_warp_output`.

Co-authored-by: Lars Grüter <lagru+github@mailbox.org>
  • Loading branch information
scott-vsi and lagru committed Mar 27, 2023
1 parent c90bfcc commit 081baae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 6 additions & 7 deletions skimage/transform/_warps.py
Expand Up @@ -162,9 +162,6 @@ def resize(image, output_shape, order=None, mode='reflect', cval=0, clip=True,
if order > 0:
image = convert_to_float(image, preserve_range)

# Save input value range for clip
img_bounds = np.array([image.min(), image.max()]) if clip else None

# Translate modes used by np.pad to those used by scipy.ndimage
ndi_mode = _to_ndimage_mode(mode)
if anti_aliasing:
Expand All @@ -179,14 +176,16 @@ def resize(image, output_shape, order=None, mode='reflect', cval=0, clip=True,
elif np.any((anti_aliasing_sigma > 0) & (factors <= 1)):
warn("Anti-aliasing standard deviation greater than zero but "
"not down-sampling along all axes")
image = ndi.gaussian_filter(image, anti_aliasing_sigma,
cval=cval, mode=ndi_mode)
filtered = ndi.gaussian_filter(image, anti_aliasing_sigma,
cval=cval, mode=ndi_mode)
else:
filtered = image

zoom_factors = [1 / f for f in factors]
out = ndi.zoom(image, zoom_factors, order=order, mode=ndi_mode,
out = ndi.zoom(filtered, zoom_factors, order=order, mode=ndi_mode,
cval=cval, grid_mode=True)

_clip_warp_output(img_bounds, out, mode, cval, clip)
_clip_warp_output(image, out, mode, cval, clip)

return out

Expand Down
4 changes: 3 additions & 1 deletion skimage/transform/tests/test_warps.py
Expand Up @@ -446,10 +446,12 @@ def test_resize_clip(order, preserve_range, anti_aliasing, dtype):
x = np.ones((5, 5), dtype=dtype)
if dtype == np.uint8:
x *= 255
else:
x[0, 0] = np.NaN
resized = resize(x, (3, 3), order=order, preserve_range=preserve_range,
anti_aliasing=anti_aliasing)

assert resized.max() == expected_max
assert np.nanmax(resized) == expected_max


@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
Expand Down

0 comments on commit 081baae

Please sign in to comment.