ali*_*i_m 5 python numpy image-processing scipy affinetransform
我想将刚体变换应用于大量的二维图像矩阵.理想情况下,我希望能够提供一个指定平移和旋转的仿射变换矩阵,一次应用,然后在输出上进行三次样条插值.
不幸的是,似乎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
Run Code Online (Sandbox Code Playgroud)
这可行,但它非常慢,因为它基本上是在像素坐标上循环!这样做的好方法是什么?
| 归档时间: |
|
| 查看次数: |
3657 次 |
| 最近记录: |