20 import SimpleITK
as sitk
25 def command_iteration(method):
26 print(f
"{method.GetOptimizerIteration():3} = {method.GetMetricValue():10.5f}")
30 print(
"Usage:", sys.argv[0],
"<fixedImageFilter> <movingImageFile>",
31 "<outputTransformFile>")
38 transformDomainMeshSize = [8] * moving.GetDimension()
40 transformDomainMeshSize)
42 print(
"Initial Parameters:")
43 print(tx.GetParameters())
46 R.SetMetricAsCorrelation()
48 R.SetOptimizerAsLBFGSB(gradientConvergenceTolerance=1e-5,
49 numberOfIterations=100,
50 maximumNumberOfCorrections=5,
51 maximumNumberOfFunctionEvaluations=1000,
52 costFunctionConvergenceFactor=1e+7)
53 R.SetInitialTransform(tx,
True)
54 R.SetInterpolator(sitk.sitkLinear)
56 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
58 outTx = R.Execute(fixed, moving)
62 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
63 print(f
" Iteration: {R.GetOptimizerIteration()}")
64 print(f
" Metric value: {R.GetMetricValue()}")
68 if (
"SITK_NOSHOW" not in os.environ):
70 resampler.SetReferenceImage(fixed)
71 resampler.SetInterpolator(sitk.sitkLinear)
72 resampler.SetDefaultPixelValue(100)
73 resampler.SetTransform(outTx)
75 out = resampler.Execute(moving)
78 cimg =
sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
79 sitk.Show(cimg,
"ImageRegistration1 Composition")