20 from __future__
import print_function
22 import SimpleITK
as sitk
27 def command_iteration(method):
28 if (method.GetOptimizerIteration() == 0):
29 print(
"Estimated Scales: ", method.GetOptimizerScales())
30 print(
"{0:3} = {1:7.5f} : {2}".format(method.GetOptimizerIteration(),
31 method.GetMetricValue(),
32 method.GetOptimizerPosition()))
36 print(
"Usage:", sys.argv[0],
37 "<fixedImageFilter> <movingImageFile> <outputTransformFile>")
40 pixelType = sitk.sitkFloat32
48 R.SetMetricAsCorrelation()
50 R.SetOptimizerAsRegularStepGradientDescent(learningRate=2.0,
52 numberOfIterations=500,
53 gradientMagnitudeTolerance=1e-8)
54 R.SetOptimizerScalesFromIndexShift()
58 R.SetInitialTransform(tx)
60 R.SetInterpolator(sitk.sitkLinear)
62 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
64 outTx = R.Execute(fixed, moving)
68 print(
"Optimizer stop condition: {0}"
69 .format(R.GetOptimizerStopConditionDescription()))
70 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
71 print(
" Metric value: {0}".format(R.GetMetricValue()))
75 if (
"SITK_NOSHOW" not in os.environ):
77 resampler.SetReferenceImage(fixed)
78 resampler.SetInterpolator(sitk.sitkLinear)
79 resampler.SetDefaultPixelValue(1)
80 resampler.SetTransform(outTx)
82 out = resampler.Execute(moving)
86 cimg =
sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
87 sitk.Show(cimg,
"ImageRegistration2 Composition")