From 8432b90eb84d5ec4dea04631f087402bf61c1a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 22 Sep 2014 08:56:14 -0400 Subject: [PATCH] Add option to rotate around specific center --- skimage/transform/_warps.py | 15 +++++++++++---- skimage/transform/tests/test_warps.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index 27c92ff7..9ee5bb88 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -150,7 +150,8 @@ def rescale(image, scale, order=1, mode='constant', cval=0.): return resize(image, output_shape, order=order, mode=mode, cval=cval) -def rotate(image, angle, resize=False, order=1, mode='constant', cval=0.): +def rotate(image, angle, resize=False, order=1, mode='constant', cval=0., + center=None): """Rotate image by a certain angle around its center. Parameters @@ -180,6 +181,9 @@ def rotate(image, angle, resize=False, order=1, mode='constant', cval=0.): cval : float, optional Used in conjunction with mode 'constant', the value outside the image boundaries. + center : iterable of length 2 + The rotation center. If ``center=None``, the image is rotated around + its center, i.e. ``center=(rows / 2 - 0.5, cols / 2 - 0.5)``. Examples -------- @@ -198,10 +202,13 @@ def rotate(image, angle, resize=False, order=1, mode='constant', cval=0.): rows, cols = image.shape[0], image.shape[1] # rotation around center - translation = np.array((cols, rows)) / 2. - 0.5 - tform1 = SimilarityTransform(translation=-translation) + if center is None: + center = np.array((cols, rows)) / 2. - 0.5 + else: + center = np.asarray(center) + tform1 = SimilarityTransform(translation=-center) tform2 = SimilarityTransform(rotation=np.deg2rad(angle)) - tform3 = SimilarityTransform(translation=translation) + tform3 = SimilarityTransform(translation=center) tform = tform1 + tform2 + tform3 output_shape = None diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 0197c715..1d7ac473 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -152,6 +152,17 @@ def test_rotate_resize(): assert x45.shape == (14, 14) +def test_rotate_center(): + x = np.zeros((10, 10), dtype=np.double) + x[4, 4] = 1 + refx = np.zeros((10, 10), dtype=np.double) + refx[2, 5] = 1 + x20 = rotate(x, 20, order=0, center=(0, 0)) + assert_array_almost_equal(x20, refx) + x0 = rotate(x20, -20, order=0, center=(0, 0)) + assert_array_almost_equal(x0, x) + + def test_rescale(): # same scale factor x = np.zeros((5, 5), dtype=np.double)