강체 변형을 많은 2D 이미지 행렬에 적용하고 싶습니다. 이상적으로 말하면, 변환 및 회전을 모두 지정하는 affine 변환 행렬을 제공하고이를 한 번에 적용한 다음 출력에서 3 차 스플라인 보간을 수행하는 것이 좋습니다.numpy/scipy의 빠른 2D 강체 변환
불행히도 affine_transform
에 scipy.ndimage.interpolation
가 번역되어 있지 않은 것으로 보입니다. 나는 shift
과 rotate
의 조합을 사용할 수 있다는 것을 알고 있습니다. 그러나 이것은 지저분하고 출력을 여러 번 보간하는 것과 관련되어 있습니다. 이 작품
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 등급 매트릭스를 제공 할 것으로 예상되었고 2 열 이상을 수용하는 것을 거부했기 때문입니다. 나는'affine_transform'이 Nichola의 제안처럼 변환을위한 단일 매트릭스를 허용한다면 훨씬 더 직설적이라고 생각합니다. –
Affine은 단단하지 않습니다. –