ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Registration/Common/Perform2DTranslationRegistrationWithMeanSquares/Code.py
1 #!/usr/bin/env python
2 
3 # Copyright NumFOCUS
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 import sys
18 import itk
19 
20 from distutils.version import StrictVersion as VS
21 if VS(itk.Version.GetITKVersion()) < VS("4.9.0"):
22  print("ITK 4.9.0 is required.")
23  sys.exit(1)
24 
25 if len(sys.argv) != 6:
26  print("Usage: " + sys.argv[0] + " <fixedImageFile> <movingImageFile> "
27  "<outputImagefile> <differenceImageAfter> <differenceImageBefore>")
28  sys.exit(1)
29 
30 fixedImageFile = sys.argv[1]
31 movingImageFile = sys.argv[2]
32 outputImageFile = sys.argv[3]
33 differenceImageAfterFile = sys.argv[4]
34 differenceImageBeforeFile = sys.argv[5]
35 
36 PixelType = itk.ctype('float')
37 
38 fixedImage = itk.imread(fixedImageFile, PixelType)
39 movingImage = itk.imread(movingImageFile, PixelType)
40 
41 Dimension = fixedImage.GetImageDimension()
42 FixedImageType = itk.Image[PixelType, Dimension]
43 MovingImageType = itk.Image[PixelType, Dimension]
44 
45 TransformType = itk.TranslationTransform[itk.D, Dimension]
46 initialTransform = TransformType.New()
47 
49  LearningRate=4,
50  MinimumStepLength=0.001,
51  RelaxationFactor=0.5,
52  NumberOfIterations=200)
53 
55  FixedImageType, MovingImageType].New()
56 
57 registration = itk.ImageRegistrationMethodv4.New(FixedImage=fixedImage,
58  MovingImage=movingImage,
59  Metric=metric,
60  Optimizer=optimizer,
61  InitialTransform=initialTransform)
62 
63 movingInitialTransform = TransformType.New()
64 initialParameters = movingInitialTransform.GetParameters()
65 initialParameters[0] = 0
66 initialParameters[1] = 0
67 movingInitialTransform.SetParameters(initialParameters)
68 registration.SetMovingInitialTransform(movingInitialTransform)
69 
70 identityTransform = TransformType.New()
71 identityTransform.SetIdentity()
72 registration.SetFixedInitialTransform(identityTransform)
73 
74 registration.SetNumberOfLevels(1)
75 registration.SetSmoothingSigmasPerLevel([0])
76 registration.SetShrinkFactorsPerLevel([1])
77 
78 registration.Update()
79 
80 transform = registration.GetTransform()
81 finalParameters = transform.GetParameters()
82 translationAlongX = finalParameters.GetElement(0)
83 translationAlongY = finalParameters.GetElement(1)
84 
85 numberOfIterations = optimizer.GetCurrentIteration()
86 
87 bestValue = optimizer.GetValue()
88 
89 print("Result = ")
90 print(" Translation X = " + str(translationAlongX))
91 print(" Translation Y = " + str(translationAlongY))
92 print(" Iterations = " + str(numberOfIterations))
93 print(" Metric value = " + str(bestValue))
94 
95 CompositeTransformType = itk.CompositeTransform[itk.D, Dimension]
96 outputCompositeTransform = CompositeTransformType.New()
97 outputCompositeTransform.AddTransform(movingInitialTransform)
98 outputCompositeTransform.AddTransform(registration.GetModifiableTransform())
99 
100 resampler = itk.ResampleImageFilter.New(Input=movingImage,
101  Transform=outputCompositeTransform,
102  UseReferenceImage=True,
103  ReferenceImage=fixedImage)
104 resampler.SetDefaultPixelValue(100)
105 
106 OutputPixelType = itk.ctype('unsigned char')
107 OutputImageType = itk.Image[OutputPixelType, Dimension]
108 
109 caster = itk.CastImageFilter[FixedImageType,
110  OutputImageType].New(Input=resampler)
111 
112 writer = itk.ImageFileWriter.New(Input=caster, FileName=outputImageFile)
113 writer.SetFileName(outputImageFile)
114 writer.Update()
115 
116 difference = itk.SubtractImageFilter.New(Input1=fixedImage,
117  Input2=resampler)
118 
119 intensityRescaler = itk.RescaleIntensityImageFilter[FixedImageType,
120  OutputImageType].New(
121  Input=difference,
122  OutputMinimum=itk.NumericTraits[OutputPixelType].min(),
123  OutputMaximum=itk.NumericTraits[OutputPixelType].max())
124 
125 resampler.SetDefaultPixelValue(1)
126 writer.SetInput(intensityRescaler.GetOutput())
127 writer.SetFileName(differenceImageAfterFile)
128 writer.Update()
129 
130 resampler.SetTransform(identityTransform)
131 writer.SetFileName(differenceImageBeforeFile)
132 writer.Update()
itk::CastImageFilter
Casts input pixels to output pixel type.
Definition: itkCastImageFilter.h:104
itk::CompositeTransform
This class contains a list of transforms and concatenates them by composition.
Definition: itkCompositeTransform.h:87
itk::SubtractImageFilter::New
static Pointer New()
itk::Version::GetITKVersion
static const char * GetITKVersion()
itk::ImageRegistrationMethodv4::New
static Pointer New()
itk::TranslationTransform
Translation transformation of a vector space (e.g. space coordinates)
Definition: itkTranslationTransform.h:43
itk::RegularStepGradientDescentOptimizerv4::New
static Pointer New()
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::ImageFileWriter::New
static Pointer New()
itk::MeanSquaresImageToImageMetricv4
Class implementing a mean squares metric.
Definition: itkMeanSquaresImageToImageMetricv4.h:46
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::ResampleImageFilter::New
static Pointer New()