2012-07-13 2 views
5

강체 변형을 많은 2D 이미지 행렬에 적용하고 싶습니다. 이상적으로 말하면, 변환 및 회전을 모두 지정하는 affine 변환 행렬을 제공하고이를 한 번에 적용한 다음 출력에서 ​​3 차 스플라인 보간을 수행하는 것이 좋습니다.numpy/scipy의 빠른 2D 강체 변환

불행히도 affine_transformscipy.ndimage.interpolation가 번역되어 있지 않은 것으로 보입니다. 나는 shiftrotate의 조합을 사용할 수 있다는 것을 알고 있습니다. 그러나 이것은 지저분하고 출력을 여러 번 보간하는 것과 관련되어 있습니다. 이 작품

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 

하지만 기본적으로 픽셀 좌표를 통해 루핑 이후로는, 끔찍 느린 :

나는이 같은 일반 geometric_transformation를 사용하여 시도했습니다! 이 작업을 수행하는 좋은 방법은 무엇입니까?

답변

3

은 내가 offset 매개 변수가있다 --- affine_transform않습니다 번역을 할 생각합니다.

+0

하, 당신은 아주 좋은 지적을했습니다! 저를 던진 것은 제가 3 등급 매트릭스를 제공 할 것으로 예상되었고 2 열 이상을 수용하는 것을 거부했기 때문입니다. 나는'affine_transform'이 Nichola의 제안처럼 변환을위한 단일 매트릭스를 허용한다면 훨씬 더 직설적이라고 생각합니다. –

+0

Affine은 단단하지 않습니다. –

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'는 쌍 선형 보간만을 지원하는 것 같지만 보통의 '호모 그래피'는 바이 큐빅이며 내 목적에 충분히 빠르다는 것입니다. –