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)
35 print(
"{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(),
36 method.GetMetricValue()))
40 def command_multi_iteration(method) :
44 if R.GetCurrentLevel() > 0:
45 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
46 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
47 print(
" Metric value: {0}".format(R.GetMetricValue()))
49 print(
"--------- Resolution Changing ---------")
52 if len ( sys.argv ) < 4:
53 print(
"Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
61 transformDomainMeshSize=[2]*fixed.GetDimension()
63 transformDomainMeshSize )
65 print(
"Initial Number of Parameters: {0}".format(tx.GetNumberOfParameters()))
68 R.SetMetricAsJointHistogramMutualInformation()
70 R.SetOptimizerAsGradientDescentLineSearch(5.0,
72 convergenceMinimumValue=1e-4,
73 convergenceWindowSize=5)
75 R.SetInterpolator(sitk.sitkLinear)
77 R.SetInitialTransformAsBSpline(tx,
80 R.SetShrinkFactorsPerLevel([4,2,1])
81 R.SetSmoothingSigmasPerLevel([4,2,1])
83 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R, tx) )
84 R.AddCommand( sitk.sitkMultiResolutionIterationEvent,
lambda: command_multi_iteration(R) )
86 outTx = R.Execute(fixed, moving)
91 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
92 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
93 print(
" Metric value: {0}".format(R.GetMetricValue()))
97 if (
not "SITK_NOSHOW" 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)
109 sitk.Show( cimg,
"Image Registration Composition" )