24 from distutils.version
import StrictVersion
as VS
26 print(
"ITK 4.9.0 is required.")
29 if len(sys.argv) != 6:
30 print(
"Usage: " + sys.argv[0] +
" <fixedImageFile> <movingImageFile> "
31 "<outputImagefile> <differenceImageAfter> <differenceImageBefore>")
34 fixedImage = sys.argv[1]
35 movingImage = sys.argv[2]
36 outputImage = sys.argv[3]
37 differenceImageAfter = sys.argv[4]
38 differenceImageBefore = sys.argv[5]
43 FixedImageType =
itk.Image[PixelType, Dimension]
44 MovingImageType =
itk.Image[PixelType, Dimension]
50 FixedImageType, MovingImageType]
53 FixedImageType, MovingImageType]
55 metric = MetricType.New()
56 optimizer = OptimizerType.New()
57 transform = TransformType.New()
58 registration = RegistrationType.New()
60 registration.SetMetric(metric)
61 registration.SetOptimizer(optimizer)
62 registration.SetInitialTransform(transform)
65 FixedImageType, itk.D]
67 MovingImageType, itk.D]
69 fixedInterpolator = FixedLinearInterpolatorType.New()
70 movingInterpolator = MovingLinearInterpolatorType.New()
72 metric.SetFixedInterpolator(fixedInterpolator)
73 metric.SetMovingInterpolator(movingInterpolator)
77 fixedImageReader = FixedImageReaderType.New()
78 movingImageReader = MovingImageReaderType.New()
80 fixedImageReader.SetFileName(fixedImage)
81 movingImageReader.SetFileName(movingImage)
83 registration.SetFixedImage(fixedImageReader.GetOutput())
84 registration.SetMovingImage(movingImageReader.GetOutput())
86 movingInitialTransform = TransformType.New()
87 initialParameters = movingInitialTransform.GetParameters()
88 initialParameters[0] = 0
89 initialParameters[1] = 0
90 movingInitialTransform.SetParameters(initialParameters)
92 registration.SetMovingInitialTransform(movingInitialTransform)
94 identityTransform = TransformType.New()
95 identityTransform.SetIdentity()
97 registration.SetFixedInitialTransform(identityTransform)
99 optimizer.SetLearningRate(4)
100 optimizer.SetMinimumStepLength(0.001)
101 optimizer.SetRelaxationFactor(0.5)
102 optimizer.SetNumberOfIterations(200)
104 registration.SetNumberOfLevels(1)
105 registration.SetSmoothingSigmasPerLevel([0])
106 registration.SetShrinkFactorsPerLevel([1])
108 registration.Update()
110 transform = registration.GetTransform()
112 finalParameters = transform.GetParameters()
113 TranslationAlongX = finalParameters.GetElement(0)
114 TranslationAlongY = finalParameters.GetElement(1)
116 numberOfIterations = optimizer.GetCurrentIteration()
118 bestValue = optimizer.GetValue()
121 print(
" Translation X = " + str(TranslationAlongX))
122 print(
" Translation Y = " + str(TranslationAlongY))
123 print(
" Iterations = " + str(numberOfIterations))
124 print(
" Metric value = " + str(bestValue))
127 outputCompositeTransform = CompositeTransformType.New()
128 outputCompositeTransform.AddTransform(movingInitialTransform)
129 outputCompositeTransform.AddTransform(registration.GetModifiableTransform())
132 resampler = ResampleFilterType.New()
133 resampler.SetInput(movingImageReader.GetOutput())
134 resampler.SetTransform(outputCompositeTransform)
136 fixedImage = fixedImageReader.GetOutput()
137 resampler.SetSize(fixedImage.GetLargestPossibleRegion().GetSize())
138 resampler.SetOutputOrigin(fixedImage.GetOrigin())
139 resampler.SetOutputSpacing(fixedImage.GetSpacing())
140 resampler.SetOutputDirection(fixedImage.GetDirection())
141 resampler.SetDefaultPixelValue(100)
143 OutputPixelType = itk.UC
144 OutputImageType =
itk.Image[OutputPixelType, Dimension]
149 writer = WriterType.New()
150 caster = CastFilterType.New()
152 writer.SetFileName(outputImage)
154 caster.SetInput(resampler.GetOutput())
155 writer.SetInput(caster.GetOutput())
159 FixedImageType, FixedImageType, FixedImageType]
161 difference = DifferenceFilterType.New()
163 difference.SetInput1(fixedImageReader.GetOutput())
164 difference.SetInput2(resampler.GetOutput())
167 intensityRescaler = RescalerType.New()
169 intensityRescaler.SetInput(difference.GetOutput())
173 resampler.SetDefaultPixelValue(1)
175 writer2 = WriterType.New()
176 writer2.SetInput(intensityRescaler.GetOutput())
178 writer2.SetFileName(differenceImageAfter)
181 resampler.SetTransform(identityTransform)
183 writer2.SetFileName(differenceImageBefore)