I want to apply solid state transformations to a large set of 2D image matrices. Ideally, I would just like to provide an affine transformation matrix that defines both translation and rotation, apply this at a time, then do cubic spline interpolation at the output.
Unfortunately, it seems that affine_transformin scipy.ndimage.interpolationdoes not translate. I know that I could use a combination of shiftand rotate, but this is pretty messy and involves interpolating the output several times.
I also tried using generic geometric_transformationlike this:
import numpy as np
from scipy.ndimage.interpolation import geometric_transformation
def maketmat(xshift,yshift,rotation,dimin=(0,0)):
in2orig = np.identity(3)
in2orig[:2,2] = -dimin[0]/2.,-dimin[1]/2.
theta = np.deg2rad(rotation)
rotmat = np.identity(3)
rotmat[:2,:2] = [np.cos(theta),np.sin(theta)],[-np.sin(theta),np.cos(theta)]
orig2out = np.identity(3)
orig2out[:2,2] = xshift,yshift
tmat = np.dot(orig2out,np.dot(rotmat,in2orig))
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):
forward = maketmat(xshift,yshift,rotation,source.shape)
backward = np.linalg.inv(forward)
tformed = geometric_transform(source,out2in,output_shape=outdims,extra_arguments=(backward,))
return tformed
This works, but it is terribly slow since it essentially goes over the pixel coordinates! What a good way to do this?