mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-28 14:12:30 +08:00
Fix rotation with resizing and custom center
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user