20 from __future__
import print_function
22 import SimpleITK
as sitk
27 def command_iteration(method) :
28 print(
"{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(),
29 method.GetMetricValue()))
30 print(
"\t#: ", len(method.GetOptimizerPosition()))
33 def command_multi_iteration(method) :
34 print(
"--------- Resolution Changing ---------")
37 if len ( sys.argv ) < 4:
38 print(
"Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
46 transformDomainMeshSize=[10]*moving.GetDimension()
48 transformDomainMeshSize )
50 print(
"Initial Parameters:");
51 print(tx.GetParameters())
54 R.SetMetricAsMattesMutualInformation(50)
55 R.SetOptimizerAsGradientDescentLineSearch(5.0, 100,
56 convergenceMinimumValue=1e-4,
57 convergenceWindowSize=5)
58 R.SetOptimizerScalesFromPhysicalShift( )
59 R.SetInitialTransform(tx)
60 R.SetInterpolator(sitk.sitkLinear)
62 R.SetShrinkFactorsPerLevel([6,2,1])
63 R.SetSmoothingSigmasPerLevel([6,2,1])
65 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R) )
66 R.AddCommand( sitk.sitkMultiResolutionIterationEvent,
lambda: command_multi_iteration(R) )
68 outTx = R.Execute(fixed, moving)
72 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
73 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
74 print(
" Metric value: {0}".format(R.GetMetricValue()))
78 if (
not "SITK_NOSHOW" in os.environ ):
81 resampler.SetReferenceImage(fixed);
82 resampler.SetInterpolator(sitk.sitkLinear)
83 resampler.SetDefaultPixelValue(100)
84 resampler.SetTransform(outTx)
86 out = resampler.Execute(moving)
90 sitk.Show( cimg,
"ImageRegistration1 Composition" )