From b2036aee5c81a8a7f7caa9d39711c7100c222da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Thu, 30 Aug 2012 18:41:00 +0200 Subject: [PATCH] Add image resize function --- skimage/transform/__init__.py | 2 +- skimage/transform/_warps.py | 51 ++++++++++++++++++++++++++- skimage/transform/tests/test_warps.py | 11 +++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/skimage/transform/__init__.py b/skimage/transform/__init__.py index 511c8738..0907544b 100644 --- a/skimage/transform/__init__.py +++ b/skimage/transform/__init__.py @@ -5,4 +5,4 @@ from .integral import * from ._geometric import (warp, warp_coords, estimate_transform, SimilarityTransform, AffineTransform, ProjectiveTransform, PolynomialTransform) -from ._warps import rotate, swirl, homography +from ._warps import resize, rotate, swirl, homography diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index 4e5a7245..1ed0bed9 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -1,5 +1,54 @@ import numpy as np -from ._geometric import warp, SimilarityTransform +from ._geometric import warp, SimilarityTransform, AffineTransform + + +def resize(image, output_shape, order=1, mode='constant', cval=0.): + """Resize image. + + Parameters + ---------- + image : ndarray + Input image. + output_shape : tuple or ndarray + Size of the generated output image `(rows, cols)`. + + Returns + ------- + resized : ndarray + Resized version of the input. + + Other parameters + ---------------- + order : int + Order of splines used in interpolation. See + `scipy.ndimage.map_coordinates` for detail. + mode : string + How to handle values outside the image borders. See + `scipy.ndimage.map_coordinates` for detail. + cval : string + Used in conjunction with mode 'constant', the value outside + the image boundaries. + + """ + + rows, cols = output_shape + orig_rows, orig_cols = image.shape[0], image.shape[1] + + rscale = float(orig_rows) / rows + cscale = float(orig_cols) / cols + + # 3 control points necessary to estimate exact AffineTransform + src_corners = np.array([[1, 1], [1, rows], [cols, rows]]) - 1 + dst_corners = np.zeros(src_corners.shape, dtype=np.double) + # take into account that 0th pixel is at position (0.5, 0.5) + dst_corners[:, 0] = cscale * (src_corners[:, 0] + 0.5) - 0.5 + dst_corners[:, 1] = rscale * (src_corners[:, 1] + 0.5) - 0.5 + + tform = AffineTransform() + tform.estimate(src_corners, dst_corners) + + return warp(image, tform, output_shape=output_shape, order=order, + mode=mode, cval=cval) def rotate(image, angle, resize=False, order=1, mode='constant', cval=0.): diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 65514073..ac9272c6 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -2,7 +2,7 @@ from numpy.testing import assert_array_almost_equal, run_module_suite import numpy as np from scipy.ndimage import map_coordinates -from skimage.transform import (warp, warp_coords, rotate, +from skimage.transform import (warp, warp_coords, rotate, resize, AffineTransform, ProjectiveTransform, SimilarityTransform) @@ -81,6 +81,15 @@ def test_rotate(): assert_array_almost_equal(x90, np.rot90(x)) +def test_resize(): + x = np.zeros((5, 5), dtype=np.double) + x[1, 1] = 1 + resized = resize(x, (10, 10), order=0) + ref = np.zeros((10, 10)) + ref[2:4, 2:4] = 1 + assert_array_almost_equal(resized, ref) + + def test_swirl(): image = img_as_float(data.checkerboard())