29 print(
"Missing Parameters")
31 "Usage: ImageRegistration5.py fixedImageFile movingImageFile outputImagefile"
54 fixedImageReader.SetFileName(argv[1])
55 movingImageReader.SetFileName(argv[2])
57 fixedImageReader.Update()
58 movingImageReader.Update()
60 fixedImage = fixedImageReader.GetOutput()
61 movingImage = movingImageReader.GetOutput()
67 registration = RegistrationType.New()
68 imageMetric = MetricType.New()
69 transform = TransformType.New()
70 optimizer = OptimizerType.New()
72 registration.SetOptimizer(optimizer)
73 registration.SetMetric(imageMetric)
75 registration.SetFixedImage(fixedImage)
76 registration.SetMovingImage(movingImage)
82 transform.SetAngle(0.0)
85 fixedSpacing = fixedImage.GetSpacing()
86 fixedOrigin = fixedImage.GetOrigin()
87 fixedSize = fixedImage.GetLargestPossibleRegion().GetSize()
90 fixedOrigin.GetElement(0)
91 + fixedSpacing.GetElement(0) * fixedSize.GetElement(0) / 2.0,
92 fixedOrigin.GetElement(1)
93 + fixedSpacing.GetElement(1) * fixedSize.GetElement(1) / 2.0,
97 movingSpacing = movingImage.GetSpacing()
98 movingOrigin = movingImage.GetOrigin()
99 movingSize = movingImage.GetLargestPossibleRegion().GetSize()
102 movingOrigin.GetElement(0)
103 + movingSpacing.GetElement(0) * movingSize.GetElement(0) / 2.0,
104 movingOrigin.GetElement(1)
105 + movingSpacing.GetElement(1) * movingSize.GetElement(1) / 2.0,
109 center = transform.GetCenter()
110 center.SetElement(0, centerFixed[0])
111 center.SetElement(1, centerFixed[1])
114 translation = transform.GetTranslation()
115 translation.SetElement(0, centerMoving[0] - centerFixed[0])
116 translation.SetElement(1, centerMoving[1] - centerFixed[1])
118 registration.SetInitialTransform(transform)
120 initialParameters = transform.GetParameters()
122 print(
"Initial Parameters: ")
123 print(f
"Angle: {initialParameters.GetElement(0):f}")
125 f
"Center: {initialParameters.GetElement(1):f}, {initialParameters.GetElement(2):f}"
128 "Translation: %f, %f"
129 % (initialParameters.GetElement(3), initialParameters.GetElement(4))
138 translationScale = 1.0 / 1000.0
141 optimizerScales.SetElement(0, 1.0)
142 optimizerScales.SetElement(1, translationScale)
143 optimizerScales.SetElement(2, translationScale)
144 optimizerScales.SetElement(3, translationScale)
145 optimizerScales.SetElement(4, translationScale)
147 optimizer.SetScales(optimizerScales)
149 optimizer.SetRelaxationFactor(0.6)
150 optimizer.SetLearningRate(0.1)
151 optimizer.SetMinimumStepLength(0.001)
152 optimizer.SetNumberOfIterations(200)
158 registration.SetNumberOfLevels(1)
159 registration.SetSmoothingSigmasPerLevel([0])
160 registration.SetShrinkFactorsPerLevel([1])
166 def iterationUpdate():
167 currentParameter = transform.GetParameters()
169 "M: %f P: %f %f %f %f %f "
171 optimizer.GetValue(),
172 currentParameter.GetElement(0),
173 currentParameter.GetElement(1),
174 currentParameter.GetElement(2),
175 currentParameter.GetElement(3),
176 currentParameter.GetElement(4),
182 iterationCommand.SetCommandCallable(iterationUpdate)
183 optimizer.AddObserver(itk.IterationEvent(), iterationCommand)
185 print(
"Starting registration")
191 registration.Update()
197 finalParameters = registration.GetOutput().Get().GetParameters()
199 print(
"Final Registration Parameters ")
200 print(f
"Angle in radians = {finalParameters.GetElement(0):f}")
201 print(f
"Rotation Center X = {finalParameters.GetElement(1):f}")
202 print(f
"Rotation Center Y = {finalParameters.GetElement(2):f}")
203 print(f
"Translation in X = {finalParameters.GetElement(3):f}")
204 print(f
"Translation in Y = {finalParameters.GetElement(4):f}")
212 resampler.SetTransform(registration.GetTransform())
213 resampler.SetInput(movingImageReader.GetOutput())
215 region = fixedImage.GetLargestPossibleRegion()
217 resampler.SetSize(region.GetSize())
218 resampler.SetOutputOrigin(fixedImage.GetOrigin())
219 resampler.SetOutputSpacing(fixedImage.GetSpacing())
220 resampler.SetOutputDirection(fixedImage.GetDirection())
221 resampler.SetDefaultPixelValue(100)
225 outputCast.SetInput(resampler.GetOutput())
232 writer.SetFileName(argv[3])
233 writer.SetInput(outputCast.GetOutput())