20 from InsightToolkit
import *
28 fixedImageReader = itkImageFileReaderF2_New()
29 movingImageReader = itkImageFileReaderF2_New()
31 fixedImageReader.SetFileName( argv[1] )
32 movingImageReader.SetFileName( argv[2] )
34 fixedImageReader.Update()
35 movingImageReader.Update()
37 fixedImage = fixedImageReader.GetOutput()
38 movingImage = movingImageReader.GetOutput()
43 registration = itkImageRegistrationMethodF2F2_New()
44 imageMetric = itkMeanSquaresImageToImageMetricF2F2_New()
45 transform = itkCenteredRigid2DTransform_New()
46 optimizer = itkRegularStepGradientDescentOptimizer_New()
47 interpolator = itkLinearInterpolateImageFunctionF2D_New()
49 registration.SetOptimizer( optimizer.GetPointer() )
50 registration.SetTransform( transform.GetPointer() )
51 registration.SetInterpolator( interpolator.GetPointer() )
52 registration.SetMetric( imageMetric.GetPointer() )
53 registration.SetFixedImage( fixedImage )
54 registration.SetMovingImage( movingImage )
55 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
61 transform.SetAngle( 0.0 );
64 fixedSpacing = fixedImage.GetSpacing()
65 fixedOrigin = fixedImage.GetOrigin()
66 fixedSize = fixedImage.GetLargestPossibleRegion().GetSize()
68 centerFixed = ( fixedOrigin.GetElement(0) + fixedSpacing.GetElement(0) * fixedSize.GetElement(0) / 2.0,
69 fixedOrigin.GetElement(1) + fixedSpacing.GetElement(1) * fixedSize.GetElement(1) / 2.0 )
72 movingSpacing = movingImage.GetSpacing()
73 movingOrigin = movingImage.GetOrigin()
74 movingSize = movingImage.GetLargestPossibleRegion().GetSize()
76 centerMoving = ( movingOrigin.GetElement(0) + movingSpacing.GetElement(0) * movingSize.GetElement(0) / 2.0,
77 movingOrigin.GetElement(1) + movingSpacing.GetElement(1) * movingSize.GetElement(1) / 2.0 )
80 center = transform.GetCenter()
81 center.SetElement( 0, centerFixed[0] )
82 center.SetElement( 1, centerFixed[1] )
85 translation = transform.GetTranslation()
86 translation.SetElement( 0, centerMoving[0] - centerFixed[0] )
87 translation.SetElement( 1, centerMoving[1] - centerFixed[1] )
89 initialParameters = transform.GetParameters()
91 print "Initial Parameters: "
92 print "Angle: %f" % (initialParameters.GetElement(0), )
93 print "Center: %f, %f" % ( initialParameters.GetElement(1), initialParameters.GetElement(2) )
94 print "Translation: %f, %f" % (initialParameters.GetElement(3), initialParameters.GetElement(4))
96 registration.SetInitialTransformParameters( initialParameters )
103 translationScale = 1.0 / 1000.0
105 optimizerScales = itkArrayD( transform.GetNumberOfParameters() )
106 optimizerScales.SetElement(0, 1.0)
107 optimizerScales.SetElement(1, translationScale)
108 optimizerScales.SetElement(2, translationScale)
109 optimizerScales.SetElement(3, translationScale)
110 optimizerScales.SetElement(4, translationScale)
112 optimizer.SetScales( optimizerScales )
113 optimizer.SetMaximumStepLength( 0.1 )
114 optimizer.SetMinimumStepLength( 0.001 )
115 optimizer.SetNumberOfIterations( 200 )
120 def iterationUpdate():
121 currentParameter = transform.GetParameters()
122 print "M: %f P: %f %f %f %f %f " % ( optimizer.GetValue(),
123 currentParameter.GetElement(0),
124 currentParameter.GetElement(1),
125 currentParameter.GetElement(2),
126 currentParameter.GetElement(3),
127 currentParameter.GetElement(4) )
129 iterationCommand = itkPyCommand_New()
130 iterationCommand.SetCommandCallable( iterationUpdate )
131 optimizer.AddObserver( itkIterationEvent(), iterationCommand.GetPointer() )
133 print "Starting registration"
139 registration.Update()
144 finalParameters = registration.GetLastTransformParameters()
146 print "Final Registration Parameters "
147 print "Angle in radians = %f" % finalParameters.GetElement(0)
148 print "Rotation Center X = %f" % finalParameters.GetElement(1)
149 print "Rotation Center Y = %f" % finalParameters.GetElement(2)
150 print "Translation in X = %f" % finalParameters.GetElement(3)
151 print "Translation in Y = %f" % finalParameters.GetElement(4)
154 resampler = itkResampleImageFilterF2F2_New()
156 resampler.SetTransform( transform.GetPointer() )
157 resampler.SetInput( movingImage )
159 region = fixedImage.GetLargestPossibleRegion()
161 resampler.SetSize( region.GetSize() )
162 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
163 resampler.SetOutputDirection( fixedImage.GetDirection() )
164 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
165 resampler.SetDefaultPixelValue( 100 )
170 outputCast = itkRescaleIntensityImageFilterF2US2_New()
171 outputCast.SetInput( resampler.GetOutput() )
172 outputCast.SetOutputMinimum( 0 )
173 outputCast.SetOutputMaximum( 65535 )
175 writer = itkImageFileWriterUS2_New()
177 writer.SetFileName( argv[3] )
178 writer.SetInput( outputCast.GetOutput() )