20 import SimpleITK
as sitk
25 def command_iteration(method):
27 f
"{method.GetOptimizerIteration():3} "
28 + f
"= {method.GetMetricValue():10.5f} "
29 + f
": {method.GetOptimizerPosition()}"
37 "<fixedImageFilter> <movingImageFile>",
38 "<outputTransformFile>",
47 R.SetMetricAsMeanSquares()
48 R.SetOptimizerAsRegularStepGradientDescent(4.0, 0.01, 200)
50 R.SetInterpolator(sitk.sitkLinear)
52 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
54 outTx = R.Execute(fixed, moving)
58 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
59 print(f
" Iteration: {R.GetOptimizerIteration()}")
60 print(f
" Metric value: {R.GetMetricValue()}")
64 if "SITK_NOSHOW" not in os.environ:
66 resampler.SetReferenceImage(fixed)
67 resampler.SetInterpolator(sitk.sitkLinear)
68 resampler.SetDefaultPixelValue(100)
69 resampler.SetTransform(outTx)
71 out = resampler.Execute(moving)
74 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
75 sitk.Show(cimg,
"ImageRegistration1 Composition")