2012-07-13 77 views
5

我想将刚体转换应用于一大组二维图像矩阵。理想情况下,我希望能够提供指定平移和旋转的仿射变换矩阵,一次应用这个矩阵,然后对输出执行三次样条插值。numpy/scipy中的快速2D刚体转换

不幸的是,affine_transformscipy.ndimage.interpolation似乎不会做翻译。我知道我可以使用shiftrotate的组合,但这是一种混乱,并且涉及多次内插输出。

我还使用通用geometric_transformation这样的尝试:

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 

这工作,但它的可怕缓慢,因为它本质上循环像素坐标!什么是这样做的好方法?

回答

3

我觉得affine_transform确实做翻译---还有的offset参数。

+0

哈,你说得很好!扔给我的是我期望提供一个3级矩阵,并拒绝接受两行以上的数据。我认为如果'affine_transform'接受一个转换的矩阵,就像Nichola的建议一样,这会更直接。 – 2012-07-13 20:33:09

+0

Affine不是死板的 – 2018-02-01 16:03:19

3

你能使用scikit image吗? 如果是这种情况,您可以尝试应用单应性。通过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) 

我的旧Core 2 Duo的转换花了大约30毫秒。

关于单应:http://en.wikipedia.org/wiki/Homography

+0

不错,几乎正是我所期待的。唯一的缺点是'fast_homography'似乎只支持双线性插值,但简单的'单应性'确实是双立体的,并且足够快达到我的目的。 – 2012-07-13 20:36:46