Fast 2D solid to numpy / scipy conversion

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

# 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 --> target
    forward = maketmat(xshift,yshift,rotation,source.shape)

    # target --> 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

This works, but it is terribly slow since it essentially goes over the pixel coordinates! What a good way to do this?

+5
2

, affine_transform , offset.

+3

scikit? , . , 3x3. skimage.transform.fast_homography.

import numpy as np
import scipy
import skimage.transform
im = scipy.misc.lena()
H = np.asarray([[1, 0, 10], [0, 1, 20], [0, 0, 1]])
skimage.transform.fast_homography(im, H)

30 Core 2 Duo.

: http://en.wikipedia.org/wiki/Homography

+3

All Articles