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

Transform about centre #738

Merged
merged 4 commits into from Sep 20, 2016

Conversation

Projects
None yet
3 participants
@nontas
Member

nontas commented Sep 7, 2016

This PR adds a general function for transforming about an image's centre. In general, this probably only makes sense for Affine transforms - though any transform is supported via the transform chain.

It also adds a new 2D shear transform. The shear transform has the following formulation:

array([[1,        tan(phi), 0], 
       [tan(psi), 1,        0],
       [0,        0,        1]])

This can be useful for data perturbation (distortions).

Example

The following code:

%matplotlib inline
import matplotlib.pyplot as plt
import menpo.io as mio
from menpo.transform import Affine, Rotation, shear_about_centre

im = mio.import_builtin_asset.lenna_png()
shear_tr = Affine.init_from_2d_shear(25, 10)
shear_tr = Rotation.init_from_2d_ccw_angle(45)

plt.subplot(141)
im.view_landmarks(render_axes=True)
plt.title('Original')

plt.subplot(142)
im.transform_about_centre(shear_tr).view_landmarks(render_axes=True)
plt.title('Sheared - No Retain Shape')

plt.subplot(143)
im.transform_about_centre(shear_tr, retain_shape=True).view_landmarks(render_axes=True)
plt.title('Sheared - Retain Shape')

plt.subplot(144)
bbox = im.landmarks['LJSON'].lms.bounding_box()
bbox.view(line_width=2, line_colour='b', render_markers=False)
shear_about_centre(bbox, 25, 10).apply(bbox).view(line_width=2, line_colour='r', 
                                                  render_markers=False, 
                                                  figure_size=(17, 17))
plt.title('Example')

generates the following result:
download

@jabooth jabooth added the in progress label Sep 7, 2016

@patricksnape

This comment has been minimized.

Contributor

patricksnape commented Sep 8, 2016

OK - what if we add something like affine_around_centre that applies some Affine transform around the centre and we abstract out the whole -translation, affine, translation step? Shearing seems a tad specific to me.

@@ -2257,6 +2258,101 @@ def rotate_ccw_about_centre(self, theta, degrees=True, retain_shape=False,
shape, trans.pseudoinverse(), order=order, warp_landmarks=True,
cval=cval, return_transform=return_transform)
def shear_about_centre(self, phi, psi, degrees=True, retain_shape=False,

This comment has been minimized.

@patricksnape

patricksnape Sep 11, 2016

Contributor

I think this is better (more general) as transform_about_centre where we just make sure that we apply the translation and then the translation back?

raise ValueError('Shearing can only be applied on 2D objects')
# Create shearing and translation transforms
a = create_2d_shear_transform(phi, psi, degrees=degrees)
t = Translation(-obj.centre(), skip_checks=True)

This comment has been minimized.

@patricksnape

patricksnape Sep 11, 2016

Contributor

I think we should abstract this logic out.

@patricksnape patricksnape changed the title from Shear (skew) transform to Transform about centre Sep 14, 2016

Refactor to transform_about_centre
Shearing remains, though it is not it's own function

@patricksnape patricksnape force-pushed the nontas:skew branch from a8a8a92 to f810c91 Sep 14, 2016

@patricksnape

This comment has been minimized.

Contributor

patricksnape commented Sep 14, 2016

I have refactored the PR for a more general transform about centre - maintaining the shear logic.

@nontas

This comment has been minimized.

Member

nontas commented Sep 19, 2016

Good change @patricksnape ! I tried this and it works. I simply added two usage examples in the Image.transform_about_centre() doc.
+1

@patricksnape patricksnape merged commit c0952cb into menpo:master Sep 20, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@patricksnape patricksnape deleted the nontas:skew branch Sep 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment