20 from __future__
import print_function
22 import SimpleITK
as sitk
27 def command_iteration(method, bspline_transform):
28 if method.GetOptimizerIteration() == 0:
32 print(bspline_transform)
34 print(
"{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(),
35 method.GetMetricValue()))
38 def command_multi_iteration(method):
42 if R.GetCurrentLevel() > 0:
43 print(
"Optimizer stop condition: {0}"
44 .format(R.GetOptimizerStopConditionDescription()))
45 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
46 print(
" Metric value: {0}".format(R.GetMetricValue()))
48 print(
"--------- Resolution Changing ---------")
52 print(
"Usage:", sys.argv[0],
"<fixedImageFilter> <movingImageFile>",
53 "<outputTransformFile>")
60 transformDomainMeshSize = [2] * fixed.GetDimension()
62 transformDomainMeshSize)
64 print(
"Initial Number of Parameters: {0}".format(tx.GetNumberOfParameters()))
67 R.SetMetricAsJointHistogramMutualInformation()
69 R.SetOptimizerAsGradientDescentLineSearch(5.0,
71 convergenceMinimumValue=1e-4,
72 convergenceWindowSize=5)
74 R.SetInterpolator(sitk.sitkLinear)
76 R.SetInitialTransformAsBSpline(tx,
78 scaleFactors=[1, 2, 5])
79 R.SetShrinkFactorsPerLevel([4, 2, 1])
80 R.SetSmoothingSigmasPerLevel([4, 2, 1])
82 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R, tx))
83 R.AddCommand(sitk.sitkMultiResolutionIterationEvent,
84 lambda: command_multi_iteration(R))
86 outTx = R.Execute(fixed, moving)
91 print(
"Optimizer stop condition: {0}"
92 .format(R.GetOptimizerStopConditionDescription()))
93 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
94 print(
" Metric value: {0}".format(R.GetMetricValue()))
98 if (
"SITK_NOSHOW" not in os.environ):
100 resampler.SetReferenceImage(fixed)
101 resampler.SetInterpolator(sitk.sitkLinear)
102 resampler.SetDefaultPixelValue(100)
103 resampler.SetTransform(outTx)
105 out = resampler.Execute(moving)
108 cimg =
sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
109 sitk.Show(cimg,
"Image Registration Composition")