20 """ A SimpleITK example demonstrating image registration using the
21 correlation metric and the center of mass initial transformation
22 estimation method. """
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(
"Estimated Scales: ", method.GetOptimizerScales())
34 f
"{method.GetOptimizerIteration():3} "
35 + f
"= {method.GetMetricValue():7.5f} "
36 + f
": {method.GetOptimizerPosition()}"
41 """ A SimpleITK example demonstrating image registration using the
42 correlation metric and the center of mass initial transformation
43 estimation method. """
47 "ImageRegistrationMethod3"
48 "<fixedImageFilter> <movingImageFile> <outputTransformFile>",
58 R.SetMetricAsCorrelation()
60 R.SetOptimizerAsRegularStepGradientDescent(
63 numberOfIterations=500,
64 gradientMagnitudeTolerance=1e-8,
66 R.SetOptimizerScalesFromIndexShift()
69 R.SetInitialTransform(tx)
71 R.SetInterpolator(sitk.sitkLinear)
73 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
75 outTx = R.Execute(fixed, moving)
79 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
80 print(f
" Iteration: {R.GetOptimizerIteration()}")
81 print(f
" Metric value: {R.GetMetricValue()}")
86 resampler.SetReferenceImage(fixed)
87 resampler.SetInterpolator(sitk.sitkLinear)
88 resampler.SetDefaultPixelValue(100)
89 resampler.SetTransform(outTx)
91 out = resampler.Execute(moving)
95 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
97 return {
"fixed": fixed,
"moving": moving,
"composition": cimg}
100 if __name__ ==
"__main__":
101 return_dict = main(sys.argv)
102 if "SITK_NOSHOW" not in os.environ:
103 sitk.Show(return_dict[
"composition"],
"ImageRegistration3 Composition")