20 """ A SimpleITK example demonstrating image registration using Mattes mutual
21 information as the metric. """
25 import SimpleITK
as sitk
28 def command_iteration(method):
29 """ Callback invoked when the optimization has an iteration. """
31 f
"{method.GetOptimizerIteration():3} "
32 + f
"= {method.GetMetricValue():10.5f} "
33 + f
": {method.GetOptimizerPosition()}"
38 """ A SimpleITK example demonstrating image registration using Mattes mutual
39 information as the metric. """
44 "ImageRegistrationMethod4",
45 "<fixedImageFilter> <movingImageFile>",
46 "<outputTransformFile> <numberOfBins> <samplingPercentage>",
54 samplingPercentage = 0.10
57 numberOfBins = int(args[4])
59 samplingPercentage = float(args[5])
62 R.SetMetricAsMattesMutualInformation(numberOfBins)
63 R.SetMetricSamplingPercentage(samplingPercentage, sitk.sitkWallClock)
64 R.SetMetricSamplingStrategy(R.RANDOM)
65 R.SetOptimizerAsRegularStepGradientDescent(1.0, 0.001, 200)
67 R.SetInterpolator(sitk.sitkLinear)
69 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
71 outTx = R.Execute(fixed, moving)
75 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
76 print(f
" Iteration: {R.GetOptimizerIteration()}")
77 print(f
" Metric value: {R.GetMetricValue()}")
82 resampler.SetReferenceImage(fixed)
83 resampler.SetInterpolator(sitk.sitkLinear)
84 resampler.SetDefaultPixelValue(100)
85 resampler.SetTransform(outTx)
87 out = resampler.Execute(moving)
90 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
92 return {
"fixed": fixed,
"moving": moving,
"composition": cimg}
95 if __name__ ==
"__main__":
96 return_dict = main(sys.argv)
97 if "SITK_NOSHOW" not in os.environ:
98 sitk.Show(return_dict[
"composition"],
"ImageRegistration4 Composition")