diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index 2f2cf553..1979e8d4 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -251,16 +251,21 @@ def rotate(image, angle, resize=False, center=None, order=1, mode='constant', center = np.array((cols, rows)) / 2. - 0.5 else: center = np.asarray(center) - tform1 = SimilarityTransform(translation=-center) + tform1 = SimilarityTransform(translation=center) tform2 = SimilarityTransform(rotation=np.deg2rad(angle)) - tform3 = SimilarityTransform(translation=center) - tform = tform1 + tform2 + tform3 + tform3 = SimilarityTransform(translation=-center) + tform = tform3 + tform2 + tform1 output_shape = None if resize: # determine shape of output image - corners = np.array([[1, 1], [1, rows], [cols, rows], [cols, 1]]) - corners = tform(corners - 1) + corners = np.array([ + [0, 0], + [0, rows - 1], + [cols - 1, rows - 1], + [cols - 1, 0] + ]) + corners = tform.inverse(corners) minc = corners[:, 0].min() minr = corners[:, 1].min() maxc = corners[:, 0].max() @@ -270,7 +275,7 @@ def rotate(image, angle, resize=False, center=None, order=1, mode='constant', output_shape = np.ceil((out_rows, out_cols)) # fit output image in new shape - translation = ((cols - out_cols) / 2., (rows - out_rows) / 2.) + translation = (minc, minr) tform4 = SimilarityTransform(translation=translation) tform = tform4 + tform diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index a8dfcb87..95d1eb59 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -132,6 +132,20 @@ def test_rotate_center(): assert_almost_equal(x0, x) +def test_rotate_resize_center(): + x = np.zeros((10, 10), dtype=np.double) + x[0, 0] = 1 + + ref_x45 = np.zeros((14, 14), dtype=np.double) + ref_x45[6, 0] = 1 + ref_x45[7, 0] = 1 + + x45 = rotate(x, 45, resize=True, center=(3, 3), order=0) + # new dimension should be d = sqrt(2 * (10/2)^2) + assert x45.shape == (14, 14) + assert_equal(x45, ref_x45) + + def test_rescale(): # same scale factor x = np.zeros((5, 5), dtype=np.double)