20 import SimpleITK
as sitk
25 def command_iteration(method, bspline_transform):
26 if method.GetOptimizerIteration() == 0:
30 print(bspline_transform)
33 f
"{method.GetOptimizerIteration():3} "
34 + f
"= {method.GetMetricValue():10.5f}"
38 def command_multi_iteration(method):
42 if R.GetCurrentLevel() > 0:
44 "Optimizer stop condition: "
45 + f
"{R.GetOptimizerStopConditionDescription()}"
47 print(f
" Iteration: {R.GetOptimizerIteration()}")
48 print(f
" Metric value: {R.GetMetricValue()}")
50 print(
"--------- Resolution Changing ---------")
57 "<fixedImageFilter> <movingImageFile>",
58 "<outputTransformFile>",
66 transformDomainMeshSize = [2] * fixed.GetDimension()
69 print(f
"Initial Number of Parameters: {tx.GetNumberOfParameters()}")
72 R.SetMetricAsJointHistogramMutualInformation()
74 R.SetOptimizerAsGradientDescentLineSearch(
75 5.0, 100, convergenceMinimumValue=1e-4, convergenceWindowSize=5
78 R.SetInterpolator(sitk.sitkLinear)
80 R.SetInitialTransformAsBSpline(tx, inPlace=
True, scaleFactors=[1, 2, 5])
81 R.SetShrinkFactorsPerLevel([4, 2, 1])
82 R.SetSmoothingSigmasPerLevel([4, 2, 1])
84 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R, tx))
86 sitk.sitkMultiResolutionIterationEvent,
lambda: command_multi_iteration(R)
89 outTx = R.Execute(fixed, moving)
94 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
95 print(f
" Iteration: {R.GetOptimizerIteration()}")
96 print(f
" Metric value: {R.GetMetricValue()}")
100 if "SITK_NOSHOW" not in os.environ:
102 resampler.SetReferenceImage(fixed)
103 resampler.SetInterpolator(sitk.sitkLinear)
104 resampler.SetDefaultPixelValue(100)
105 resampler.SetTransform(outTx)
107 out = resampler.Execute(moving)
110 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
111 sitk.Show(cimg,
"Image Registration Composition")