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}".format(method.GetOptimizerLearningRate()))
34 if (method.GetOptimizerConvergenceValue() != sys.float_info.max):
35 print(
"\tConvergence Value: {0:.5e}".format(method.GetOptimizerConvergenceValue()))
38 def command_multiresolution_iteration(method):
39 print(
"\tStop Condition: {0}".format(method.GetOptimizerStopConditionDescription()))
40 print(
"============= Resolution Change =============")
43 if len ( sys.argv ) < 4:
44 print(
"Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
56 R.SetShrinkFactorsPerLevel([3,2,1])
57 R.SetSmoothingSigmasPerLevel([2,1,1])
59 R.SetMetricAsJointHistogramMutualInformation(20)
60 R.MetricUseFixedImageGradientFilterOff()
61 R.MetricUseFixedImageGradientFilterOff()
64 R.SetOptimizerAsGradientDescent(learningRate=1.0,
65 numberOfIterations=100,
66 estimateLearningRate = R.EachIteration)
67 R.SetOptimizerScalesFromPhysicalShift()
69 R.SetInitialTransform(initialTx,inPlace=
True)
71 R.SetInterpolator(sitk.sitkLinear)
73 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R) )
74 R.AddCommand( sitk.sitkMultiResolutionIterationEvent,
lambda: command_multiresolution_iteration(R) )
76 outTx = R.Execute(fixed, moving)
81 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
82 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
83 print(
" Metric value: {0}".format(R.GetMetricValue()))
86 displacementField =
sitk.Image(fixed.GetSize(), sitk.sitkVectorFloat64)
87 displacementField.CopyInformation(fixed)
90 displacementTx.SetSmoothingGaussianOnUpdate(varianceForUpdateField=0.0,
91 varianceForTotalField=1.5)
95 R.SetMovingInitialTransform(outTx)
96 R.SetInitialTransform(displacementTx, inPlace=
True)
98 R.SetMetricAsANTSNeighborhoodCorrelation(4)
99 R.MetricUseFixedImageGradientFilterOff()
100 R.MetricUseFixedImageGradientFilterOff()
103 R.SetShrinkFactorsPerLevel([3,2,1])
104 R.SetSmoothingSigmasPerLevel([2,1,1])
106 R.SetOptimizerScalesFromPhysicalShift()
107 R.SetOptimizerAsGradientDescent(learningRate=1,
108 numberOfIterations=300,
109 estimateLearningRate=R.EachIteration)
111 outTx.AddTransform( R.Execute(fixed, moving) )
116 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
117 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
118 print(
" Metric value: {0}".format(R.GetMetricValue()))
123 if (
not "SITK_NOSHOW" in os.environ ):
125 sitk.Show(displacementTx.GetDisplacementField(),
"Displacement Field")
128 resampler.SetReferenceImage(fixed);
129 resampler.SetInterpolator(sitk.sitkLinear)
130 resampler.SetDefaultPixelValue(100)
131 resampler.SetTransform(outTx)
133 out = resampler.Execute(moving)
137 sitk.Show( cimg,
"ImageRegistration1 Composition" )