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)
82 transform.SetAngle( 0.0 )
85 fixedSpacing = fixedImage.GetSpacing()
86 fixedOrigin = fixedImage.GetOrigin()
87 fixedSize = fixedImage.GetLargestPossibleRegion().GetSize()
89 centerFixed = ( fixedOrigin.GetElement(0) + fixedSpacing.GetElement(0) * fixedSize.GetElement(0) / 2.0,
90 fixedOrigin.GetElement(1) + fixedSpacing.GetElement(1) * fixedSize.GetElement(1) / 2.0 )
93 movingSpacing = movingImage.GetSpacing()
94 movingOrigin = movingImage.GetOrigin()
95 movingSize = movingImage.GetLargestPossibleRegion().GetSize()
97 centerMoving = ( movingOrigin.GetElement(0) + movingSpacing.GetElement(0) * movingSize.GetElement(0) / 2.0,
98 movingOrigin.GetElement(1) + movingSpacing.GetElement(1) * movingSize.GetElement(1) / 2.0 )
101 center = transform.GetCenter()
102 center.SetElement( 0, centerFixed[0] )
103 center.SetElement( 1, centerFixed[1] )
106 translation = transform.GetTranslation()
107 translation.SetElement( 0, centerMoving[0] - centerFixed[0] )
108 translation.SetElement( 1, centerMoving[1] - centerFixed[1] )
110 registration.SetInitialTransform(transform)
112 initialParameters = transform.GetParameters()
114 print "Initial Parameters: "
115 print "Angle: %f" % (initialParameters.GetElement(0), )
116 print "Center: %f, %f" % ( initialParameters.GetElement(1), initialParameters.GetElement(2) )
117 print "Translation: %f, %f" % (initialParameters.GetElement(3), initialParameters.GetElement(4))
125 translationScale = 1.0 / 1000.0
128 optimizerScales.SetElement(0, 1.0)
129 optimizerScales.SetElement(1, translationScale)
130 optimizerScales.SetElement(2, translationScale)
131 optimizerScales.SetElement(3, translationScale)
132 optimizerScales.SetElement(4, translationScale)
134 optimizer.SetScales( optimizerScales )
136 optimizer.SetRelaxationFactor( 0.6 );
137 optimizer.SetLearningRate( 0.1 );
138 optimizer.SetMinimumStepLength( 0.001 );
139 optimizer.SetNumberOfIterations( 200 );
145 registration.SetNumberOfLevels(1)
146 registration.SetSmoothingSigmasPerLevel([0])
147 registration.SetShrinkFactorsPerLevel([1])
153 def iterationUpdate():
154 currentParameter = transform.GetParameters()
155 print "M: %f P: %f %f %f %f %f " % ( optimizer.GetValue(),
156 currentParameter.GetElement(0),
157 currentParameter.GetElement(1),
158 currentParameter.GetElement(2),
159 currentParameter.GetElement(3),
160 currentParameter.GetElement(4) )
162 iterationCommand = itk.PyCommand.New()
163 iterationCommand.SetCommandCallable( iterationUpdate )
164 optimizer.AddObserver( itk.IterationEvent(), iterationCommand )
166 print "Starting registration"
172 registration.Update()
178 finalParameters = registration.GetOutput().Get().GetParameters()
180 print "Final Registration Parameters "
181 print "Angle in radians = %f" % finalParameters.GetElement(0)
182 print "Rotation Center X = %f" % finalParameters.GetElement(1)
183 print "Rotation Center Y = %f" % finalParameters.GetElement(2)
184 print "Translation in X = %f" % finalParameters.GetElement(3)
185 print "Translation in Y = %f" % finalParameters.GetElement(4)
193 resampler.SetTransform(registration.GetTransform())
194 resampler.SetInput(movingImageReader.GetOutput())
196 region = fixedImage.GetLargestPossibleRegion()
198 resampler.SetSize(region.GetSize())
199 resampler.SetOutputOrigin(fixedImage.GetOrigin())
200 resampler.SetOutputSpacing(fixedImage.GetSpacing())
201 resampler.SetOutputDirection(fixedImage.GetDirection())
202 resampler.SetDefaultPixelValue(100)
206 outputCast.SetInput(resampler.GetOutput())
213 writer.SetFileName( argv[3] )
214 writer.SetInput( outputCast.GetOutput() )