-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_tsd_syn.py
More file actions
184 lines (145 loc) · 5.96 KB
/
Copy pathtest_tsd_syn.py
File metadata and controls
184 lines (145 loc) · 5.96 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================
"""
import numpy as np
import itk
import math
fixedMesh = itk.meshread('../test/Baseline/fixedMesh.vtk', itk.D)
movingMesh = itk.meshread('../test/Baseline/movingMesh.vtk', itk.D)
fixedMesh.BuildCellLinks()
movingMesh.BuildCellLinks()
PixelType = itk.D
Dimension = 3
MeshType = itk.Mesh[itk.D, Dimension]
FixedImageType = itk.Image[PixelType, Dimension]
MovingImageType = itk.Image[PixelType, Dimension]
# For getting the Bounding Box
ElementIdentifierType = itk.UL
CoordType = itk.F
Dimension = 3
VecContType = itk.VectorContainer[
ElementIdentifierType, itk.Point[CoordType, Dimension]
]
bounding_box = itk.BoundingBox[ElementIdentifierType, Dimension, CoordType, VecContType].New()
bounding_box.SetPoints(movingMesh.GetPoints())
bounding_box.ComputeBoundingBox()
minBounds = np.array(bounding_box.GetMinimum())
maxBounds = np.array(bounding_box.GetMaximum())
imageDiagonal = 100
spacing = np.sqrt(bounding_box.GetDiagonalLength2()) / imageDiagonal
diff = maxBounds - minBounds
fixedImageSize = [0]*3
fixedImageSize[0] = math.ceil( 1.2 * diff[0] / spacing )
fixedImageSize[1] = math.ceil( 1.2 * diff[1] / spacing )
fixedImageSize[2] = math.ceil( 1.2 * diff[2] / spacing )
fixedImageOrigin = [0]*3
fixedImageOrigin[0] = minBounds[0] - 0.1 * diff[0]
fixedImageOrigin[1] = minBounds[1] - 0.1 * diff[1]
fixedImageOrigin[2] = minBounds[2] - 0.1 * diff[2]
fixedImageSpacing = np.ones(3)*spacing
fixedImageDirection = np.identity(3)
fixedImage = FixedImageType.New()
fixedImage.SetRegions(fixedImageSize)
fixedImage.SetOrigin( fixedImageOrigin )
fixedImage.SetDirection( fixedImageDirection )
fixedImage.SetSpacing( fixedImageSpacing )
fixedImage.Allocate()
VectorType = itk.Vector[itk.D, Dimension]
zeroVector = VectorType()
zeroVector.Fill(0)
DisplacementFieldType = itk.Image[VectorType, Dimension]
displacementField = DisplacementFieldType.New()
displacementField.CopyInformation(fixedImage)
displacementField.SetRegions(fixedImage.GetBufferedRegion())
displacementField.Allocate()
displacementField.FillBuffer(zeroVector)
inverseDisplacementField = DisplacementFieldType.New()
inverseDisplacementField.CopyInformation(fixedImage)
inverseDisplacementField.SetRegions(fixedImage.GetBufferedRegion())
inverseDisplacementField.Allocate()
inverseDisplacementField.FillBuffer(zeroVector)
TransformType = itk.DisplacementFieldTransform[itk.D, Dimension]
DisplacementFieldRegistrationType = itk.SyNImageRegistrationMethod[FixedImageType, MovingImageType,
TransformType, FixedImageType, MeshType]
registration = DisplacementFieldRegistrationType.New()
OutputTransformType = TransformType
outputTransform = OutputTransformType.New()
outputTransform.SetDisplacementField(displacementField)
outputTransform.SetInverseDisplacementField(inverseDisplacementField)
registration.SetInitialTransform(outputTransform)
registration.InPlaceOn()
# Affine Transform Object
AffineTransformType = itk.AffineTransform.D3
transform = AffineTransformType.New()
transform.SetIdentity()
transform.SetCenter(minBounds + (maxBounds - minBounds)/2)
print('Transform Created')
print(transform)
MetricType = itk.ThinShellDemonsMetricv4[type(fixedMesh)]
metric = MetricType.New()
metric.SetStretchWeight(1)
metric.SetBendWeight(5)
metric.SetGeometricFeatureWeight(10)
metric.UseConfidenceWeightingOn()
metric.UseMaximalDistanceConfidenceSigmaOn()
metric.UpdateFeatureMatchingAtEachIterationOff()
metric.SetMovingTransform(transform)
# Reversed due to using points instead of an image
# to keep semantics the same as in itkThinShellDemonsTest.cxx
# For the ThinShellDemonsMetricv4 the fixed mesh is regularized
metric.SetFixedPointSet(movingMesh)
metric.SetMovingPointSet(fixedMesh)
metric.SetVirtualDomainFromImage(fixedImage)
metric.Initialize()
print('TSD Metric Created')
varianceForUpdateField = spacing*spacing*25
varianceForTotalField = 0.0
registration.SetGaussianSmoothingVarianceForTheUpdateField(varianceForUpdateField)
registration.SetGaussianSmoothingVarianceForTheTotalField(varianceForTotalField)
registration.SetFixedPointSet(movingMesh)
registration.SetMovingPointSet(fixedMesh)
registration.SetMovingInitialTransform(transform)
registration.SetMetric(metric)
numberOfLevels = 3
registration.SetNumberOfLevels(numberOfLevels)
numberOfIterationsPerLevel = itk.Array[itk.UI]()
numberOfIterationsPerLevel.SetSize(numberOfLevels)
numberOfIterationsPerLevel[0] = 5
numberOfIterationsPerLevel[1] = 10
numberOfIterationsPerLevel[2] = 50
registration.SetNumberOfIterationsPerLevel(numberOfIterationsPerLevel)
shrinkFactorsPerLevel = itk.Array[itk.UI]()
shrinkFactorsPerLevel.SetSize(numberOfLevels)
shrinkFactorsPerLevel.Fill(1)
smoothingSigmasPerLevel = itk.Array[itk.UI]()
smoothingSigmasPerLevel.SetSize(numberOfLevels)
smoothingSigmasPerLevel.Fill(0)
registration.SetShrinkFactorsPerLevel(shrinkFactorsPerLevel)
registration.SetSmoothingSigmasPerLevel(smoothingSigmasPerLevel)
print('Initial Value of Metric ', metric.GetValue())
try:
registration.Update()
except:
print('Error Occured')
print('Final Value of Metric ', metric.GetValue())
finalTransform = registration.GetModifiableTransform()
numberOfPoints = movingMesh.GetNumberOfPoints()
for n in range(0, numberOfPoints):
movingMesh.SetPoint(n, finalTransform.TransformPoint(movingMesh.GetPoint(n)))
itk.meshwrite(movingMesh, "synMovingMesh.vtk")