-
Notifications
You must be signed in to change notification settings - Fork 8
/
interp_transform.py
executable file
·99 lines (81 loc) · 3.18 KB
/
interp_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# https://vtk.org/doc/nightly/html/classvtkTransformInterpolator.html
# https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1Transform.html
# https://vtk.org/doc/nightly/html/classvtkMatrix4x4.html
# https://www.paraview.org/Bug/view.php?id=10102#c37133
# https://www.cs.cmu.edu/~kiranb/animation/p245-shoemake.pdf
# Scaling the affine transform by performing interpolation between identity and
# the input transform
import vtk
import SimpleITK as sitk
import argparse
import numpy as np
def itk_to_homogeneous_matrix(itk_transform):
# Dump the matrix 4x4 matrix
input_itk_transform_parameters = itk_transform.GetParameters()
M = np.zeros((4, 4))
# Extract the 3x3 matrix and place into the larger 4x4
M[0:3, 0:3] = np.asarray(input_itk_transform_parameters[0:9]).reshape((3, 3))
# Append the translation
M[0:3, 3] = input_itk_transform_parameters[9:]
M[3, 3] = 1
return M
def homogenous_matrix_to_itk(M):
return sitk.AffineTransform(
M[0:3, 0:3].flatten(),
M[0:3, 3].flatten(),
(0, 0, 0),
)
def vtk_to_homogenous_matrix(vtk_transform):
M = np.eye(4)
vtk_matrix = vtk_transform.GetMatrix()
vtk_matrix.DeepCopy(M.ravel(), vtk_matrix)
return M
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"input_transform",
type=str,
help="""
Name of output scaled transform.
""",
)
parser.add_argument(
"scale_factor",
type=float,
help="""
Scaling factor.
""",
)
parser.add_argument(
"output_transform",
type=str,
help="""
Name of output scaled transform.
""",
)
opts = parser.parse_args()
# Input transform
input_itk_transform = sitk.ReadTransform(opts.input_transform)
output_itk_transform = sitk.AffineTransform(3)
input_itk_transform_matrix = itk_to_homogeneous_matrix(input_itk_transform)
# Create VTK input/output transform, and an identity for the interpolation
input_vtk_transform = vtk.vtkTransform()
identity_vtk_transform = vtk.vtkTransform()
output_vtk_transform = vtk.vtkTransform()
# Setup the VTK transform by reconstructing from the ITK matrix parameters
input_vtk_transform.SetMatrix(input_itk_transform_matrix.ravel())
# Create an interpolator
vtk_transform_interpolator = vtk.vtkTransformInterpolator()
# Build an interpolation stack, identity transform and the input transform
vtk_transform_interpolator.AddTransform(0, identity_vtk_transform)
vtk_transform_interpolator.AddTransform(1, input_vtk_transform)
# Generate a transform a fractional step between identity and the input transform
vtk_transform_interpolator.InterpolateTransform(
opts.scale_factor, output_vtk_transform
)
output_itk_transform = homogenous_matrix_to_itk(vtk_to_homogenous_matrix(output_vtk_transform))
output_itk_transform.SetFixedParameters(input_itk_transform.GetFixedParameters())
sitk.WriteTransform(output_itk_transform, opts.output_transform)