Merge pull request #1168 from ahojnnes/rot-center

Add option to rotate around specific center. Closes gh-1166.
This commit is contained in:
Stefan van der Walt
2014-09-22 16:03:37 +02:00
2 changed files with 22 additions and 4 deletions
+11 -4
View File
@@ -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
+11
View File
@@ -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)