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