[SciPy-User] ndimage.rotate around which anchor point? center?

Samuel scipy at samueljohn.de
Sun Feb 13 14:37:35 EST 2011


Hey again

[sorry for double posting, i mixed scipy-user and scipy-dev ... shame on me]

On 13.02.2011, at 15:43, I wrote:
> What is the point around which is rotated? I guess it is something
> like the center (h/2,w/2) of the image. There is nothing in the
> docstring about this.
> However, I need to define the point, around which is rotated (in my
> case the point is ndimage.center_of_mass(myImage)[0:2] for an RGBA
> image).
> 
> I really like the fact, that ndimage.rotate and ndimage.zoom return
> the array such that nothing is cropped.

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*.

What do you think?

Samuel




More information about the SciPy-User mailing list