20 """ A SimpleITK example demonstrating image registration using the
21 BSplineTransform and the ComplexCorrelation metric. """
25 import SimpleITK
as sitk
28 def command_iteration(method):
29 """ Callback invoked when the optimization has an iteration """
30 print(f
"{method.GetOptimizerIteration():3} " + f
"= {method.GetMetricValue():10.5f}")
34 """ A SimpleITK example demonstrating image registration using the
35 BSplineTransform and the ComplexCorrelation metric. """
40 "<fixedImageFilter> <movingImageFile>",
41 "<outputTransformFile>",
49 transformDomainMeshSize = [8] * moving.GetDimension()
52 print(
"Initial Parameters:")
53 print(tx.GetParameters())
56 R.SetMetricAsCorrelation()
58 R.SetOptimizerAsLBFGSB(
59 gradientConvergenceTolerance=1e-5,
60 numberOfIterations=100,
61 maximumNumberOfCorrections=5,
62 maximumNumberOfFunctionEvaluations=1000,
63 costFunctionConvergenceFactor=1e7,
65 R.SetInitialTransform(tx,
True)
66 R.SetInterpolator(sitk.sitkLinear)
68 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
70 outTx = R.Execute(fixed, moving)
74 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
75 print(f
" Iteration: {R.GetOptimizerIteration()}")
76 print(f
" Metric value: {R.GetMetricValue()}")
81 resampler.SetReferenceImage(fixed)
82 resampler.SetInterpolator(sitk.sitkLinear)
83 resampler.SetDefaultPixelValue(100)
84 resampler.SetTransform(outTx)
86 out = resampler.Execute(moving)
89 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
91 return_images = {
"fixed": fixed,
"moving": moving,
"composition": cimg}
95 if __name__ ==
"__main__":
96 return_dict = main(sys.argv)
97 if "SITK_NOSHOW" not in os.environ:
99 return_dict[
"composition"],
"ImageRegistrationMethodBSpline1 Composition"