19 from InsightToolkit
import *
27 fixedImageReader = itkImageFileReaderF2_New()
28 movingImageReader = itkImageFileReaderF2_New()
30 fixedImageReader.SetFileName( argv[1] )
31 movingImageReader.SetFileName( argv[2] )
33 fixedImageReader.Update()
34 movingImageReader.Update()
36 fixedImage = fixedImageReader.GetOutput()
37 movingImage = movingImageReader.GetOutput()
45 registration = itkImageRegistrationMethodF2F2_New()
46 imageMetric = itkMeanSquaresImageToImageMetricF2F2_New()
47 transform = itkTranslationTransform2_New()
48 optimizer = itkRegularStepGradientDescentOptimizer_New()
49 interpolator = itkLinearInterpolateImageFunctionF2D_New()
52 registration.SetOptimizer( optimizer.GetPointer() )
53 registration.SetTransform( transform.GetPointer() )
54 registration.SetInterpolator( interpolator.GetPointer() )
55 registration.SetMetric( imageMetric.GetPointer() )
56 registration.SetFixedImage( fixedImage )
57 registration.SetMovingImage( movingImage )
59 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
61 transform.SetIdentity()
62 initialParameters = transform.GetParameters()
64 registration.SetInitialTransformParameters( initialParameters )
69 def iterationUpdate():
70 currentParameter = transform.GetParameters()
71 print "M: %f P: %f %f " % ( optimizer.GetValue(),
72 currentParameter.GetElement(0),
73 currentParameter.GetElement(1) )
75 iterationCommand = itkPyCommand_New()
76 iterationCommand.SetCommandCallable( iterationUpdate )
77 optimizer.AddObserver( itkIterationEvent(), iterationCommand.GetPointer() )
84 optimizer.SetMaximumStepLength( 4.00 )
85 optimizer.SetMinimumStepLength( 0.01 )
86 optimizer.SetNumberOfIterations( 200 )
89 print "Starting registration"
101 finalParameters = registration.GetLastTransformParameters()
103 print "Final Registration Parameters "
104 print "Translation X = %f" % (finalParameters.GetElement(0),)
105 print "Translation Y = %f" % (finalParameters.GetElement(1),)
114 resampler = itkResampleImageFilterF2F2_New()
115 resampler.SetTransform( transform.GetPointer() )
116 resampler.SetInput( movingImage )
118 region = fixedImage.GetLargestPossibleRegion()
120 resampler.SetSize( region.GetSize() )
122 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
123 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
124 resampler.SetOutputDirection( fixedImage.GetDirection() )
125 resampler.SetDefaultPixelValue( 100 )
127 outputCast = itkRescaleIntensityImageFilterF2US2_New()
128 outputCast.SetOutputMinimum( 0 )
129 outputCast.SetOutputMaximum( 65535 )
130 outputCast.SetInput(resampler.GetOutput())
135 writer = itkImageFileWriterUS2_New()
137 writer.SetFileName( argv[3] )
138 writer.SetInput( outputCast.GetOutput() )