Created
August 7, 2012 16:30
-
-
Save Cadair/3287042 to your computer and use it in GitHub Desktop.
Propose rotate function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| :Created on: Tue Aug 7 16:44:50 2012 | |
| :author: Stuart Mumford | |
| This is a wrapper for a general rotate method, it will rotate and rescale the | |
| image around an arbitary point and then if requested shift the point to the centre. | |
| """ | |
| import numpy as np | |
| import scipy.ndimage | |
| def _af_args(angle, centre, shift, scale): | |
| """ Makes args for affine_transform scipy lib fun from values for | |
| input array, since fn. takes values in output array as args. """ | |
| # Re-usable bit - set centre, shift, ang | |
| c = np.cos(angle) | |
| s = np.sin(angle) | |
| mati = np.array([[c, s],[-s, c]])/scale # res->orig | |
| centre = np.array([centre]).transpose() # the centre of rotn | |
| shift = np.array([shift]).transpose() # the shift | |
| kpos = centre - np.dot(mati, (centre + shift)) | |
| # kpos and mati are the two transform constants, kpos is a 2x1 array | |
| return (mati, (kpos[0,0], kpos[1,0])) | |
| def rotate(image, angle, scale=1.0, centroid=None, recentre=True, order=3, | |
| missing=0.0, method='scipy'): | |
| """ Rotate, rescale and shift and image | |
| Arguments | |
| --------- | |
| image ndarray The input data | |
| angle float The angle to rotate the image by. (radians) | |
| Keyword Arguments | |
| ----------------- | |
| scale float A scale factor for the image, default is no scaling | |
| centroid tuple The point in the image to rotate around (Axis of rotation), | |
| default is the centre of the array | |
| recentre bool, or list Move the centroid (axis of rotation) to | |
| the centre of the array or recentre coords | |
| order int The order of the spline interpolation to be used. | |
| missing float The numerical value of any missing data | |
| method string Selects the rotation method from: | |
| 'scipy', 'C' | |
| """ | |
| i_rows,i_cols = image.shape | |
| centre = ((i_rows - 1)/2.0, (i_cols - 1)/2.0) | |
| if not(centroid): | |
| centroid = centre | |
| if recentre: | |
| if centroid == centre: | |
| shift = (0., 0.) | |
| if type(recentre) in [list, tuple, np.ndarray]: | |
| shift = np.array(recentre) - np.array(centre) | |
| else: | |
| shift = np.array(centroid) - np.array(centre) | |
| else: | |
| shift = (0.,0.) | |
| if method == 'scipy': | |
| rsmat, offs = _af_args(angle, centroid, shift, scale) | |
| image.data = scipy.ndimage.interpolation.affine_transform( | |
| image, rsmat, offset=offs, order=order, mode='constant', cval=missing) | |
| elif method == 'C': | |
| raise NotImplementedError("Nope, not yet") | |
| else: | |
| if type(method) != type(''): | |
| raise TypeError("method keyword argument should be a string") | |
| raise ValueError("%s is not a valid argument for method"%method) | |
| return image | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two comments:
(1) I would suggest making radians optional, and make degrees the default. Most angles in FITS headers are quoted in degrees (I think), and I imagine this ability would be applied to FITS images quite directly.
(2) In which direction does the rotation operate - clockwise, or anti-clockwise?
Thanks!