20 from __future__
import print_function
22 import SimpleITK
as sitk
27 if len ( sys.argv ) < 4:
28 print(
"Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile> <numberOfBins> <samplingPercentage>".format(sys.argv[0]))
31 def command_iteration(method) :
32 print(
"{0:3} = {1:10.5f} : {2}".format(method.GetOptimizerIteration(),
33 method.GetMetricValue(),
34 method.GetOptimizerPosition()))
42 samplingPercentage = 0.10
44 if len ( sys.argv ) > 4:
45 numberOfBins = int(sys.argv[4])
46 if len ( sys.argv ) > 5:
47 samplingPercentage = float(sys.argv[5])
50 R.SetMetricAsMattesMutualInformation(numberOfBins)
51 R.SetMetricSamplingPercentage(samplingPercentage,sitk.sitkWallClock)
52 R.SetMetricSamplingStrategy(R.RANDOM)
53 R.SetOptimizerAsRegularStepGradientDescent(1.0,.001,200)
55 R.SetInterpolator(sitk.sitkLinear)
57 R.AddCommand( sitk.sitkIterationEvent,
lambda: command_iteration(R) )
59 outTx = R.Execute(fixed, moving)
64 print(
"Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
65 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
66 print(
" Metric value: {0}".format(R.GetMetricValue()))
71 if (
not "SITK_NOSHOW" in os.environ ):
73 resampler.SetReferenceImage(fixed);
74 resampler.SetInterpolator(sitk.sitkLinear)
75 resampler.SetDefaultPixelValue(100)
76 resampler.SetTransform(outTx)
78 out = resampler.Execute(moving)
82 sitk.Show( cimg,
"ImageRegistration4 Composition" )