29 print 'Missing Parameters'
30 print 'Usage: ImageRegistration3.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)
78 registration.SetInitialTransform(transform)
84 optimizer.SetLearningRate(4)
85 optimizer.SetMinimumStepLength(0.001)
86 optimizer.SetRelaxationFactor(0.5)
87 optimizer.SetNumberOfIterations(100)
93 registration.SetNumberOfLevels(1)
94 registration.SetSmoothingSigmasPerLevel([0])
95 registration.SetShrinkFactorsPerLevel([1])
101 def iterationUpdate():
102 currentParameter = registration.GetOutput().Get().GetParameters()
103 print "M: %f P: %f %f " % ( optimizer.GetValue(),
104 currentParameter.GetElement(0),
105 currentParameter.GetElement(1))
107 iterationCommand = itk.PyCommand.New()
108 iterationCommand.SetCommandCallable(iterationUpdate)
109 optimizer.AddObserver(itk.IterationEvent(),iterationCommand)
111 print "Starting registration"
117 registration.Update()
123 finalParameters = registration.GetOutput().Get().GetParameters()
125 print "Final Registration Parameters "
126 print "Translation X = %f" % (finalParameters.GetElement(0),)
127 print "Translation Y = %f" % (finalParameters.GetElement(1),)
135 resampler.SetTransform(registration.GetTransform())
136 resampler.SetInput(movingImageReader.GetOutput())
138 region = fixedImage.GetLargestPossibleRegion()
140 resampler.SetSize(region.GetSize())
141 resampler.SetOutputOrigin(fixedImage.GetOrigin())
142 resampler.SetOutputSpacing(fixedImage.GetSpacing())
143 resampler.SetOutputDirection(fixedImage.GetDirection())
144 resampler.SetDefaultPixelValue(100)
148 outputCast.SetInput(resampler.GetOutput())
155 writer.SetFileName( argv[3] )
156 writer.SetInput( outputCast.GetOutput() )