ITK  5.0.0
Insight Segmentation and Registration Toolkit
Examples/RegistrationITKv4/ImageRegistration4.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: ImageRegistration4.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.MattesMutualInformationImageToImageMetricv4[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 numberOfBins = 24
76 
77 imageMetric.SetNumberOfHistogramBins( numberOfBins );
78 imageMetric.SetUseMovingImageGradientFilter( False );
79 imageMetric.SetUseFixedImageGradientFilter( False );
80 
81 registration.SetFixedImage(fixedImage)
82 registration.SetMovingImage(movingImage)
83 
84 registration.SetInitialTransform(transform)
85 
86 
87 #
88 # Define optimizer parameters
89 #
90 optimizer.SetLearningRate(8.00)
91 optimizer.SetMinimumStepLength(0.001)
92 optimizer.SetNumberOfIterations(100)
93 optimizer.ReturnBestParametersAndValueOn();
94 optimizer.SetRelaxationFactor(0.8)
95 
96 
97 #
98 # One level registration process without shrinking and smoothing.
99 #
100 registration.SetNumberOfLevels(1)
101 registration.SetSmoothingSigmasPerLevel([0])
102 registration.SetShrinkFactorsPerLevel([1])
103 
104 registration.SetMetricSamplingStrategy( RegistrationType.RANDOM );
105 registration.SetMetricSamplingPercentage( 0.20 );
106 
107 
108 #
109 # Iteration Observer
110 #
111 def iterationUpdate():
112  currentParameter = registration.GetOutput().Get().GetParameters()
113  print "M: %f P: %f %f " % ( optimizer.GetValue(),
114  currentParameter.GetElement(0),
115  currentParameter.GetElement(1))
116 
117 iterationCommand = itk.PyCommand.New()
118 iterationCommand.SetCommandCallable(iterationUpdate)
119 optimizer.AddObserver(itk.IterationEvent(),iterationCommand)
120 
121 print "Starting registration"
122 
123 
124 #
125 # Start the registration process
126 #
127 registration.Update()
128 
129 
130 #
131 # Get the final parameters of the transformation
132 #
133 finalParameters = registration.GetOutput().Get().GetParameters()
134 
135 print "Final Registration Parameters "
136 print "Translation X = %f" % (finalParameters.GetElement(0),)
137 print "Translation Y = %f" % (finalParameters.GetElement(1),)
138 
139 
140 #
141 # Now, we use the final transform for resampling the
142 # moving image.
143 #
144 resampler = itk.ResampleImageFilter[MovingImageType,FixedImageType].New()
145 resampler.SetTransform(registration.GetTransform())
146 resampler.SetInput(movingImageReader.GetOutput())
147 
148 region = fixedImage.GetLargestPossibleRegion()
149 
150 resampler.SetSize(region.GetSize())
151 resampler.SetOutputOrigin(fixedImage.GetOrigin())
152 resampler.SetOutputSpacing(fixedImage.GetSpacing())
153 resampler.SetOutputDirection(fixedImage.GetDirection())
154 resampler.SetDefaultPixelValue(100)
155 
156 OutputImageType = itk.Image[itk.UC, 2]
157 outputCast = itk.CastImageFilter[FixedImageType, OutputImageType].New()
158 outputCast.SetInput(resampler.GetOutput())
159 
160 
161 #
162 # Write the resampled image
163 #
164 writer = itk.ImageFileWriter[OutputImageType].New()
165 writer.SetFileName( argv[3] )
166 writer.SetInput( outputCast.GetOutput() )
167 writer.Update()