SimpleITK  
ImageRegistrationMethodExhaustive/ImageRegistrationMethodExhaustive.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # =========================================================================
19 
20 import SimpleITK as sitk
21 import sys
22 import os
23 from math import pi
24 
25 
26 def command_iteration(method):
27  if method.GetOptimizerIteration() == 0:
28  print("Scales: ", method.GetOptimizerScales())
29  print(
30  f"{method.GetOptimizerIteration():3} "
31  + f"= {method.GetMetricValue():7.5f} "
32  + f": {method.GetOptimizerPosition()}"
33  )
34 
35 
36 if len(sys.argv) < 4:
37  print(
38  "Usage:",
39  sys.argv[0],
40  "<fixedImageFilter> <movingImageFile>",
41  "<outputTransformFile>",
42  )
43  sys.exit(1)
44 
45 fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
46 
47 moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
48 
50 
51 R.SetMetricAsMattesMutualInformation(numberOfHistogramBins=50)
52 
53 sample_per_axis = 12
54 if fixed.GetDimension() == 2:
56  # Set the number of samples (radius) in each dimension, with a
57  # default step size of 1.0
58  R.SetOptimizerAsExhaustive([sample_per_axis // 2, 0, 0])
59  # Utilize the scale to set the step size for each dimension
60  R.SetOptimizerScales([2.0 * pi / sample_per_axis, 1.0, 1.0])
61 elif fixed.GetDimension() == 3:
63  R.SetOptimizerAsExhaustive(
64  [
65  sample_per_axis // 2,
66  sample_per_axis // 2,
67  sample_per_axis // 4,
68  0,
69  0,
70  0,
71  ]
72  )
73  R.SetOptimizerScales(
74  [
75  2.0 * pi / sample_per_axis,
76  2.0 * pi / sample_per_axis,
77  2.0 * pi / sample_per_axis,
78  1.0,
79  1.0,
80  1.0,
81  ]
82  )
83 
84 # Initialize the transform with a translation and the center of
85 # rotation from the moments of intensity.
86 tx = sitk.CenteredTransformInitializer(fixed, moving, tx)
87 
88 R.SetInitialTransform(tx)
89 
90 R.SetInterpolator(sitk.sitkLinear)
91 
92 R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R))
93 
94 outTx = R.Execute(fixed, moving)
95 
96 print("-------")
97 print(outTx)
98 print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
99 print(f" Iteration: {R.GetOptimizerIteration()}")
100 print(f" Metric value: {R.GetMetricValue()}")
101 
102 sitk.WriteTransform(outTx, sys.argv[3])
103 
104 if "SITK_NOSHOW" not in os.environ:
105  resampler = sitk.ResampleImageFilter()
106  resampler.SetReferenceImage(fixed)
107  resampler.SetInterpolator(sitk.sitkLinear)
108  resampler.SetDefaultPixelValue(1)
109  resampler.SetTransform(outTx)
110 
111  out = resampler.Execute(moving)
112 
113  simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
114  simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
115  cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
116  sitk.Show(cimg, "ImageRegistrationExhaustive Composition")
itk::simple::WriteTransform
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const std::string &filename)
itk::simple::RescaleIntensity
Image RescaleIntensity(const Image &image1, double outputMinimum=0, double outputMaximum=255)
Applies a linear transformation to the intensity levels of the input Image .
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< std::string > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...
itk::simple::Cast
Image Cast(const Image &image, PixelIDValueEnum pixelID)
itk::simple::Euler2DTransform
A rigid 2D transform with rotation in radians around a fixed center with translation.
Definition: sitkEuler2DTransform.h:33
itk::simple::ResampleImageFilter
Resample an image via a coordinate transform.
Definition: sitkResampleImageFilter.h:52
itk::simple::Compose
Image Compose(const Image &image1, const Image &image2, const Image &image3, const Image &image4, const Image &image5)
ComposeImageFilter combine several scalar images into a multicomponent image.
itk::simple::ImageRegistrationMethod
An interface method to the modular ITKv4 registration framework.
Definition: sitkImageRegistrationMethod.h:89
itk::simple::Euler3DTransform
A rigid 3D transform with rotation in radians around a fixed center with translation.
Definition: sitkEuler3DTransform.h:33
itk::simple::CenteredTransformInitializer
Transform CenteredTransformInitializer(const Image &fixedImage, const Image &movingImage, const Transform &transform, CenteredTransformInitializerFilter::OperationModeType operationMode=itk::simple::CenteredTransformInitializerFilter::MOMENTS)
CenteredTransformInitializer is a helper class intended to initialize the center of rotation and the ...