Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2D Rotation convenience constructor, Image.rotate_ccw_about_centre #511

Merged
merged 3 commits into from Nov 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion menpo/image/base.py
Expand Up @@ -9,7 +9,7 @@
from menpo.base import Vectorizable
from menpo.landmark import LandmarkableViewable
from menpo.transform import (Translation, NonUniformScale,
AlignmentUniformScale, Affine)
AlignmentUniformScale, Affine, Rotation)
from menpo.visualize.base import ImageViewer
from .interpolation import scipy_interpolation, cython_interpolation
from .extract_patches import extract_patches_cython
Expand Down Expand Up @@ -1238,6 +1238,30 @@ def resize(self, shape, order=1):
# floating point inaccuracy.
return self.rescale(scales, round='round', order=order)

def rotate_ccw_about_centre(self, theta, degrees=True, cval=0):
r"""
Return a rotation of this image clockwise about it's centre.

Parameters
----------
theta : ``float``
The angle of rotation about the origin
degrees : ``bool``, optional
If true theta is interpreted as a degree. If False, theta is
interpreted as radians.
cval : ``float``, optional
The value to be set outside the rotated image boundaries
"""
if self.n_dims != 2:
raise ValueError('Image rotation is presently only supported on '
'2D images')
# create a translation that moves the centre of the image to the origin
t = Translation(self.centre)
r = Rotation.from_2d_ccw_angle(theta, degrees=degrees)
r_about_centre = t.pseudoinverse().compose_before(r).compose_before(t)
return self.warp_to_shape(self.shape, r_about_centre.pseudoinverse(),
warp_landmarks=True, cval=cval)

def pyramid(self, n_levels=3, downscale=2):
image = self
yield image
Expand Down
24 changes: 24 additions & 0 deletions menpo/transform/homogeneous/rotation.py
Expand Up @@ -51,6 +51,30 @@ def __init__(self, rotation_matrix, skip_checks=False):
Similarity.__init__(self, h_matrix, copy=False, skip_checks=True)
self.set_rotation_matrix(rotation_matrix, skip_checks=skip_checks)

@classmethod
def from_2d_ccw_angle(cls, theta, degrees=True):
r"""
Convenience constructor for 2D CCW rotations about the origin

Parameters
----------
theta : float
The angle of rotation about the origin
degrees : bool, optional
If true theta is interpreted as a degree. If False, theta is
interpreted as radians.

Returns
-------
rotation : :map:`Rotation`
A 2D rotation transform.
"""
if degrees:
# convert to radians
theta = theta * np.pi / 180.0
return Rotation(np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]]))

@classmethod
def identity(cls, n_dims):
return Rotation(np.eye(n_dims))
Expand Down