7 ImageType =
itk.Image[PixelType, Dimension]
33 metric = MetricType.New()
34 transform = TransformType.New()
35 optimizer = OptimizerType.New()
36 interpolator = InterpolatorType.New()
37 registration = RegistrationType.New()
40 registration.SetMetric(metric)
41 registration.SetOptimizer(optimizer)
42 registration.SetTransform(transform)
43 registration.SetInterpolator(interpolator)
46 fixedImage = ImageType.New()
47 movingImage = ImageType.New()
49 CreateSphereImage(fixedImage)
50 CreateEllipseImage(movingImage)
53 itk.imwrite(fixedImage,
"fixed.png")
54 itk.imwrite(movingImage,
"moving.png")
57 registration.SetFixedImage(fixedImage)
58 registration.SetMovingImage(movingImage)
60 registration.SetFixedImageRegion(fixedImage.GetLargestPossibleRegion())
64 transform.GetNumberOfParameters()
67 initialParameters[0] = 0.0
68 initialParameters[1] = 0.0
70 registration.SetInitialTransformParameters(initialParameters)
72 optimizer.SetMaximumStepLength(4.00)
73 optimizer.SetMinimumStepLength(0.01)
76 optimizer.SetNumberOfIterations(200)
89 print(
"ExceptionObject caught !")
96 finalParameters = registration.GetLastTransformParameters()
102 TranslationAlongX = finalParameters[0]
103 TranslationAlongY = finalParameters[1]
111 numberOfIterations = optimizer.GetCurrentIteration()
116 bestValue = optimizer.GetValue()
120 print(
" Translation X = {}".format(TranslationAlongX))
121 print(
" Translation Y = {}".format(TranslationAlongY))
122 print(
" Iterations = {}".format(numberOfIterations))
123 print(
" Metric value = {}".format(bestValue))
137 resampler = ResampleFilterType.New()
138 resampler.SetInput(movingImage)
148 resampler.SetTransform(registration.GetOutput().Get())
156 resampler.SetSize(itk.size(fixedImage))
157 resampler.SetOutputOrigin(fixedImage.GetOrigin())
158 resampler.SetOutputSpacing(fixedImage.GetSpacing())
159 resampler.SetOutputDirection(fixedImage.GetDirection())
160 resampler.SetDefaultPixelValue(100)
168 caster = CastFilterType.New()
169 caster.SetInput(resampler.GetOutput())
170 itk.imwrite(caster.GetOutput(),
"outputPython.png")
178 difference = DifferenceFilterType.New()
179 difference.SetInput1(fixedImage)
180 difference.SetInput2(resampler.GetOutput())
185 def CreateEllipseImage(image):
192 imageFilter = SpatialObjectToImageFilterType.New()
198 imageFilter.SetSize(size)
201 imageFilter.SetSpacing(spacing)
203 ellipse = EllipseType.New()
205 radiusArray.SetSize(Dimension)
208 ellipse.SetRadiusInObjectSpace(radiusArray)
211 transform = TransformType.New()
212 transform.SetIdentity()
217 transform.Translate(translation,
False)
219 ellipse.SetObjectToParentTransform(transform)
221 imageFilter.SetInput(ellipse)
223 ellipse.SetDefaultInsideValue(255)
224 ellipse.SetDefaultOutsideValue(0)
225 imageFilter.SetUseObjectValue(
True)
226 imageFilter.SetOutsideValue(0)
229 image.Graft(imageFilter.GetOutput())
232 def CreateSphereImage(image):
239 imageFilter = SpatialObjectToImageFilterType.New()
244 imageFilter.SetSize(size)
247 imageFilter.SetSpacing(spacing)
249 ellipse = EllipseType.New()
251 radiusArray.SetSize(Dimension)
254 ellipse.SetRadiusInObjectSpace(radiusArray)
257 transform = TransformType.New()
258 transform.SetIdentity()
260 translation =
itk.Vector[PixelType, Dimension]()
263 transform.Translate(translation,
False)
265 ellipse.SetObjectToParentTransform(transform)
267 imageFilter.SetInput(ellipse)
269 ellipse.SetDefaultInsideValue(255)
270 ellipse.SetDefaultOutsideValue(0)
271 imageFilter.SetUseObjectValue(
True)
272 imageFilter.SetOutsideValue(0)
275 image.Graft(imageFilter.GetOutput())
278 if __name__ ==
"__main__":