29 print 'Missing Parameters'
30 print 'Usage: ImageRegistration3.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 registration.SetOptimizer( optimizer )
70 registration.SetTransform( transform )
71 registration.SetInterpolator( interpolator )
72 registration.SetMetric( imageMetric )
74 registration.SetFixedImage( fixedImage )
75 registration.SetMovingImage( movingImage )
77 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
79 transform.SetIdentity()
80 initialParameters = transform.GetParameters()
82 registration.SetInitialTransformParameters( initialParameters )
88 def iterationUpdate():
89 currentParameter = transform.GetParameters()
90 print "M: %f P: %f %f " % ( optimizer.GetValue(),
91 currentParameter.GetElement(0),
92 currentParameter.GetElement(1) )
94 iterationCommand = itk.PyCommand.New()
95 iterationCommand.SetCommandCallable( iterationUpdate )
96 optimizer.AddObserver( itk.IterationEvent(), iterationCommand )
102 optimizer.SetMaximumStepLength( 4.00 )
103 optimizer.SetMinimumStepLength( 0.01 )
104 optimizer.SetNumberOfIterations( 200 )
106 print "Starting registration"
112 registration.Update()
118 finalParameters = registration.GetLastTransformParameters()
120 print "Final Registration Parameters "
121 print "Translation X = %f" % (finalParameters.GetElement(0),)
122 print "Translation Y = %f" % (finalParameters.GetElement(1),)
130 resampler.SetTransform( transform )
131 resampler.SetInput( movingImage )
133 region = fixedImage.GetLargestPossibleRegion()
135 resampler.SetSize( region.GetSize() )
137 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
138 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
139 resampler.SetOutputDirection( fixedImage.GetDirection() )
140 resampler.SetDefaultPixelValue( 100 )
143 outputCast.SetInput(resampler.GetOutput())
151 writer.SetFileName( argv[3] )
152 writer.SetInput( outputCast.GetOutput() )