20 from __future__
import print_function
22 import SimpleITK
as sitk
27 def command_iteration(method):
28 print(
"{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(),
29 method.GetMetricValue()))
30 print(
"\t#: ", len(method.GetOptimizerPosition()))
33 def command_multi_iteration(method):
34 print(
"--------- Resolution Changing ---------")
38 print(
"Usage:", sys.argv[0],
"<fixedImageFilter> <movingImageFile>",
39 "<outputTransformFile>")
46 transformDomainMeshSize = [10] * moving.GetDimension()
48 transformDomainMeshSize)
50 print(
"Initial Parameters:")
51 print(tx.GetParameters())
54 R.SetMetricAsMattesMutualInformation(50)
55 R.SetOptimizerAsGradientDescentLineSearch(5.0, 100,
56 convergenceMinimumValue=1e-4,
57 convergenceWindowSize=5)
58 R.SetOptimizerScalesFromPhysicalShift()
59 R.SetInitialTransform(tx)
60 R.SetInterpolator(sitk.sitkLinear)
62 R.SetShrinkFactorsPerLevel([6, 2, 1])
63 R.SetSmoothingSigmasPerLevel([6, 2, 1])
65 R.AddCommand(sitk.sitkIterationEvent,
lambda: command_iteration(R))
66 R.AddCommand(sitk.sitkMultiResolutionIterationEvent,
67 lambda: command_multi_iteration(R))
69 outTx = R.Execute(fixed, moving)
73 print(
"Optimizer stop condition: {0}"
74 .format(R.GetOptimizerStopConditionDescription()))
75 print(
" Iteration: {0}".format(R.GetOptimizerIteration()))
76 print(
" Metric value: {0}".format(R.GetMetricValue()))
80 if (
"SITK_NOSHOW" not in os.environ):
82 resampler.SetReferenceImage(fixed)
83 resampler.SetInterpolator(sitk.sitkLinear)
84 resampler.SetDefaultPixelValue(100)
85 resampler.SetTransform(outTx)
87 out = resampler.Execute(moving)
90 cimg =
sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
91 sitk.Show(cimg,
"ImageRegistration1 Composition")