5

剛体変換を大量の 2D 画像行列に適用したいと考えています。理想的には、平行移動と回転の両方を指定するアフィン変換行列を提供し、これを一度に適用してから、出力に対して 3 次スプライン補間を実行できるようにしたいと考えています。

残念ながら、affine_transforminscipy.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

これは機能しますが、基本的にピクセル座標をループしているため、非常に遅くなります! これを行う良い方法は何ですか?

4

2 に答える 2

3

翻訳を行うと思いますaffine_transform ---パラメータがありoffsetます。

于 2012-07-13T10:56:09.920 に答える
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)

古い Core 2 Duo では、変換に約 30 ミリ秒かかりました。

ホモグラフィーについて : http://en.wikipedia.org/wiki/Homography

于 2012-07-13T08:11:29.450 に答える