21 from distutils.version
import StrictVersion
as VS
24 print(
"ITK 4.9.0 is required.")
27 parser = argparse.ArgumentParser(
28 description=
"Perform 2D Translation Registration With Mean Squares."
30 parser.add_argument(
"fixed_input_image")
31 parser.add_argument(
"moving_input_image")
32 parser.add_argument(
"output_image")
33 parser.add_argument(
"difference_image_after")
34 parser.add_argument(
"difference_image_before")
35 args = parser.parse_args()
37 PixelType = itk.ctype(
"float")
39 fixedImage = itk.imread(args.fixed_input_image, PixelType)
40 movingImage = itk.imread(args.moving_input_image, PixelType)
42 Dimension = fixedImage.GetImageDimension()
43 FixedImageType =
itk.Image[PixelType, Dimension]
44 MovingImageType =
itk.Image[PixelType, Dimension]
47 initialTransform = TransformType.New()
51 MinimumStepLength=0.001,
53 NumberOfIterations=200,
59 FixedImage=fixedImage,
60 MovingImage=movingImage,
63 InitialTransform=initialTransform,
66 movingInitialTransform = TransformType.New()
67 initialParameters = movingInitialTransform.GetParameters()
68 initialParameters[0] = 0
69 initialParameters[1] = 0
70 movingInitialTransform.SetParameters(initialParameters)
71 registration.SetMovingInitialTransform(movingInitialTransform)
73 identityTransform = TransformType.New()
74 identityTransform.SetIdentity()
75 registration.SetFixedInitialTransform(identityTransform)
77 registration.SetNumberOfLevels(1)
78 registration.SetSmoothingSigmasPerLevel([0])
79 registration.SetShrinkFactorsPerLevel([1])
83 transform = registration.GetTransform()
84 finalParameters = transform.GetParameters()
85 translationAlongX = finalParameters.GetElement(0)
86 translationAlongY = finalParameters.GetElement(1)
88 numberOfIterations = optimizer.GetCurrentIteration()
90 bestValue = optimizer.GetValue()
93 print(
" Translation X = " + str(translationAlongX))
94 print(
" Translation Y = " + str(translationAlongY))
95 print(
" Iterations = " + str(numberOfIterations))
96 print(
" Metric value = " + str(bestValue))
99 outputCompositeTransform = CompositeTransformType.New()
100 outputCompositeTransform.AddTransform(movingInitialTransform)
101 outputCompositeTransform.AddTransform(registration.GetModifiableTransform())
105 Transform=outputCompositeTransform,
106 UseReferenceImage=
True,
107 ReferenceImage=fixedImage,
109 resampler.SetDefaultPixelValue(100)
111 OutputPixelType = itk.ctype(
"unsigned char")
112 OutputImageType =
itk.Image[OutputPixelType, Dimension]
122 FixedImageType, OutputImageType
129 resampler.SetDefaultPixelValue(1)
130 writer.SetInput(intensityRescaler.GetOutput())
131 writer.SetFileName(args.difference_image_after)
134 resampler.SetTransform(identityTransform)
135 writer.SetFileName(args.difference_image_before)