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()))
31 if len ( sys.argv ) < 4:
32 print(
"Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
40 transformDomainMeshSize=[8]*moving.GetDimension()
42 transformDomainMeshSize )
44 print(
"Initial Parameters:");
45 print(tx.GetParameters())
48 R.SetMetricAsCorrelation()
50 R.SetOptimizerAsLBFGSB(gradientConvergenceTolerance=1e-5,
51 numberOfIterations=100,
52 maximumNumberOfCorrections=5,
53 maximumNumberOfFunctionEvaluations=1000,
54 costFunctionConvergenceFactor=1e+7)
55 R.SetInitialTransform(tx,
True)
56 R.SetInterpolator(sitk.sitkLinear)
58 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R) )
60 outTx = R.Execute(fixed, moving)
64 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
65 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
66 print(
" Metric value: {0}".format(R.GetMetricValue()))
70 if (
not "SITK_NOSHOW" in os.environ ):
73 resampler.SetReferenceImage(fixed);
74 resampler.SetInterpolator(sitk.sitkLinear)
75 resampler.SetDefaultPixelValue(100)
76 resampler.SetTransform(outTx)
78 out = resampler.Execute(moving)
82 sitk.Show( cimg,
"ImageRegistration1 Composition" )