20 """ A SimpleITK example demonstrating image registration using the
21 DisplacementFieldTransform and the JointHistogramMutualInformation
26 import SimpleITK
as sitk
29 def command_iteration(method):
30 """ Callback invoked each iteration. """
31 if method.GetOptimizerIteration() == 0:
32 print(f
"\tLevel: {method.GetCurrentLevel()}")
33 print(f
"\tScales: {method.GetOptimizerScales()}")
34 print(f
"#{method.GetOptimizerIteration()}")
35 print(f
"\tMetric Value: {method.GetMetricValue():10.5f}")
36 print(f
"\tLearningRate: {method.GetOptimizerLearningRate():10.5f}")
37 if method.GetOptimizerConvergenceValue() != sys.float_info.max:
38 print(
"\tConvergence Value: " + f
"{method.GetOptimizerConvergenceValue():.5e}")
41 def command_multiresolution_iteration(method):
42 """ Callback invoked at the end of each multi-resolution level. """
43 print(f
"\tStop Condition: {method.GetOptimizerStopConditionDescription()}")
44 print(
"============= Resolution Change =============")
51 "<fixedImageFilter> <movingImageFile>",
52 "<outputTransformFile>",
66 R.SetShrinkFactorsPerLevel([3, 2, 1])
67 R.SetSmoothingSigmasPerLevel([2, 1, 1])
69 R.SetMetricAsJointHistogramMutualInformation(20)
70 R.MetricUseFixedImageGradientFilterOff()
72 R.SetOptimizerAsGradientDescent(
74 numberOfIterations=100,
75 estimateLearningRate=R.EachIteration,
77 R.SetOptimizerScalesFromPhysicalShift()
79 R.SetInitialTransform(initialTx)
81 R.SetInterpolator(sitk.sitkLinear)
83 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
85 sitk.sitkMultiResolutionIterationEvent,
86 lambda: command_multiresolution_iteration(R),
89 outTx1 = R.Execute(fixed, moving)
93 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
94 print(f
" Iteration: {R.GetOptimizerIteration()}")
95 print(f
" Metric value: {R.GetMetricValue()}")
97 displacementField =
sitk.Image(fixed.GetSize(), sitk.sitkVectorFloat64)
98 displacementField.CopyInformation(fixed)
100 del displacementField
101 displacementTx.SetSmoothingGaussianOnUpdate(
102 varianceForUpdateField=0.0, varianceForTotalField=1.5
105 R.SetMovingInitialTransform(outTx1)
106 R.SetInitialTransform(displacementTx, inPlace=
True)
108 R.SetMetricAsANTSNeighborhoodCorrelation(4)
109 R.MetricUseFixedImageGradientFilterOff()
111 R.SetShrinkFactorsPerLevel([3, 2, 1])
112 R.SetSmoothingSigmasPerLevel([2, 1, 1])
114 R.SetOptimizerScalesFromPhysicalShift()
115 R.SetOptimizerAsGradientDescent(
117 numberOfIterations=300,
118 estimateLearningRate=R.EachIteration,
121 R.Execute(fixed, moving)
124 print(displacementTx)
125 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
126 print(f
" Iteration: {R.GetOptimizerIteration()}")
127 print(f
" Metric value: {R.GetMetricValue()}")
132 if "SITK_NOSHOW" not in os.environ:
133 sitk.Show(displacementTx.GetDisplacementField(),
"Displacement Field")
136 resampler.SetReferenceImage(fixed)
137 resampler.SetInterpolator(sitk.sitkLinear)
138 resampler.SetDefaultPixelValue(100)
139 resampler.SetTransform(compositeTx)
141 out = resampler.Execute(moving)
144 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
145 sitk.Show(cimg,
"ImageRegistration1 Composition")