29 print 'Missing Parameters'
30 print 'Usage: ImageRegistration5.py fixedImageFile movingImageFile outputImagefile'
50 fixedImageReader.SetFileName( argv[1] )
51 movingImageReader.SetFileName( argv[2] )
53 fixedImageReader.Update()
54 movingImageReader.Update()
56 fixedImage = fixedImageReader.GetOutput()
57 movingImage = movingImageReader.GetOutput()
65 transform = TransformType.New()
69 registration.SetOptimizer( optimizer )
70 registration.SetTransform( transform )
71 registration.SetInterpolator( interpolator)
72 registration.SetMetric( imageMetric )
74 registration.SetFixedImage( fixedImage )
75 registration.SetMovingImage( movingImage )
77 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
83 transform.SetAngle( 0.0 );
86 fixedSpacing = fixedImage.GetSpacing()
87 fixedOrigin = fixedImage.GetOrigin()
88 fixedSize = fixedImage.GetLargestPossibleRegion().GetSize()
90 centerFixed = ( fixedOrigin.GetElement(0) + fixedSpacing.GetElement(0) * fixedSize.GetElement(0) / 2.0,
91 fixedOrigin.GetElement(1) + fixedSpacing.GetElement(1) * fixedSize.GetElement(1) / 2.0 )
94 movingSpacing= movingImage.GetSpacing()
95 movingOrigin = movingImage.GetOrigin()
96 movingSize = movingImage.GetLargestPossibleRegion().GetSize()
98 centerMoving = ( movingOrigin.GetElement(0) + movingSpacing.GetElement(0) * movingSize.GetElement(0) / 2.0,
99 movingOrigin.GetElement(1) + movingSpacing.GetElement(1) * movingSize.GetElement(1) / 2.0 )
102 center = transform.GetCenter()
103 center.SetElement( 0, centerFixed[0] )
104 center.SetElement( 1, centerFixed[1] )
107 translation = transform.GetTranslation()
108 translation.SetElement( 0, centerMoving[0] - centerFixed[0] )
109 translation.SetElement( 1, centerMoving[1] - centerFixed[1] )
111 initialParameters = transform.GetParameters()
113 print "Initial Parameters: "
114 print "Angle: %f" % (initialParameters.GetElement(0), )
115 print "Center: %f, %f" % ( initialParameters.GetElement(1), initialParameters.GetElement(2) )
116 print "Translation: %f, %f" % (initialParameters.GetElement(3), initialParameters.GetElement(4))
118 registration.SetInitialTransformParameters( initialParameters )
126 translationScale = 1.0 / 1000.0
128 optimizerScales =
itk.Array[itk.D](transform.GetNumberOfParameters())
129 optimizerScales.SetElement(0, 1.0)
130 optimizerScales.SetElement(1, translationScale)
131 optimizerScales.SetElement(2, translationScale)
132 optimizerScales.SetElement(3, translationScale)
133 optimizerScales.SetElement(4, translationScale)
135 optimizer.SetScales( optimizerScales )
136 optimizer.SetMaximumStepLength( 0.1 )
137 optimizer.SetMinimumStepLength( 0.001 )
138 optimizer.SetNumberOfIterations( 200 )
144 def iterationUpdate():
145 currentParameter = transform.GetParameters()
146 print "M: %f P: %f %f %f %f %f " % ( optimizer.GetValue(),
147 currentParameter.GetElement(0),
148 currentParameter.GetElement(1),
149 currentParameter.GetElement(2),
150 currentParameter.GetElement(3),
151 currentParameter.GetElement(4) )
153 iterationCommand = itk.PyCommand.New()
154 iterationCommand.SetCommandCallable( iterationUpdate )
155 optimizer.AddObserver( itk.IterationEvent(), iterationCommand )
157 print "Starting registration"
163 registration.Update()
169 finalParameters = registration.GetLastTransformParameters()
171 print "Final Registration Parameters "
172 print "Angle in radians = %f" % finalParameters.GetElement(0)
173 print "Rotation Center X = %f" % finalParameters.GetElement(1)
174 print "Rotation Center Y = %f" % finalParameters.GetElement(2)
175 print "Translation in X = %f" % finalParameters.GetElement(3)
176 print "Translation in Y = %f" % finalParameters.GetElement(4)
180 resampler.SetTransform( transform )
181 resampler.SetInput( movingImage )
183 region = fixedImage.GetLargestPossibleRegion()
185 resampler.SetSize( region.GetSize() )
187 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
188 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
189 resampler.SetOutputDirection( fixedImage.GetDirection() )
190 resampler.SetDefaultPixelValue( 100 )
197 outputCast.SetInput(resampler.GetOutput())
205 writer.SetFileName( argv[3] )
206 writer.SetInput( outputCast.GetOutput() )