ITK  4.9.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 import itk
20 from sys import argv
21 
22 
23 #
24 # Check input parameters
25 # INPUTS(fixedImage): {BrainProtonDensitySliceBorder20.png}
26 # INPUTS(movingImage): {BrainProtonDensitySliceShifted13x17y.png}
27 #
28 if len(argv) < 4:
29  print 'Missing Parameters'
30  print 'Usage: ImageRegistration3.py fixedImageFile movingImageFile outputImagefile'
31  exit()
32 
33 
34 #
35 # Define data types
36 #
37 FixedImageType = itk.Image[itk.F, 2]
38 MovingImageType = itk.Image[itk.F, 2]
39 TransformType = itk.TranslationTransform[itk.D, 2]
40 OptimizerType = itk.RegularStepGradientDescentOptimizerv4[itk.D]
41 RegistrationType = itk.ImageRegistrationMethodv4[FixedImageType,
42  MovingImageType]
43 MetricType = itk.MeanSquaresImageToImageMetricv4[FixedImageType,
44  MovingImageType]
45 
46 
47 #
48 # Read the fixed and moving images using filenames
49 # from the command line arguments
50 #
51 fixedImageReader = itk.ImageFileReader[FixedImageType].New()
52 movingImageReader = itk.ImageFileReader[MovingImageType].New()
53 
54 fixedImageReader.SetFileName( argv[1])
55 movingImageReader.SetFileName( argv[2])
56 
57 fixedImageReader.Update()
58 movingImageReader.Update()
59 
60 fixedImage = fixedImageReader.GetOutput()
61 movingImage = movingImageReader.GetOutput()
62 
63 
64 #
65 # Instantiate the classes for the registration framework
66 #
67 registration = RegistrationType.New()
68 imageMetric = MetricType.New()
69 transform = TransformType.New()
70 optimizer = OptimizerType.New()
71 
72 registration.SetOptimizer(optimizer)
73 registration.SetMetric(imageMetric)
74 
75 registration.SetFixedImage(fixedImage)
76 registration.SetMovingImage(movingImage)
77 
78 registration.SetInitialTransform(transform)
79 
80 
81 #
82 # Define optimizer parameters
83 #
84 optimizer.SetLearningRate(4)
85 optimizer.SetMinimumStepLength(0.001)
86 optimizer.SetRelaxationFactor(0.5)
87 optimizer.SetNumberOfIterations(100)
88 
89 
90 #
91 # One level registration process without shrinking and smoothing.
92 #
93 registration.SetNumberOfLevels(1)
94 registration.SetSmoothingSigmasPerLevel([0])
95 registration.SetShrinkFactorsPerLevel([1])
96 
97 
98 #
99 # Iteration Observer
100 #
101 def iterationUpdate():
102  currentParameter = registration.GetOutput().Get().GetParameters()
103  print "M: %f P: %f %f " % ( optimizer.GetValue(),
104  currentParameter.GetElement(0),
105  currentParameter.GetElement(1))
106 
107 iterationCommand = itk.PyCommand.New()
108 iterationCommand.SetCommandCallable(iterationUpdate)
109 optimizer.AddObserver(itk.IterationEvent(),iterationCommand)
110 
111 print "Starting registration"
112 
113 
114 #
115 # Start the registration process
116 #
117 registration.Update()
118 
119 
120 #
121 # Get the final parameters of the transformation
122 #
123 finalParameters = registration.GetOutput().Get().GetParameters()
124 
125 print "Final Registration Parameters "
126 print "Translation X = %f" % (finalParameters.GetElement(0),)
127 print "Translation Y = %f" % (finalParameters.GetElement(1),)
128 
129 
130 #
131 # Now, we use the final transform for resampling the
132 # moving image.
133 #
134 resampler = itk.ResampleImageFilter[MovingImageType,FixedImageType].New()
135 resampler.SetTransform(registration.GetTransform())
136 resampler.SetInput(movingImageReader.GetOutput())
137 
138 region = fixedImage.GetLargestPossibleRegion()
139 
140 resampler.SetSize(region.GetSize())
141 resampler.SetOutputOrigin(fixedImage.GetOrigin())
142 resampler.SetOutputSpacing(fixedImage.GetSpacing())
143 resampler.SetOutputDirection(fixedImage.GetDirection())
144 resampler.SetDefaultPixelValue(100)
145 
146 OutputImageType = itk.Image[itk.UC, 2]
147 outputCast = itk.CastImageFilter[FixedImageType, OutputImageType].New()
148 outputCast.SetInput(resampler.GetOutput())
149 
150 
151 #
152 # Write the resampled image
153 #
154 writer = itk.ImageFileWriter[OutputImageType].New()
155 writer.SetFileName( argv[3] )
156 writer.SetInput( outputCast.GetOutput() )
157 writer.Update()