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()
62 R.SetOptimizerAsGradientDescent(learningRate=1.0,
63 numberOfIterations=100,
64 estimateLearningRate = R.EachIteration)
65 R.SetOptimizerScalesFromPhysicalShift()
67 R.SetInitialTransform(initialTx,inPlace=
True)
69 R.SetInterpolator(sitk.sitkLinear)
71 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R) )
72 R.AddCommand( sitk.sitkMultiResolutionIterationEvent,
lambda: command_multiresolution_iteration(R) )
74 outTx = R.Execute(fixed, moving)
79 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
80 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
81 print(
" Metric value: {0}".format(R.GetMetricValue()))
84 displacementField =
sitk.Image(fixed.GetSize(), sitk.sitkVectorFloat64)
85 displacementField.CopyInformation(fixed)
88 displacementTx.SetSmoothingGaussianOnUpdate(varianceForUpdateField=0.0,
89 varianceForTotalField=1.5)
93 R.SetMovingInitialTransform(outTx)
94 R.SetInitialTransform(displacementTx, inPlace=
True)
96 R.SetMetricAsANTSNeighborhoodCorrelation(4)
97 R.MetricUseFixedImageGradientFilterOff()
99 R.SetShrinkFactorsPerLevel([3,2,1])
100 R.SetSmoothingSigmasPerLevel([2,1,1])
102 R.SetOptimizerScalesFromPhysicalShift()
103 R.SetOptimizerAsGradientDescent(learningRate=1,
104 numberOfIterations=300,
105 estimateLearningRate=R.EachIteration)
107 outTx.AddTransform( R.Execute(fixed, moving) )
112 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
113 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
114 print(
" Metric value: {0}".format(R.GetMetricValue()))
119 if (
not "SITK_NOSHOW" in os.environ ):
121 sitk.Show(displacementTx.GetDisplacementField(),
"Displacement Field")
124 resampler.SetReferenceImage(fixed);
125 resampler.SetInterpolator(sitk.sitkLinear)
126 resampler.SetDefaultPixelValue(100)
127 resampler.SetTransform(outTx)
129 out = resampler.Execute(moving)
133 sitk.Show( cimg,
"ImageRegistration1 Composition" )