20 import SimpleITK
as sitk
25 def command_iteration(method):
26 print(f
"{method.GetOptimizerIteration():3} " + f
"= {method.GetMetricValue():10.5f}")
34 "<fixedImageFilter> <movingImageFile>",
35 "<outputTransformFile>",
43 transformDomainMeshSize = [8] * moving.GetDimension()
46 print(
"Initial Parameters:")
47 print(tx.GetParameters())
50 R.SetMetricAsCorrelation()
52 R.SetOptimizerAsLBFGSB(
53 gradientConvergenceTolerance=1e-5,
54 numberOfIterations=100,
55 maximumNumberOfCorrections=5,
56 maximumNumberOfFunctionEvaluations=1000,
57 costFunctionConvergenceFactor=1e7,
59 R.SetInitialTransform(tx,
True)
60 R.SetInterpolator(sitk.sitkLinear)
62 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
64 outTx = R.Execute(fixed, moving)
68 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
69 print(f
" Iteration: {R.GetOptimizerIteration()}")
70 print(f
" Metric value: {R.GetMetricValue()}")
75 resampler.SetReferenceImage(fixed)
76 resampler.SetInterpolator(sitk.sitkLinear)
77 resampler.SetDefaultPixelValue(100)
78 resampler.SetTransform(outTx)
80 out = resampler.Execute(moving)
83 cimg =
sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
85 return_images = {
"fixed": fixed,
"moving": moving,
"composition": cimg}
89 if __name__ ==
"__main__":
90 return_dict = main(sys.argv)
91 if "SITK_NOSHOW" not in os.environ:
93 return_dict[
"composition"],
"ImageRegistrationMethodBSpline1 Composition"