20 """ A SimpleITK example demonstrating image registration using the exhaustive
26 import SimpleITK
as sitk
29 def command_iteration(method):
30 """ Callback invoked when the optimization has an iteration """
31 if method.GetOptimizerIteration() == 0:
32 print(
"Scales: ", method.GetOptimizerScales())
34 f
"{method.GetOptimizerIteration():3} "
35 + f
"= {method.GetMetricValue():7.5f} "
36 + f
": {method.GetOptimizerPosition()}"
44 "<fixedImageFilter> <movingImageFile>",
45 "<outputTransformFile>",
55 R.SetMetricAsMattesMutualInformation(numberOfHistogramBins=50)
60 if fixed.GetDimension() == 2:
64 R.SetOptimizerAsExhaustive([sample_per_axis // 2, 0, 0])
66 R.SetOptimizerScales([2.0 * pi / sample_per_axis, 1.0, 1.0])
67 elif fixed.GetDimension() == 3:
69 R.SetOptimizerAsExhaustive(
81 2.0 * pi / sample_per_axis,
82 2.0 * pi / sample_per_axis,
83 2.0 * pi / sample_per_axis,
94 R.SetInitialTransform(tx)
96 R.SetInterpolator(sitk.sitkLinear)
98 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
100 outTx = R.Execute(fixed, moving)
104 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
105 print(f
" Iteration: {R.GetOptimizerIteration()}")
106 print(f
" Metric value: {R.GetMetricValue()}")
110 if "SITK_NOSHOW" not in os.environ:
112 resampler.SetReferenceImage(fixed)
113 resampler.SetInterpolator(sitk.sitkLinear)
114 resampler.SetDefaultPixelValue(1)
115 resampler.SetTransform(outTx)
117 out = resampler.Execute(moving)
121 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
122 sitk.Show(cimg,
"ImageRegistrationExhaustive Composition")