20 """ A SimpleITK example demonstrating basic image registration using the
21 TranlationTransform and the gradient descent optimizer. """
25 import SimpleITK
as sitk
28 def command_iteration(method):
29 """ Callback invoked when the optimization process is performing an iteration. """
31 f
"{method.GetOptimizerIteration():3} "
32 + f
"= {method.GetMetricValue():10.5f} "
33 + f
": {method.GetOptimizerPosition()}"
38 """ A basic SimpleITK image registration example. """
43 "ImageRegistrationMethod1",
44 "<fixedImageFilter> <movingImageFile>",
45 "<outputTransformFile>",
54 R.SetMetricAsMeanSquares()
55 R.SetOptimizerAsRegularStepGradientDescent(4.0, 0.01, 200)
57 R.SetInterpolator(sitk.sitkLinear)
59 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
61 outTx = R.Execute(fixed, moving)
65 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
66 print(f
" Iteration: {R.GetOptimizerIteration()}")
67 print(f
" Metric value: {R.GetMetricValue()}")
72 resampler.SetReferenceImage(fixed)
73 resampler.SetInterpolator(sitk.sitkLinear)
74 resampler.SetDefaultPixelValue(100)
75 resampler.SetTransform(outTx)
77 out = resampler.Execute(moving)
80 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
82 return_images = {
"fixed": fixed,
"moving": moving,
"composition": cimg}
86 if __name__ ==
"__main__":
87 return_dict = main(sys.argv)
88 if "SITK_NOSHOW" not in os.environ:
89 sitk.Show(return_dict[
"composition"],
"ImageRegistration1 Composition")