我需要使用相同的变换矩阵变换(现在旋转)itk图像和vtk polydata,但我遇到了麻烦。
所有代码和测试数据都在这里:https://github.com/jmerkow/vtk_itk_rotate
以下是相关部分:
import SimpleITK as sitk
import vtk
import numpy as np
def rotate_img(img, rotation_center=None, theta_x=0,theta_y=0, theta_z=0, translation=(0,0,0), interp=sitk.sitkLinear, pixel_type=None, default_value=None):
if not rotation_center:
rotation_center = np.array(img.GetOrigin())+np.array(img.GetSpacing())*np.array(img.GetSize())/2
if default_value is None:
default_value = img.GetPixel(0,0,0)
pixel_type = img.GetPixelIDValue()
rigid_euler = sitk.Euler3DTransform(rotation_center, theta_x, theta_y, theta_z, translation)
return sitk.Resample(img, img, rigid_euler, interp, default_value, pixel_type)
def rotate_polydata(pd, rotation_center, theta_x=0,theta_y=0, theta_z=0, translation=(0,0,0)):
rigid_euler = sitk.Euler3DTransform(rotation_center, -theta_x, -theta_y, -theta_z, translation)
matrix = np.zeros([4,4])
old_matrix=np.array(rigid_euler.GetMatrix()).reshape(3,3)
matrix[:3,:3] = old_matrix
matrix[-1,-1] = 1
# to rotate about a center we first need to translate
transform_t = vtk.vtkTransform()
transform_t.Translate(-rotation_center)
transformer_t = vtk.vtkTransformPolyDataFilter()
transformer_t.SetTransform(transform_t)
transformer_t.SetInputData(pd)
transformer_t.Update()
transform = vtk.vtkTransform()
transform.SetMatrix(matrix.ravel())
transformer = vtk.vtkTransformPolyDataFilter()
transformer.SetTransform(transform)
transformer.SetInputConnection(transformer_t.GetOutputPort())
transformer.Update()
# translate back
transform_t2 = vtk.vtkTransform()
transform_t2.Translate(rotation_center)
transformer_t2 = vtk.vtkTransformPolyDataFilter()
transformer_t2.SetTransform(transform_t2)
transformer_t2.SetInputConnection(transformer.GetOutputPort())
transformer_t2.Update()
return transformer_t2.GetOutputDataObject(0)
datafn = 'test.mha'
polydata_file = 'test.vtp'
reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName(polydata_file)
reader.Update()
pd = reader.GetOutput()
img = sitk.ReadImage(datafn)
seg = pd_to_itk_image(pd, img)
rotation_center = np.array(img.GetOrigin())+np.array(img.GetSpacing())*np.array(img.GetSize())/2
thetas = [0, 50]
thetas = [0, 50]
for theta_x in thetas:
for theta_y in thetas:
for theta_z in thetas:
theta_xr = theta_x/180.*np.pi
theta_yr = theta_y/180.*np.pi
theta_zr = theta_z/180.*np.pi
img_rot=rotate_img(img, theta_z=theta_zr, theta_y=theta_yr, theta_x=theta_xr)
seg_rot=rotate_img(seg, theta_z=theta_zr, theta_y=theta_yr, theta_x=theta_xr, interp=sitk.sitkNearestNeighbor, default_value=0)
pd_rot = rotate_polydata(pd, rotation_center, theta_z=theta_zr, theta_y=theta_yr, theta_x=theta_xr)
seg_pd_rot = pd_to_itk_image(pd_rot, img_rot)
mse = ((sitk.GetArrayFromImage(seg_pd_rot)-sitk.GetArrayFromImage(seg_rot))**2.).mean()
print theta_x, theta_y, theta_z, mse
#this outputs for this particular volume:
#0 0 0 mse: 0.0
#0 0 50 mse: 50.133369863 visually about the same
#0 50 0 mse: 25.2197787166 visually about the same
#0 50 50 mse: 863.588476181 visually totally different
#50 0 0 mse: 20.4021692276 visually about the same
#50 0 50 mse: 546.699844301 visually totally different
#50 50 0 mse: 662.337975204 visually totally different
#50 50 50 mse: 339.220945537 visually totally different
此代码旋转从polydata生成的二进制卷,并对polydata执行相同的旋转操作,然后从中生成二进制卷。我希望这两个结果(大致)相同,但是,如果我围绕多个轴旋转,我得到的是两个完全不同的旋转。 这对我来说很困惑,因为我从一个变换矩阵中取出变换矩阵并将其直接应用到另一个变换矩阵中。
如何设置这些变换,使两个操作执行相同的转换?为什么我们最终得到不同的结果?
答案 0 :(得分:0)
欧拉角的顺序对最终结果 [Wikipedia]很重要。此外,矩阵预乘也有逆序到后乘 [vtkTransform]。尝试拨打vtkTransform::PostMultiply()
或改变rotate_polydata
功能中的转换顺序。这很容易尝试。
如果这不能解决问题,请查看ITK如何在ComputeOffset,TransformPoint和ComputeMatrix中应用转换,以及VTK如何在vtkLinearTransformPoint中执行转换。这应该解释行为的差异,并提供如何实现相同变换的线索。
答案 1 :(得分:0)
感谢Dženan指出我正确的方向。
在这种情况下,答案很简单。 VTK和ITK使用不同的行/列主要格式进行矩阵乘法。所以答案就是在将矩阵放入vtkTransform之前转置矩阵。
这是新功能。
def rotate_polydata(pd, rotation_center, theta_x=0,theta_y=0, theta_z=0):
#I don't want to deal with translation
translation=(0,0,0)
rigid_euler = sitk.Euler3DTransform(rotation_center, theta_x, theta_y, theta_z, translation)
matrix = np.zeros([4,4])
old_matrix=np.array(rigid_euler.GetMatrix()).reshape(3,3)
matrix[:3,:3] = old_matrix
matrix[-1,-1] = 1
#ITK and VTK use different orders.
matrix= matrix.T
# to rotate about a center we first need to translate
transform_t = vtk.vtkTransform()
transform_t.Translate(-rotation_center)
transformer_t = vtk.vtkTransformPolyDataFilter()
transformer_t.SetTransform(transform_t)
transformer_t.SetInputData(pd)
transformer_t.Update()
transform = vtk.vtkTransform()
transform.SetMatrix(matrix.ravel())
transform.Translate(translation)
transform.PostMultiply()
transformer = vtk.vtkTransformPolyDataFilter()
transformer.SetTransform(transform)
transformer.SetInputConnection(transformer_t.GetOutputPort())
transformer.Update()
# translate back
transform_t2 = vtk.vtkTransform()
transform_t2.Translate(rotation_center)
transformer_t2 = vtk.vtkTransformPolyDataFilter()
transformer_t2.SetTransform(transform_t2)
transformer_t2.SetInputConnection(transformer.GetOutputPort())
transformer_t2.Update()
return transformer_t2.GetOutputDataObject(0)