Note that there are some explanatory texts on larger screens.

plurals
  1. POFast 2D rigid body transformations in numpy/scipy
    text
    copied!<p>I want to apply rigid body transformations to a large set of 2D image matrices. Ideally, I'd like to be able to just supply an affine transformation matrix specifying both the translation and rotation, apply this in one go, then do cubic spline interpolation on the output.</p> <p>Unfortunately it seems that <code>affine_transform</code> in <code>scipy.ndimage.interpolation</code> doesn't do translation. I know I could use a combination of <code>shift</code> and <code>rotate</code>, but this is kind of messy and in involves interpolating the output multiple times.</p> <p>I've also tried using the generic <code>geometric_transformation</code> like this:</p> <pre><code>import numpy as np from scipy.ndimage.interpolation import geometric_transformation # make the affine matrix def maketmat(xshift,yshift,rotation,dimin=(0,0)): # centre on the origin in2orig = np.identity(3) in2orig[:2,2] = -dimin[0]/2.,-dimin[1]/2. # rotate about the origin theta = np.deg2rad(rotation) rotmat = np.identity(3) rotmat[:2,:2] = [np.cos(theta),np.sin(theta)],[-np.sin(theta),np.cos(theta)] # translate to new position orig2out = np.identity(3) orig2out[:2,2] = xshift,yshift # the final affine matrix is just the product tmat = np.dot(orig2out,np.dot(rotmat,in2orig)) # function that maps output space to input space def out2in(outcoords,affinemat): outcoords = np.asarray(outcoords) outcoords = np.concatenate((outcoords,(1.,))) incoords = np.dot(affinemat,outcoords) incoords = tuple(incoords[0:2]) return incoords def rbtransform(source,xshift,yshift,rotation,outdims): # source --&gt; target forward = maketmat(xshift,yshift,rotation,source.shape) # target --&gt; source backward = np.linalg.inv(forward) # now we can use geometric_transform to do the interpolation etc. tformed = geometric_transform(source,out2in,output_shape=outdims,extra_arguments=(backward,)) return tformed </code></pre> <p>This works, but it's <i>horribly</i> slow, since it's essentially looping over pixel coordinates! What's a good way to do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload