ITK  6.0.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 # https://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 import argparse
20 
21 from distutils.version import StrictVersion as VS
22 
23 if VS(itk.Version.GetITKVersion()) < VS("4.9.0"):
24  print("ITK 4.9.0 is required.")
25  sys.exit(1)
26 
27 parser = argparse.ArgumentParser(
28  description="Perform 2D Translation Registration With Mean Squares."
29 )
30 parser.add_argument("fixed_input_image")
31 parser.add_argument("moving_input_image")
32 parser.add_argument("output_image")
33 parser.add_argument("difference_image_after")
34 parser.add_argument("difference_image_before")
35 args = parser.parse_args()
36 
37 PixelType = itk.ctype("float")
38 
39 fixedImage = itk.imread(args.fixed_input_image, PixelType)
40 movingImage = itk.imread(args.moving_input_image, PixelType)
41 
42 Dimension = fixedImage.GetImageDimension()
43 FixedImageType = itk.Image[PixelType, Dimension]
44 MovingImageType = itk.Image[PixelType, Dimension]
45 
46 TransformType = itk.TranslationTransform[itk.D, Dimension]
47 initialTransform = TransformType.New()
48 
50  LearningRate=4,
51  MinimumStepLength=0.001,
52  RelaxationFactor=0.5,
53  NumberOfIterations=200,
54 )
55 
56 metric = itk.MeanSquaresImageToImageMetricv4[FixedImageType, MovingImageType].New()
57 
59  FixedImage=fixedImage,
60  MovingImage=movingImage,
61  Metric=metric,
62  Optimizer=optimizer,
63  InitialTransform=initialTransform,
64 )
65 
66 movingInitialTransform = TransformType.New()
67 initialParameters = movingInitialTransform.GetParameters()
68 initialParameters[0] = 0
69 initialParameters[1] = 0
70 movingInitialTransform.SetParameters(initialParameters)
71 registration.SetMovingInitialTransform(movingInitialTransform)
72 
73 identityTransform = TransformType.New()
74 identityTransform.SetIdentity()
75 registration.SetFixedInitialTransform(identityTransform)
76 
77 registration.SetNumberOfLevels(1)
78 registration.SetSmoothingSigmasPerLevel([0])
79 registration.SetShrinkFactorsPerLevel([1])
80 
81 registration.Update()
82 
83 transform = registration.GetTransform()
84 finalParameters = transform.GetParameters()
85 translationAlongX = finalParameters.GetElement(0)
86 translationAlongY = finalParameters.GetElement(1)
87 
88 numberOfIterations = optimizer.GetCurrentIteration()
89 
90 bestValue = optimizer.GetValue()
91 
92 print("Result = ")
93 print(" Translation X = " + str(translationAlongX))
94 print(" Translation Y = " + str(translationAlongY))
95 print(" Iterations = " + str(numberOfIterations))
96 print(" Metric value = " + str(bestValue))
97 
98 CompositeTransformType = itk.CompositeTransform[itk.D, Dimension]
99 outputCompositeTransform = CompositeTransformType.New()
100 outputCompositeTransform.AddTransform(movingInitialTransform)
101 outputCompositeTransform.AddTransform(registration.GetModifiableTransform())
102 
103 resampler = itk.ResampleImageFilter.New(
104  Input=movingImage,
105  Transform=outputCompositeTransform,
106  UseReferenceImage=True,
107  ReferenceImage=fixedImage,
108 )
109 resampler.SetDefaultPixelValue(100)
110 
111 OutputPixelType = itk.ctype("unsigned char")
112 OutputImageType = itk.Image[OutputPixelType, Dimension]
113 
114 caster = itk.CastImageFilter[FixedImageType, OutputImageType].New(Input=resampler)
115 
116 writer = itk.ImageFileWriter.New(Input=caster, FileName=args.output_image)
117 writer.Update()
118 
119 difference = itk.SubtractImageFilter.New(Input1=fixedImage, Input2=resampler)
120 
121 intensityRescaler = itk.RescaleIntensityImageFilter[
122  FixedImageType, OutputImageType
123 ].New(
124  Input=difference,
125  OutputMinimum=itk.NumericTraits[OutputPixelType].min(),
126  OutputMaximum=itk.NumericTraits[OutputPixelType].max(),
127 )
128 
129 resampler.SetDefaultPixelValue(1)
130 writer.SetInput(intensityRescaler.GetOutput())
131 writer.SetFileName(args.difference_image_after)
132 writer.Update()
133 
134 resampler.SetTransform(identityTransform)
135 writer.SetFileName(args.difference_image_before)
136 writer.Update()
itk::CastImageFilter
Casts input pixels to output pixel type.
Definition: itkCastImageFilter.h:100
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:60
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:133
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::ResampleImageFilter::New
static Pointer New()