[SciPy-User] ndimage.rotate around which anchor point? center?
Stéfan van der Walt
stefan at sun.ac.za
Mon Feb 14 08:49:16 EST 2011
Hi Samual
On Sun, Feb 13, 2011 at 9:37 PM, Samuel <scipy at samueljohn.de> wrote:
> My current solution is quite complicated. It rotates an *image* around *center*:
>
> import scipy as S
> from scipy import ndimage
>
> center = S.array(ndimage.center_of_mass( image[:,:,-1] ))
> orig_shape = S.array(image.shape[0:2],dtype=S.float32)
> im_center = orig_shape/2.0 # only h,w
> bigger_shape = orig_shape + S.absolute( center - im_center )
> bigger_shape = S.array( [ bigger_shape.max(), bigger_shape.max()] ) #square shaped
> bigger = S.zeros( (bigger_shape[0], bigger_shape[1], image.shape[2]) ,dtype=S.float32)
> # Now I copy image into top left of bigger
> bigger[0:orig_shape[0], 0:orig_shape[1],:] = image[:]
> bigger_center = bigger_shape/2.0
> image = ndimage.shift( input=bigger,
> shift=S.concatenate( (bigger_center-center, [0]) ),
> order=1 )
>
> The idea is to make enough room, then shift the bigger array such that *center* will be
> in the center of bigger. If you then rotate via ndimage.rotate(image, order=1) the effect
> is as if you would rotate around the point *center*.
The standard solution is pretty much like you describe. Say we have
transformation matrices T (translate centre to origin) and R (perform
rotation), you want to apply them as follows:
inv(T) R T
In other words, translate centre to origin, rotate, translate back.
The matrices have the forms
R =
[cos(theta) -sin(theta) 0]
[sin(theta) cos(theta) 0]
[0 0 1]
T =
[0 0 tx]
[0 0 ty]
[0 0 1]
After multiplying the matrices, you probably get the solution you
propose earlier on.
You can apply these transformation matrices using the "homography"
function from scikits.image.transform:
https://github.com/stefanv/scikits.image/blob/master/scikits/image/transform/project.py#L19
Regards
Stéfan
More information about the SciPy-User
mailing list