20 import SimpleITK
as sitk
25 print(
"Usage:", sys.argv[0],
"<fixedImageFilter> <movingImageFile>",
26 "<outputTransformFile> <numberOfBins> <samplingPercentage>")
30 def command_iteration(method):
31 print(f
"{method.GetOptimizerIteration():3} = {method.GetMetricValue():10.5f} : {method.GetOptimizerPosition()}")
38 samplingPercentage = 0.10
41 numberOfBins = int(sys.argv[4])
43 samplingPercentage = float(sys.argv[5])
46 R.SetMetricAsMattesMutualInformation(numberOfBins)
47 R.SetMetricSamplingPercentage(samplingPercentage, sitk.sitkWallClock)
48 R.SetMetricSamplingStrategy(R.RANDOM)
49 R.SetOptimizerAsRegularStepGradientDescent(1.0, .001, 200)
51 R.SetInterpolator(sitk.sitkLinear)
53 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
55 outTx = R.Execute(fixed, moving)
59 print(f
"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
60 print(f
" Iteration: {R.GetOptimizerIteration()}")
61 print(f
" Metric value: {R.GetMetricValue()}")
65 if (
"SITK_NOSHOW" not in os.environ):
67 resampler.SetReferenceImage(fixed)
68 resampler.SetInterpolator(sitk.sitkLinear)
69 resampler.SetDefaultPixelValue(100)
70 resampler.SetTransform(outTx)
72 out = resampler.Execute(moving)
75 cimg =
sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
76 sitk.Show(cimg,
"ImageRegistration4 Composition")