我想将刚体转换应用于一大组二维图像矩阵。理想情况下,我希望能够提供指定平移和旋转的仿射变换矩阵,一次应用这个矩阵,然后对输出执行三次样条插值。numpy/scipy中的快速2D刚体转换
不幸的是,affine_transform
在scipy.ndimage.interpolation
似乎不会做翻译。我知道我可以使用shift
和rotate
的组合,但这是一种混乱,并且涉及多次内插输出。
我还使用通用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'接受一个转换的矩阵,就像Nichola的建议一样,这会更直接。 – 2012-07-13 20:33:09
Affine不是死板的 – 2018-02-01 16:03:19