19 from InsightToolkit
import *
29 fixedImageReader = itkImageFileReaderF2_New()
30 movingImageReader = itkImageFileReaderF2_New()
32 fixedImageReader.SetFileName( argv[1] )
33 movingImageReader.SetFileName( argv[2] )
35 fixedImageReader.Update()
36 movingImageReader.Update()
38 fixedImage = fixedImageReader.GetOutput()
39 movingImage = movingImageReader.GetOutput()
47 registration = itkImageRegistrationMethodF2F2_New()
48 imageMetric = itkMeanSquaresImageToImageMetricF2F2_New()
49 transform = itkTranslationTransform2_New()
50 optimizer = itkRegularStepGradientDescentOptimizer_New()
51 interpolator = itkLinearInterpolateImageFunctionF2D_New()
54 registration.SetOptimizer( optimizer.GetPointer() )
55 registration.SetTransform( transform.GetPointer() )
56 registration.SetInterpolator( interpolator.GetPointer() )
57 registration.SetMetric( imageMetric.GetPointer() )
58 registration.SetFixedImage( fixedImage )
59 registration.SetMovingImage( movingImage )
61 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
63 transform.SetIdentity()
64 initialParameters = transform.GetParameters()
66 registration.SetInitialTransformParameters( initialParameters )
71 def iterationUpdate():
72 currentParameter = transform.GetParameters()
73 print "M: %f P: %f %f " % ( optimizer.GetValue(),
74 currentParameter.GetElement(0),
75 currentParameter.GetElement(1) )
77 iterationCommand = itkPyCommand_New()
78 iterationCommand.SetCommandCallable( iterationUpdate )
79 optimizer.AddObserver( itkIterationEvent(), iterationCommand.GetPointer() )
86 optimizer.SetMaximumStepLength( 4.00 )
87 optimizer.SetMinimumStepLength( 0.01 )
88 optimizer.SetNumberOfIterations( 200 )
91 print "Starting registration"
103 finalParameters = registration.GetLastTransformParameters()
105 print "Final Registration Parameters "
106 print "Translation X = %f" % (finalParameters.GetElement(0),)
107 print "Translation Y = %f" % (finalParameters.GetElement(1),)
116 resampler = itkResampleImageFilterF2F2_New()
117 resampler.SetTransform( transform.GetPointer() )
118 resampler.SetInput( movingImage )
120 region = fixedImage.GetLargestPossibleRegion()
122 resampler.SetSize( region.GetSize() )
124 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
125 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
126 resampler.SetOutputDirection( fixedImage.GetDirection() )
127 resampler.SetDefaultPixelValue( 100 )
129 outputCast = itkRescaleIntensityImageFilterF2US2_New()
130 outputCast.SetOutputMinimum( 0 )
131 outputCast.SetOutputMaximum( 65535 )
132 outputCast.SetInput(resampler.GetOutput())
137 writer = itkImageFileWriterUS2_New()
139 writer.SetFileName( argv[3] )
140 writer.SetInput( outputCast.GetOutput() )