剛体変換を大量の 2D 画像行列に適用したいと考えています。理想的には、平行移動と回転の両方を指定するアフィン変換行列を提供し、これを一度に適用してから、出力に対して 3 次スプライン補間を実行できるようにしたいと考えています。
残念ながら、affine_transform
inscipy.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
これは機能しますが、基本的にピクセル座標をループしているため、非常に遅くなります! これを行う良い方法は何ですか?