ntg
ntg

Reputation: 14145

python: skimage.transform.AffineTransform rotation center

I am trying to use skimage in python to rotate an image, in opencv, it seems I can do:

cv.GetRotationMatrix2D(center, angle, scale, mapMatrix) 

where center is the Center of the rotation in the source image.

in skimage the respective transformation seems to be skimage.transform.AffineTransform:

skimage.transform.AffineTransform(matrix=None, scale=None, rotation=None, shear=None, translation=None)

But I cannot how to define the center of the rotation.... Is there away to define the rotation center in this (or maybe there is another skimage method?)

I have checked the web and the manuals, finding nothing so far...

Upvotes: 4

Views: 10077

Answers (2)

roro
roro

Reputation: 41

The image center in Stefan's answer seems not correct, here comes a corrected version of his code

from skimage import transform
import numpy as np
import matplotlib.pyplot as plt

image = np.zeros([21, 21])
image[10,:] = 1
image[10,10] = 5
image[7, 10] = 1

shift_y, shift_x = (np.array(image.shape)-1) / 2.
tf_rotate = transform.SimilarityTransform(rotation=np.deg2rad(60))
tf_shift = transform.SimilarityTransform(translation=[-shift_x, -shift_y])
tf_shift_inv = transform.SimilarityTransform(translation=[shift_x, shift_y])
image_rotated = transform.warp(image, (tf_shift + (tf_rotate + tf_shift_inv)).inverse, order = 3)


plt.subplot(121)
plt.imshow(image)
plt.subplot(122)
plt.imshow(image_rotated)
plt.show()

print "original image maximum at: ", np.unravel_index(np.argmax(image), image.shape)
print "rotated image maximum at : ", np.unravel_index(np.argmax(image_rotated), image_rotated.shape)

original image maximum at: (10, 10) rotated image maximum at : (10, 10)

Upvotes: 4

Stefan van der Walt
Stefan van der Walt

Reputation: 7253

This is currently possible by combining the following transformations:

  1. Shift the image so that the centre is around the origin
  2. Rotate with N degrees
  3. Shift the image back

However, a single parameter would make this much easier! Would you please file an issue on GitHub so that we can implement this?

In the meantime, the code:

from skimage import data
from skimage import transform
import numpy as np
import matplotlib.pyplot as plt

image = data.chelsea()

shift_y, shift_x = np.array(image.shape[:2]) / 2.
tf_rotate = transform.SimilarityTransform(rotation=np.deg2rad(30))
tf_shift = transform.SimilarityTransform(translation=[-shift_x, -shift_y])
tf_shift_inv = transform.SimilarityTransform(translation=[shift_x, shift_y])

image_rotated = transform.warp(image, (tf_shift + (tf_rotate + tf_shift_inv)).inverse)

plt.imshow(image_rotated)
plt.show()

Upvotes: 6

Related Questions