From dedcee6543590b60309fdb846cc9104a1f938490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 16 Dec 2014 02:16:40 +0100 Subject: [PATCH] Fix keep_range and add test cases --- skimage/transform/_geometric.py | 10 +++++++--- skimage/transform/_warps.py | 13 +++++++++---- skimage/transform/tests/test_warps.py | 21 +++++++++++++++++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index 516ffd82..0b74fe48 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -4,8 +4,9 @@ import warnings import numpy as np from scipy import ndimage, spatial -from skimage._shared.utils import get_bound_method_class, safe_as_int -from skimage.util import img_as_float +from .._shared.utils import get_bound_method_class, safe_as_int +from ..util import img_as_float +from ..exposure import rescale_intensity from ._warps_cy import _warp_fast @@ -1148,7 +1149,10 @@ def warp(image, inverse_map=None, map_args={}, output_shape=None, order=1, if keep_range: image = image.astype(np.double) else: - image = img_as_float(image) + if image.dtype == np.double: + image = rescale_intensity(image) + else: + image = img_as_float(image) input_shape = np.array(image.shape) diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index 53f07faa..7ebcad6f 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -1,9 +1,11 @@ import numpy as np from scipy import ndimage -from skimage.transform._geometric import (warp, SimilarityTransform, - AffineTransform, _clip_warp_output) -from skimage.measure import block_reduce +from ..util import img_as_float +from ..exposure import rescale_intensity +from ..measure import block_reduce +from ._geometric import (warp, SimilarityTransform, AffineTransform, + _clip_warp_output) def resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, @@ -82,7 +84,10 @@ def resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, if keep_range: image = image.astype(np.double) else: - image = img_as_float(image) + if image.dtype == np.double: + image = rescale_intensity(image) + else: + image = img_as_float(image) out = ndimage.map_coordinates(image, coord_map, order=order, mode=mode, cval=cval) diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 240ab7d5..a18ad492 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -1,5 +1,5 @@ from numpy.testing import (assert_almost_equal, run_module_suite, - assert_array_equal, assert_raises) + assert_equal, assert_raises) import numpy as np from scipy.ndimage import map_coordinates @@ -236,13 +236,13 @@ def test_downscale_local_mean(): out1 = downscale_local_mean(image1, (2, 3)) expected1 = np.array([[ 4., 7.], [ 16., 19.]]) - assert_array_equal(expected1, out1) + assert_equal(expected1, out1) image2 = np.arange(5 * 8).reshape(5, 8) out2 = downscale_local_mean(image2, (4, 5)) expected2 = np.array([[ 14. , 10.8], [ 8.5, 5.7]]) - assert_array_equal(expected2, out2) + assert_equal(expected2, out2) def test_invalid(): @@ -255,7 +255,7 @@ def test_inverse(): tform = SimilarityTransform(scale=0.5, rotation=0.1) inverse_tform = SimilarityTransform(matrix=np.linalg.inv(tform.params)) image = np.arange(10 * 10).reshape(10, 10).astype(np.double) - assert_array_equal(warp(image, inverse_tform), warp(image, tform.inverse)) + assert_equal(warp(image, inverse_tform), warp(image, tform.inverse)) def test_slow_warp_nonint_oshape(): @@ -267,5 +267,18 @@ def test_slow_warp_nonint_oshape(): warp(image, lambda xy: xy, output_shape=(13.0001, 19.9999)) +def test_keep_range(): + image = np.linspace(0, 2, 25).reshape(5, 5) + + out = rescale(image, 2, keep_range=False, clip=True, order=0) + assert out.min() == 0 + assert out.max() == 1 + + out = rescale(image, 2, keep_range=True, clip=True, order=0) + assert out.min() == 0 + assert out.max() == 2 + + + if __name__ == "__main__": run_module_suite()