32 raise Exception(f
"Usage: {argv[0]} fixed_image moving_image")
34 fixed_image = itk.imread(argv[1], itk.F)
35 moving_image = itk.imread(argv[2], itk.F)
37 dimension = fixed_image.GetImageDimension()
38 FixedImageType = type(fixed_image)
39 MovingImageType = type(moving_image)
44 raise Exception(f
"Unsupported dimension: {dimension}")
51 initializer = TransformInitializerType.New(
53 FixedImage=fixed_image,
54 MovingImage=moving_image,
56 initializer.InitializeTransform()
62 def print_iteration():
65 f
"{optimizer.GetCurrentIteration()}\t"
66 f
"{list(optimizer.GetCurrentIndex())} \t"
67 f
"{optimizer.GetCurrentValue():10.4f}\t"
68 f
"{list(optimizer.GetCurrentPosition())}\t"
71 optimizer.AddObserver(itk.IterationEvent(), print_iteration)
74 optimizer.SetNumberOfSteps([int(angles / 2), 0, 0])
77 scales = optimizer.GetScales()
79 scales.SetElement(0, 2.0 * pi / angles)
80 scales.SetElement(1, 1.0)
81 scales.SetElement(2, 1.0)
82 optimizer.SetScales(scales)
85 registration = RegistrationType.New(
88 FixedImage=fixed_image,
89 MovingImage=moving_image,
90 InitialTransform=transform,
98 f
"MinimumMetricValue: {optimizer.GetMinimumMetricValue():.4f}\t"
99 f
"MaximumMetricValue: {optimizer.GetMaximumMetricValue():.4f}\n"
100 f
"MinimumMetricValuePosition: {list(optimizer.GetMinimumMetricValuePosition())}\t"
101 f
"MaximumMetricValuePosition: {list(optimizer.GetMaximumMetricValuePosition())}\n"
102 f
"StopConditionDescription: {optimizer.GetStopConditionDescription()}\t"
105 except Exception
as e:
106 print(f
"Exception caught: {e}")
112 if __name__ ==
"__main__":