29 print 'Missing Parameters'
30 print 'Usage: ImageRegistration4.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 imageMetric.SetNumberOfHistogramBins( 20 );
70 imageMetric.SetNumberOfSpatialSamples( 10000 );
72 registration.SetOptimizer( optimizer )
73 registration.SetTransform( transform )
74 registration.SetInterpolator( interpolator )
75 registration.SetMetric( imageMetric )
77 registration.SetFixedImage( fixedImage )
78 registration.SetMovingImage( movingImage )
80 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
82 transform.SetIdentity()
83 initialParameters = transform.GetParameters()
85 registration.SetInitialTransformParameters( initialParameters )
91 def iterationUpdate():
92 currentParameter = transform.GetParameters()
93 print "M: %f P: %f %f " % ( optimizer.GetValue(),
94 currentParameter.GetElement(0),
95 currentParameter.GetElement(1) )
97 iterationCommand = itk.PyCommand.New()
98 iterationCommand.SetCommandCallable( iterationUpdate )
99 optimizer.AddObserver( itk.IterationEvent(), iterationCommand )
105 optimizer.SetMaximumStepLength( 4.00 )
106 optimizer.SetMinimumStepLength( 0.01 )
107 optimizer.SetNumberOfIterations( 200 )
109 print "Starting registration"
115 registration.Update()
121 finalParameters = registration.GetLastTransformParameters()
123 print "Final Registration Parameters "
124 print "Translation X = %f" % (finalParameters.GetElement(0),)
125 print "Translation Y = %f" % (finalParameters.GetElement(1),)
133 resampler.SetTransform( transform )
134 resampler.SetInput( movingImage )
136 region = fixedImage.GetLargestPossibleRegion()
138 resampler.SetSize( region.GetSize() )
140 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
141 resampler.SetOutputDirection( fixedImage.GetDirection() )
142 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
143 resampler.SetDefaultPixelValue( 100 )
146 outputCast.SetInput(resampler.GetOutput())
154 writer.SetFileName( argv[3] )
155 writer.SetInput( outputCast.GetOutput() )