20 from __future__
import print_function
21 from functools
import reduce
24 import SimpleITK
as sitk
29 def command_iteration(method) :
30 if (method.GetOptimizerIteration()==0):
31 print(
"Estimated Scales: ", method.GetOptimizerScales())
32 print(
"{0:3} = {1:7.5f} : {2}".format(method.GetOptimizerIteration(),
33 method.GetMetricValue(),
34 method.GetOptimizerPosition()))
38 if len ( sys.argv ) < 4:
39 print(
"Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
42 pixelType = sitk.sitkFloat32
51 R.SetMetricAsCorrelation()
53 R.SetOptimizerAsRegularStepGradientDescent(learningRate=2.0,
55 numberOfIterations=500,
56 gradientMagnitudeTolerance=1e-8 )
57 R.SetOptimizerScalesFromIndexShift()
60 R.SetInitialTransform(tx)
62 R.SetInterpolator(sitk.sitkLinear)
64 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R) )
66 outTx = R.Execute(fixed, moving)
70 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
71 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
72 print(
" Metric value: {0}".format(R.GetMetricValue()))
77 if (
not "SITK_NOSHOW" in os.environ ):
80 resampler.SetReferenceImage(fixed);
81 resampler.SetInterpolator(sitk.sitkLinear)
82 resampler.SetDefaultPixelValue(1)
83 resampler.SetTransform(outTx)
85 out = resampler.Execute(moving)
90 sitk.Show( cimg,
"ImageRegistration2 Composition" )