29 print 'Missing Parameters'
30 print 'Usage: ImageRegistration4.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)
77 imageMetric.SetNumberOfHistogramBins( numberOfBins );
78 imageMetric.SetUseMovingImageGradientFilter(
False );
79 imageMetric.SetUseFixedImageGradientFilter(
False );
81 registration.SetFixedImage(fixedImage)
82 registration.SetMovingImage(movingImage)
84 registration.SetInitialTransform(transform)
90 optimizer.SetLearningRate(8.00)
91 optimizer.SetMinimumStepLength(0.001)
92 optimizer.SetNumberOfIterations(100)
93 optimizer.ReturnBestParametersAndValueOn();
94 optimizer.SetRelaxationFactor(0.8)
100 registration.SetNumberOfLevels(1)
101 registration.SetSmoothingSigmasPerLevel([0])
102 registration.SetShrinkFactorsPerLevel([1])
104 registration.SetMetricSamplingStrategy( RegistrationType.RANDOM );
105 registration.SetMetricSamplingPercentage( 0.20 );
111 def iterationUpdate():
112 currentParameter = registration.GetOutput().Get().GetParameters()
113 print "M: %f P: %f %f " % ( optimizer.GetValue(),
114 currentParameter.GetElement(0),
115 currentParameter.GetElement(1))
117 iterationCommand = itk.PyCommand.New()
118 iterationCommand.SetCommandCallable(iterationUpdate)
119 optimizer.AddObserver(itk.IterationEvent(),iterationCommand)
121 print "Starting registration"
127 registration.Update()
133 finalParameters = registration.GetOutput().Get().GetParameters()
135 print "Final Registration Parameters "
136 print "Translation X = %f" % (finalParameters.GetElement(0),)
137 print "Translation Y = %f" % (finalParameters.GetElement(1),)
145 resampler.SetTransform(registration.GetTransform())
146 resampler.SetInput(movingImageReader.GetOutput())
148 region = fixedImage.GetLargestPossibleRegion()
150 resampler.SetSize(region.GetSize())
151 resampler.SetOutputOrigin(fixedImage.GetOrigin())
152 resampler.SetOutputSpacing(fixedImage.GetSpacing())
153 resampler.SetOutputDirection(fixedImage.GetDirection())
154 resampler.SetDefaultPixelValue(100)
158 outputCast.SetInput(resampler.GetOutput())
165 writer.SetFileName( argv[3] )
166 writer.SetInput( outputCast.GetOutput() )