1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20""" A SimpleITK example demonstrating image registration using the
21 BSplineTransform and the ComplexCorrelation metric. """
22
23import sys
24import os
25import SimpleITK as sitk
26
27
28def command_iteration(method):
29 """ Callback invoked when the optimization has an iteration """
30 print(f"{method.GetOptimizerIteration():3} " + f"= {method.GetMetricValue():10.5f}")
31
32
33def main(args):
34 """ A SimpleITK example demonstrating image registration using the
35 BSplineTransform and the ComplexCorrelation metric. """
36 if len(args) < 4:
37 print(
38 "Usage:",
39 sys.argv[0],
40 "<fixedImageFilter> <movingImageFile>",
41 "<outputTransformFile>",
42 )
43 sys.exit(1)
44
46
48
49 transformDomainMeshSize = [8] * moving.GetDimension()
51
52 print("Initial Parameters:")
53 print(tx.GetParameters())
54
56 R.SetMetricAsCorrelation()
57
58 R.SetOptimizerAsLBFGSB(
59 gradientConvergenceTolerance=1e-5,
60 numberOfIterations=100,
61 maximumNumberOfCorrections=5,
62 maximumNumberOfFunctionEvaluations=1000,
63 costFunctionConvergenceFactor=1e7,
64 )
65 R.SetInitialTransform(tx, True)
66 R.SetInterpolator(sitk.sitkLinear)
67
68 R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R))
69
70 outTx = R.Execute(fixed, moving)
71
72 print("-------")
73 print(outTx)
74 print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
75 print(f" Iteration: {R.GetOptimizerIteration()}")
76 print(f" Metric value: {R.GetMetricValue()}")
77
79
81 resampler.SetReferenceImage(fixed)
82 resampler.SetInterpolator(sitk.sitkLinear)
83 resampler.SetDefaultPixelValue(100)
84 resampler.SetTransform(outTx)
85
86 out = resampler.Execute(moving)
89 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
90
91 return_images = {"fixed": fixed, "moving": moving, "composition": cimg}
92 return return_images
93
94
95if __name__ == "__main__":
96 return_dict = main(sys.argv)
97 if "SITK_NOSHOW" not in os.environ:
99 return_dict["composition"], "ImageRegistrationMethodBSpline1 Composition"
100 )
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())
BSplineTransform BSplineTransformInitializer(const Image &image1, const std::vector< uint32_t > &transformDomainMeshSize=std::vector< uint32_t >(3, 1u), unsigned int order=3u)
BSplineTransformInitializerFilter is a helper class intended to initialize the control point grid suc...
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)