29 print(
"Missing Parameters")
31 "Usage: ImageRegistration3.py fixed_image_file moving_image_file output_image_file"
34 fixed_image_file = sys.argv[1]
35 moving_image_file = sys.argv[2]
36 output_image_file = sys.argv[3]
41 PixelType = itk.ctype(
"float")
47 fixed_image = itk.imread(fixed_image_file, PixelType)
48 moving_image = itk.imread(moving_image_file, PixelType)
49 Dimension = fixed_image.GetImageDimension()
60 type(fixed_image), type(moving_image)
67 optimizer.SetLearningRate(4)
68 optimizer.SetMinimumStepLength(0.001)
69 optimizer.SetRelaxationFactor(0.5)
70 optimizer.SetNumberOfIterations(100)
76 def iteration_update():
77 metric_value = optimizer.GetValue()
78 current_parameters = optimizer.GetCurrentPosition()
79 parameter_list = [current_parameters[i]
for i
in range(len(current_parameters))]
80 print(f
"Metric: {metric_value:.8g} \tParameters: {parameter_list}")
84 iteration_command.SetCommandCallable(iteration_update)
85 optimizer.AddObserver(itk.IterationEvent(), iteration_command)
91 fixed_image=fixed_image,
92 moving_image=moving_image,
94 metric=matching_metric,
95 initial_transform=initial_transform,
97 registration.SetNumberOfLevels(1)
98 registration.SetSmoothingSigmasPerLevel([0])
99 registration.SetShrinkFactorsPerLevel([1])
104 registration.Update()
110 final_parameters = registration.GetOutput().Get().GetParameters()
112 print(
"\nFinal Registration Parameters: ")
113 print(f
"Translation X = {final_parameters[0]:f}")
114 print(f
"Translation Y = {final_parameters[1]:f}")
121 resampled_moving = itk.resample_image_filter(
123 transform=registration.GetTransform(),
124 use_reference_image=
True,
125 reference_image=fixed_image,
126 default_pixel_value=100,
132 OutputPixelType = itk.ctype(
"unsigned char")
133 resampled_cast = resampled_moving.astype(OutputPixelType)
138 itk.imwrite(resampled_cast, output_image_file)