20 """ A SimpleITK example demonstrating image registration using the
21 BSplineTransform and the MattesMutualInformation metric. """
25 import SimpleITK
as sitk
28 def command_iteration(method):
29 """ Callback invoked each iteration. """
30 print(f
"{method.GetOptimizerIteration():3} " + f
"= {method.GetMetricValue():10.5f}")
31 print(
"\t#: ", len(method.GetOptimizerPosition()))
34 def command_multi_iteration(_):
35 """ Callback invoked at the end of each multi-resolution iteration. """
36 print(
"--------- Resolution Changing ---------")
43 "<fixedImageFilter> <movingImageFile>",
44 "<outputTransformFile>",
52 transformDomainMeshSize = [10] * moving.GetDimension()
55 print(
"Initial Parameters:")
56 print(tx.GetParameters())
59 R.SetMetricAsMattesMutualInformation(50)
60 R.SetOptimizerAsGradientDescentLineSearch(
61 5.0, 100, convergenceMinimumValue=1e-4, convergenceWindowSize=5
63 R.SetOptimizerScalesFromPhysicalShift()
64 R.SetInitialTransform(tx)
65 R.SetInterpolator(sitk.sitkLinear)
67 R.SetShrinkFactorsPerLevel([6, 2, 1])
68 R.SetSmoothingSigmasPerLevel([6, 2, 1])
70 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
71 R.AddCommand(sitk.sitkMultiResolutionIterationEvent,
lambda: command_multi_iteration(R))
73 outTx = R.Execute(fixed, moving)
77 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
78 print(f
" Iteration: {R.GetOptimizerIteration()}")
79 print(f
" Metric value: {R.GetMetricValue()}")
83 if "SITK_NOSHOW" not in os.environ:
85 resampler.SetReferenceImage(fixed)
86 resampler.SetInterpolator(sitk.sitkLinear)
87 resampler.SetDefaultPixelValue(100)
88 resampler.SetTransform(outTx)
90 out = resampler.Execute(moving)
93 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
94 sitk.Show(cimg,
"ImageRegistration1 Composition")