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 = itkMattesMutualInformationImageToImageMetricF2F2_New()
49 transform = itkTranslationTransform2_New()
50 optimizer = itkRegularStepGradientDescentOptimizer_New()
51 interpolator = itkLinearInterpolateImageFunctionF2D_New()
54 imageMetric.SetNumberOfHistogramBins( 20 );
55 imageMetric.SetNumberOfSpatialSamples( 10000 );
57 registration.SetOptimizer( optimizer.GetPointer() )
58 registration.SetTransform( transform.GetPointer() )
59 registration.SetInterpolator( interpolator.GetPointer() )
60 registration.SetMetric( imageMetric.GetPointer() )
61 registration.SetFixedImage( fixedImage )
62 registration.SetMovingImage( movingImage )
64 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
66 transform.SetIdentity()
67 initialParameters = transform.GetParameters()
69 registration.SetInitialTransformParameters( initialParameters )
74 def iterationUpdate():
75 currentParameter = transform.GetParameters()
76 print "M: %f P: %f %f " % ( optimizer.GetValue(),
77 currentParameter.GetElement(0),
78 currentParameter.GetElement(1) )
80 iterationCommand = itkPyCommand_New()
81 iterationCommand.SetCommandCallable( iterationUpdate )
82 optimizer.AddObserver( itkIterationEvent(), iterationCommand.GetPointer() )
89 optimizer.SetMaximumStepLength( 4.00 )
90 optimizer.SetMinimumStepLength( 0.01 )
91 optimizer.SetNumberOfIterations( 200 )
94 print "Starting registration"
100 registration.Update()
106 finalParameters = registration.GetLastTransformParameters()
108 print "Final Registration Parameters "
109 print "Translation X = %f" % (finalParameters.GetElement(0),)
110 print "Translation Y = %f" % (finalParameters.GetElement(1),)
119 resampler = itkResampleImageFilterF2F2_New()
120 resampler.SetTransform( transform.GetPointer() )
121 resampler.SetInput( movingImage )
123 region = fixedImage.GetLargestPossibleRegion()
125 resampler.SetSize( region.GetSize() )
127 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
128 resampler.SetOutputDirection( fixedImage.GetDirection() )
129 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
130 resampler.SetDefaultPixelValue( 100 )
132 outputCast = itkRescaleIntensityImageFilterF2US2_New()
133 outputCast.SetOutputMinimum( 0 )
134 outputCast.SetOutputMaximum( 65535 )
135 outputCast.SetInput(resampler.GetOutput())
140 writer = itkImageFileWriterUS2_New()
142 writer.SetFileName( argv[3] )
143 writer.SetInput( outputCast.GetOutput() )