ITK  5.0.0
Insight Segmentation and Registration Toolkit
Examples/RegistrationITKv3/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 OutputImageType = itk.Image[itk.UC, 2]
40 TransformType = itk.TranslationTransform[itk.D, 2]
41 
42 
43 #
44 # Read the fixed and moving images using filenames
45 # from the command line arguments
46 #
47 fixedImageReader = itk.ImageFileReader[FixedImageType].New()
48 movingImageReader = itk.ImageFileReader[MovingImageType].New()
49 
50 fixedImageReader.SetFileName( argv[1] )
51 movingImageReader.SetFileName( argv[2] )
52 
53 fixedImageReader.Update()
54 movingImageReader.Update()
55 
56 fixedImage = fixedImageReader.GetOutput()
57 movingImage = movingImageReader.GetOutput()
58 
59 
60 #
61 # Instantiate the classes for the registration framework
62 #
63 registration = itk.ImageRegistrationMethod[FixedImageType, MovingImageType].New()
64 imageMetric = itk.MeanSquaresImageToImageMetric[FixedImageType, MovingImageType].New()
65 transform = TransformType.New()
67 interpolator = itk.LinearInterpolateImageFunction[FixedImageType, itk.D].New()
68 
69 registration.SetOptimizer( optimizer )
70 registration.SetTransform( transform )
71 registration.SetInterpolator( interpolator )
72 registration.SetMetric( imageMetric )
73 
74 registration.SetFixedImage( fixedImage )
75 registration.SetMovingImage( movingImage )
76 
77 registration.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
78 
79 transform.SetIdentity()
80 initialParameters = transform.GetParameters()
81 
82 registration.SetInitialTransformParameters( initialParameters )
83 
84 
85 #
86 # Iteration Observer
87 #
88 def iterationUpdate():
89  currentParameter = transform.GetParameters()
90  print "M: %f P: %f %f " % ( optimizer.GetValue(),
91  currentParameter.GetElement(0),
92  currentParameter.GetElement(1) )
93 
94 iterationCommand = itk.PyCommand.New()
95 iterationCommand.SetCommandCallable( iterationUpdate )
96 optimizer.AddObserver( itk.IterationEvent(), iterationCommand )
97 
98 
99 #
100 # Define optimizer parameters
101 #
102 optimizer.SetMaximumStepLength( 4.00 )
103 optimizer.SetMinimumStepLength( 0.01 )
104 optimizer.SetNumberOfIterations( 200 )
105 
106 print "Starting registration"
107 
108 
109 #
110 # Start the registration process
111 #
112 registration.Update()
113 
114 
115 #
116 # Get the final parameters of the transformation
117 #
118 finalParameters = registration.GetLastTransformParameters()
119 
120 print "Final Registration Parameters "
121 print "Translation X = %f" % (finalParameters.GetElement(0),)
122 print "Translation Y = %f" % (finalParameters.GetElement(1),)
123 
124 
125 #
126 # Now, we use the final transform for resampling the
127 # moving image.
128 #
129 resampler = itk.ResampleImageFilter[MovingImageType, FixedImageType].New()
130 resampler.SetTransform( transform )
131 resampler.SetInput( movingImage )
132 
133 region = fixedImage.GetLargestPossibleRegion()
134 
135 resampler.SetSize( region.GetSize() )
136 
137 resampler.SetOutputSpacing( fixedImage.GetSpacing() )
138 resampler.SetOutputOrigin( fixedImage.GetOrigin() )
139 resampler.SetOutputDirection( fixedImage.GetDirection() )
140 resampler.SetDefaultPixelValue( 100 )
141 
142 outputCast = itk.RescaleIntensityImageFilter[FixedImageType, OutputImageType].New()
143 outputCast.SetInput(resampler.GetOutput())
144 
145 
146 #
147 # Write the resampled image
148 #
149 writer = itk.ImageFileWriter[OutputImageType].New()
150 
151 writer.SetFileName( argv[3] )
152 writer.SetInput( outputCast.GetOutput() )
153 writer.Update()