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)
74 def iteration_update():
75 metric_value = optimizer.GetValue()
76 current_parameters = optimizer.GetCurrentPosition()
77 parameter_list = [current_parameters[i]
for i
in range(len(current_parameters))]
78 print(f
"Metric: {metric_value:.8g} \tParameters: {parameter_list}")
81 iteration_command = itk.PyCommand.New()
82 iteration_command.SetCommandCallable(iteration_update)
83 optimizer.AddObserver(itk.IterationEvent(), iteration_command)
89 fixed_image=fixed_image,
90 moving_image=moving_image,
92 metric=matching_metric,
93 initial_transform=initial_transform,
95 registration.SetNumberOfLevels(1)
96 registration.SetSmoothingSigmasPerLevel([0])
97 registration.SetShrinkFactorsPerLevel([1])
102 registration.Update()
108 final_parameters = registration.GetOutput().Get().GetParameters()
110 print(
"\nFinal Registration Parameters: ")
111 print(f
"Translation X = {final_parameters[0]:f}")
112 print(f
"Translation Y = {final_parameters[1]:f}")
119 resampled_moving = itk.resample_image_filter(
121 transform=registration.GetTransform(),
122 use_reference_image=
True,
123 reference_image=fixed_image,
124 default_pixel_value=100,
130 OutputPixelType = itk.ctype(
"unsigned char")
131 resampled_cast = resampled_moving.astype(OutputPixelType)
136 itk.imwrite(resampled_cast, output_image_file)