1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20""" A SimpleITK example demonstrating basic image registration using the
21 TranlationTransform and the gradient descent optimizer. """
22
23import sys
24import os
25import SimpleITK as sitk
26
27
28def command_iteration(method):
29 """ Callback invoked when the optimization process is performing an iteration. """
30 print(
31 f"{method.GetOptimizerIteration():3} "
32 + f"= {method.GetMetricValue():10.5f} "
33 + f": {method.GetOptimizerPosition()}"
34 )
35
36
37def main(args):
38 """ A basic SimpleITK image registration example. """
39
40 if len(args) < 3:
41 print(
42 "Usage:",
43 "ImageRegistrationMethod1",
44 "<fixedImageFilter> <movingImageFile>",
45 "<outputTransformFile>",
46 )
47 sys.exit(1)
48
50
52
54 R.SetMetricAsMeanSquares()
55 R.SetOptimizerAsRegularStepGradientDescent(4.0, 0.01, 200)
57 R.SetInterpolator(sitk.sitkLinear)
58
59 R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R))
60
61 outTx = R.Execute(fixed, moving)
62
63 print("-------")
64 print(outTx)
65 print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
66 print(f" Iteration: {R.GetOptimizerIteration()}")
67 print(f" Metric value: {R.GetMetricValue()}")
68
70
72 resampler.SetReferenceImage(fixed)
73 resampler.SetInterpolator(sitk.sitkLinear)
74 resampler.SetDefaultPixelValue(100)
75 resampler.SetTransform(outTx)
76
77 out = resampler.Execute(moving)
80 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
81
82 return_images = {"fixed": fixed, "moving": moving, "composition": cimg}
83 return return_images
84
85
86if __name__ == "__main__":
87 return_dict = main(sys.argv)
88 if "SITK_NOSHOW" not in os.environ:
89 sitk.Show(return_dict[
"composition"],
"ImageRegistration1 Composition")
An interface method to the modular ITKv4 registration framework.
Resample an image via a coordinate transform.
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...
Image Compose(const std::vector< Image > &images)
ComposeImageFilter combine several scalar images into a multicomponent image.
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)
Image RescaleIntensity(Image &&image1, double outputMinimum=0, double outputMaximum=255)
Applies a linear transformation to the intensity levels of the input Image .
Image Cast(const Image &image, PixelIDValueEnum pixelID)