SimpleITK  
ImageRegistrationOptimizerWeights/ImageRegistrationOptimizerWeights.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 
21 """ A SimpleITK example demonstrating image registration using optimizer
22  weights. """
23 
24 import sys
25 import SimpleITK as sitk
26 
27 if len(sys.argv) < 4:
28  print(
29  "Usage:",
30  sys.argv[0],
31  "<fixedImageFile> <movingImageFile>",
32  "<outputTransformFile>",
33  )
34  sys.exit(1)
35 
36 fixed_image = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
37 moving_image = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
38 
39 # initialization
41  fixed_image,
42  moving_image,
44  sitk.CenteredTransformInitializerFilter.GEOMETRY,
45 )
46 # registration
47 registration_method = sitk.ImageRegistrationMethod()
48 registration_method.SetMetricAsCorrelation()
49 registration_method.SetMetricSamplingStrategy(registration_method.NONE)
50 registration_method.SetInterpolator(sitk.sitkLinear)
51 registration_method.SetOptimizerAsGradientDescent(
52  learningRate=1.0,
53  numberOfIterations=300,
54  convergenceMinimumValue=1e-6,
55  convergenceWindowSize=10,
56 )
57 registration_method.SetOptimizerScalesFromPhysicalShift()
58 registration_method.SetInitialTransform(transform, inPlace=True)
59 registration_method.SetOptimizerWeights([0, 0, 1, 1, 1, 1])
60 registration_method.Execute(fixed_image, moving_image)
61 
62 print("-------")
63 print(f"Final transform parameters: {transform.GetParameters()}")
64 print(
65  "Optimizer stop condition: "
66  + f"{registration_method.GetOptimizerStopConditionDescription()}"
67 )
68 print(f"Iteration: {registration_method.GetOptimizerIteration()}")
69 print(f"Metric value: {registration_method.GetMetricValue()}")
70 
71 sitk.WriteTransform(transform, sys.argv[3])
itk::simple::WriteTransform
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)
itk::simple::ImageRegistrationMethod
An interface method to the modular ITKv4 registration framework.
Definition: sitkImageRegistrationMethod.h:87
itk::simple::Euler3DTransform
A rigid 3D transform with rotation in radians around a fixed center with translation.
Definition: sitkEuler3DTransform.h:33
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< PathType > &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::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 ...