20 import SimpleITK
as sitk
25 def command_iteration(method):
26 if method.GetOptimizerIteration() == 0:
27 print(
"Estimated Scales: ", method.GetOptimizerScales())
29 f
"{method.GetOptimizerIteration():3} "
30 + f
"= {method.GetMetricValue():7.5f} "
31 + f
": {method.GetOptimizerPosition()}"
39 "<fixedImageFilter> <movingImageFile> <outputTransformFile>",
43 pixelType = sitk.sitkFloat32
51 R.SetMetricAsCorrelation()
53 R.SetOptimizerAsRegularStepGradientDescent(
56 numberOfIterations=500,
57 gradientMagnitudeTolerance=1e-8,
59 R.SetOptimizerScalesFromIndexShift()
64 R.SetInitialTransform(tx)
66 R.SetInterpolator(sitk.sitkLinear)
68 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
70 outTx = R.Execute(fixed, moving)
74 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
75 print(f
" Iteration: {R.GetOptimizerIteration()}")
76 print(f
" Metric value: {R.GetMetricValue()}")
80 if "SITK_NOSHOW" not in os.environ:
82 resampler.SetReferenceImage(fixed)
83 resampler.SetInterpolator(sitk.sitkLinear)
84 resampler.SetDefaultPixelValue(1)
85 resampler.SetTransform(outTx)
87 out = resampler.Execute(moving)
91 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
92 sitk.Show(cimg,
"ImageRegistration2 Composition")