20 import SimpleITK
as sitk
28 "<fixedImageFilter> <movingImageFile>",
29 "<outputTransformFile> <numberOfBins> <samplingPercentage>",
34 def command_iteration(method):
36 f
"{method.GetOptimizerIteration():3} "
37 + f
"= {method.GetMetricValue():10.5f} "
38 + f
": {method.GetOptimizerPosition()}"
46 samplingPercentage = 0.10
49 numberOfBins = int(sys.argv[4])
51 samplingPercentage = float(sys.argv[5])
54 R.SetMetricAsMattesMutualInformation(numberOfBins)
55 R.SetMetricSamplingPercentage(samplingPercentage, sitk.sitkWallClock)
56 R.SetMetricSamplingStrategy(R.RANDOM)
57 R.SetOptimizerAsRegularStepGradientDescent(1.0, 0.001, 200)
59 R.SetInterpolator(sitk.sitkLinear)
61 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
63 outTx = R.Execute(fixed, moving)
67 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
68 print(f
" Iteration: {R.GetOptimizerIteration()}")
69 print(f
" Metric value: {R.GetMetricValue()}")
73 if "SITK_NOSHOW" not in os.environ:
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)
84 sitk.Show(cimg,
"ImageRegistration4 Composition")