ITK  4.8.0
Insight Segmentation and Registration Toolkit
Examples/RegistrationITKv4/ImageRegistration3.py
1 #==========================================================================
2 #
3 # Copyright Insight Software Consortium
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #==========================================================================*/
18 
19 from InsightToolkit import *
20 
21 from sys import argv
22 
23 
24 
25 #
26 # Read the fixed and moving images using filenames
27 # from the command line arguments
28 #
29 fixedImageReader = itkImageFileReaderF2_New()
30 movingImageReader = itkImageFileReaderF2_New()
31 
32 fixedImageReader.SetFileName( argv[1] )
33 movingImageReader.SetFileName( argv[2] )
34 
35 fixedImageReader.Update()
36 movingImageReader.Update()
37 
38 fixedImage = fixedImageReader.GetOutput()
39 movingImage = movingImageReader.GetOutput()
40 
41 
42 
43 
44 #
45 # Instantiate the classes for the registration framework
46 #
47 registration = itkImageRegistrationMethodF2F2_New()
48 imageMetric = itkMeanSquaresImageToImageMetricF2F2_New()
49 transform = itkTranslationTransform2_New()
50 optimizer = itkRegularStepGradientDescentOptimizer_New()
51 interpolator = itkLinearInterpolateImageFunctionF2D_New()
52 
53 
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 )
60 
61 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
62 
63 transform.SetIdentity()
64 initialParameters = transform.GetParameters()
65 
66 registration.SetInitialTransformParameters( initialParameters )
67 
68 #
69 # Iteration Observer
70 #
71 def iterationUpdate():
72  currentParameter = transform.GetParameters()
73  print "M: %f P: %f %f " % ( optimizer.GetValue(),
74  currentParameter.GetElement(0),
75  currentParameter.GetElement(1) )
76 
77 iterationCommand = itkPyCommand_New()
78 iterationCommand.SetCommandCallable( iterationUpdate )
79 optimizer.AddObserver( itkIterationEvent(), iterationCommand.GetPointer() )
80 
81 
82 
83 #
84 # Define optimizer parameters
85 #
86 optimizer.SetMaximumStepLength( 4.00 )
87 optimizer.SetMinimumStepLength( 0.01 )
88 optimizer.SetNumberOfIterations( 200 )
89 
90 
91 print "Starting registration"
92 
93 #
94 # Start the registration process
95 #
96 
97 registration.Update()
98 
99 
100 #
101 # Get the final parameters of the transformation
102 #
103 finalParameters = registration.GetLastTransformParameters()
104 
105 print "Final Registration Parameters "
106 print "Translation X = %f" % (finalParameters.GetElement(0),)
107 print "Translation Y = %f" % (finalParameters.GetElement(1),)
108 
109 
110 
111 
112 #
113 # Now, we use the final transform for resampling the
114 # moving image.
115 #
116 resampler = itkResampleImageFilterF2F2_New()
117 resampler.SetTransform( transform.GetPointer() )
118 resampler.SetInput( movingImage )
119 
120 region = fixedImage.GetLargestPossibleRegion()
121 
122 resampler.SetSize( region.GetSize() )
123 
124 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
125 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
126 resampler.SetOutputDirection( fixedImage.GetDirection() )
127 resampler.SetDefaultPixelValue( 100 )
128 
129 outputCast = itkRescaleIntensityImageFilterF2US2_New()
130 outputCast.SetOutputMinimum( 0 )
131 outputCast.SetOutputMaximum( 65535 )
132 outputCast.SetInput(resampler.GetOutput())
133 
134 #
135 # Write the resampled image
136 #
137 writer = itkImageFileWriterUS2_New()
138 
139 writer.SetFileName( argv[3] )
140 writer.SetInput( outputCast.GetOutput() )
141 writer.Update()