29 print(
"Missing Parameters")
31 "Usage: ImageRegistration4.py fixedImageFile movingImageFile outputImagefile"
45 FixedImageType, MovingImageType
56 fixedImageReader.SetFileName(argv[1])
57 movingImageReader.SetFileName(argv[2])
59 fixedImageReader.Update()
60 movingImageReader.Update()
62 fixedImage = fixedImageReader.GetOutput()
63 movingImage = movingImageReader.GetOutput()
69 registration = RegistrationType.New()
70 imageMetric = MetricType.New()
71 transform = TransformType.New()
72 optimizer = OptimizerType.New()
74 registration.SetOptimizer(optimizer)
75 registration.SetMetric(imageMetric)
79 imageMetric.SetNumberOfHistogramBins(numberOfBins)
80 imageMetric.SetUseMovingImageGradientFilter(
False)
81 imageMetric.SetUseFixedImageGradientFilter(
False)
83 registration.SetFixedImage(fixedImage)
84 registration.SetMovingImage(movingImage)
86 registration.SetInitialTransform(transform)
92 optimizer.SetLearningRate(8.00)
93 optimizer.SetMinimumStepLength(0.001)
94 optimizer.SetNumberOfIterations(100)
95 optimizer.ReturnBestParametersAndValueOn()
96 optimizer.SetRelaxationFactor(0.8)
102 registration.SetNumberOfLevels(1)
103 registration.SetSmoothingSigmasPerLevel([0])
104 registration.SetShrinkFactorsPerLevel([1])
106 registration.SetMetricSamplingStrategy(RegistrationType.RANDOM)
107 registration.SetMetricSamplingPercentage(0.20)
113 def iterationUpdate():
114 currentParameter = registration.GetOutput().Get().GetParameters()
118 optimizer.GetValue(),
119 currentParameter.GetElement(0),
120 currentParameter.GetElement(1),
126 iterationCommand.SetCommandCallable(iterationUpdate)
127 optimizer.AddObserver(itk.IterationEvent(), iterationCommand)
129 print(
"Starting registration")
135 registration.Update()
141 finalParameters = registration.GetOutput().Get().GetParameters()
143 print(
"Final Registration Parameters ")
144 print(f
"Translation X = {finalParameters.GetElement(0):f}")
145 print(f
"Translation Y = {finalParameters.GetElement(1):f}")
153 resampler.SetTransform(registration.GetTransform())
154 resampler.SetInput(movingImageReader.GetOutput())
156 region = fixedImage.GetLargestPossibleRegion()
158 resampler.SetSize(region.GetSize())
159 resampler.SetOutputOrigin(fixedImage.GetOrigin())
160 resampler.SetOutputSpacing(fixedImage.GetSpacing())
161 resampler.SetOutputDirection(fixedImage.GetDirection())
162 resampler.SetDefaultPixelValue(100)
166 outputCast.SetInput(resampler.GetOutput())
173 writer.SetFileName(argv[3])
174 writer.SetInput(outputCast.GetOutput())