20 from __future__
import print_function
22 import SimpleITK
as sitk
27 def command_iteration(method):
28 if (method.GetOptimizerIteration() == 0):
29 print(
"\tLevel: {0}".format(method.GetCurrentLevel()))
30 print(
"\tScales: {0}".format(method.GetOptimizerScales()))
31 print(
"#{0}".format(method.GetOptimizerIteration()))
32 print(
"\tMetric Value: {0:10.5f}".format(method.GetMetricValue()))
33 print(
"\tLearningRate: {0:10.5f}"
34 .format(method.GetOptimizerLearningRate()))
35 if (method.GetOptimizerConvergenceValue() != sys.float_info.max):
36 print(
"\tConvergence Value: {0:.5e}"
37 .format(method.GetOptimizerConvergenceValue()))
40 def command_multiresolution_iteration(method):
41 print(
"\tStop Condition: {0}"
42 .format(method.GetOptimizerStopConditionDescription()))
43 print(
"============= Resolution Change =============")
47 print(
"Usage:", sys.argv[0],
"<fixedImageFilter> <movingImageFile>",
48 "<outputTransformFile>")
57 fixed.GetDimension()))
61 R.SetShrinkFactorsPerLevel([3, 2, 1])
62 R.SetSmoothingSigmasPerLevel([2, 1, 1])
64 R.SetMetricAsJointHistogramMutualInformation(20)
65 R.MetricUseFixedImageGradientFilterOff()
67 R.SetOptimizerAsGradientDescent(learningRate=1.0,
68 numberOfIterations=100,
69 estimateLearningRate=R.EachIteration)
70 R.SetOptimizerScalesFromPhysicalShift()
72 R.SetInitialTransform(initialTx)
74 R.SetInterpolator(sitk.sitkLinear)
76 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
77 R.AddCommand(sitk.sitkMultiResolutionIterationEvent,
78 lambda: command_multiresolution_iteration(R))
80 outTx1 = R.Execute(fixed, moving)
84 print(
"Optimizer stop condition: {0}"
85 .format(R.GetOptimizerStopConditionDescription()))
86 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
87 print(
" Metric value: {0}".format(R.GetMetricValue()))
89 displacementField =
sitk.Image(fixed.GetSize(), sitk.sitkVectorFloat64)
90 displacementField.CopyInformation(fixed)
93 displacementTx.SetSmoothingGaussianOnUpdate(varianceForUpdateField=0.0,
94 varianceForTotalField=1.5)
96 R.SetMovingInitialTransform(outTx1)
97 R.SetInitialTransform(displacementTx, inPlace=
True)
99 R.SetMetricAsANTSNeighborhoodCorrelation(4)
100 R.MetricUseFixedImageGradientFilterOff()
102 R.SetShrinkFactorsPerLevel([3, 2, 1])
103 R.SetSmoothingSigmasPerLevel([2, 1, 1])
105 R.SetOptimizerScalesFromPhysicalShift()
106 R.SetOptimizerAsGradientDescent(learningRate=1,
107 numberOfIterations=300,
108 estimateLearningRate=R.EachIteration)
110 R.Execute(fixed, moving)
113 print(displacementTx)
114 print(
"Optimizer stop condition: {0}"
115 .format(R.GetOptimizerStopConditionDescription()))
116 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
117 print(
" Metric value: {0}".format(R.GetMetricValue()))
122 if (
"SITK_NOSHOW" not in os.environ):
123 sitk.Show(displacementTx.GetDisplacementField(),
"Displacement Field")
126 resampler.SetReferenceImage(fixed)
127 resampler.SetInterpolator(sitk.sitkLinear)
128 resampler.SetDefaultPixelValue(100)
129 resampler.SetTransform(compositeTx)
131 out = resampler.Execute(moving)
134 cimg =
sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
135 sitk.Show(cimg,
"ImageRegistration1 Composition")