def itk_registration_rigid_3d(fixed_image, moving_image, options): """ A Python implementation for a Rigid Body 3D registration, utilizing ITK (www.itk.org) library functions. :param fixed_image: The reference image. Must be an instance of sitk.Image class. :param moving_image: The image for which the spatial transform will be calculated. Same requirements as above. :param options: Options provided by the user via CLI or the included GUI. See image_quality_options.py. :return: The final transform as a sitk.Euler2DTransform """ print('Setting up registration job') assert isinstance(fixed_image, sitk.Image) assert isinstance(moving_image, sitk.Image) fixed_image = sitk.Cast(fixed_image, sitk.sitkFloat32) moving_image = sitk.Cast(moving_image, sitk.sitkFloat32) # REGISTRATION COMPONENTS SETUP # ======================================================================== registration = sitk.ImageRegistrationMethod() # OPTIMIZER registration.SetOptimizerAsRegularStepGradientDescent( options.learning_rate, options.min_step_length, options.registration_max_iterations, relaxationFactor=options.relaxation_factor, estimateLearningRate=registration.EachIteration) registration.SetOptimizerScalesFromJacobian() # translation_scale = 1.0/options.translation_scale # registration.SetOptimizerScales([1.0, translation_scale, translation_scale]) # INTERPOLATOR registration.SetInterpolator(sitk.sitkLinear) # METRIC if options.registration_method == 'mattes': registration.SetMetricAsMattesMutualInformation( numberOfHistogramBins=options.mattes_histogram_bins) elif options.registration_method == 'correlation': registration.SetMetricAsCorrelation() elif options.registration_method == 'mean-squared-difference': registration.SetMetricAsMeanSquares() else: raise ValueError("Unknown metric: %s" % options.registration_method) registration.SetMetricSamplingStrategy(registration.RANDOM) registration.SetMetricSamplingPercentage(options.sampling_percentage) if options.reg_translate_only: tx = sitk.TranslationTransform(3) else: tx = sitk.Euler3DTransform() transform = sitk.CenteredTransformInitializer( fixed_image, moving_image, tx, sitk.CenteredTransformInitializerFilter.MOMENTS) registration.SetInitialTransform(transform) tx.SetCenter(ops_itk.calculate_center_of_image(moving_image)) registration.SetInitialTransform(tx) if options.reg_enable_observers: # OBSERVERS registration.AddCommand(sitk.sitkStartEvent, start_plot) registration.AddCommand(sitk.sitkIterationEvent, lambda: plot_values(registration)) # START # ======================================================================== print("Starting registration") final_transform = registration.Execute(fixed_image, moving_image) print(('Final metric value: {0}'.format(registration.GetMetricValue()))) print(('Optimizer\'s stopping condition, {0}'.format( registration.GetOptimizerStopConditionDescription()))) if options.reg_enable_observers: end_plot(fixed_image, moving_image, final_transform) return final_transform
def itk_registration_similarity_2d(fixed_image, moving_image, options): """ A Python implementation for a Rigid Body 2D registration, utilizing ITK (www.itk.org) library functions. :param fixed_image: The reference image. Must be an instance of sitk.Image class. :param moving_image: The image that is to be registered. Must be an instance of sitk.Image class. The image for which the spatial transform will be calculated. Same requirements as above. :param options: Options provided by the user via CLI :return: The final transform as a sitk.Similarity2DTransform """ print('Setting up registration job') assert isinstance(fixed_image, sitk.Image) assert isinstance(moving_image, sitk.Image) fixed_image = sitk.Cast(fixed_image, sitk.sitkFloat32) moving_image = sitk.Cast(moving_image, sitk.sitkFloat32) # REGISTRATION COMPONENTS SETUP # ======================================================================== registration = sitk.ImageRegistrationMethod() # OPTIMIZER registration.SetOptimizerAsRegularStepGradientDescent( options.max_step_length, options.min_step_length, options.registration_max_iterations, relaxationFactor=options.relaxation_factor) translation_scale = 1.0 / options.translation_scale scaling_scale = 1.0 / options.scaling_scale registration.SetOptimizerScales( [scaling_scale, 1.0, translation_scale, translation_scale]) # INTERPOLATOR registration.SetInterpolator(sitk.sitkLinear) # METRIC if options.registration_method == 'mattes': registration.SetMetricAsMattesMutualInformation( numberOfHistogramBins=options.mattes_histogram_bins) registration.SetMetricSamplingStrategy(registration.RANDOM) registration.SetMetricSamplingPercentage( options.mattes_sampling_percentage) elif options.registration_method == 'correlation': registration.SetMetricAsCorrelation() elif options.registration_method == 'mean-squared-difference': registration.SetMetricAsMeanSquares() else: raise ValueError("Unknown metric: %s" % options.registration_method) print('Calculating initial registration parameters') tx = sitk.Similarity2DTransform() tx.SetAngle(options.set_rotation) tx.SetScale(options.set_scale) if options.initializer: transform = sitk.CenteredTransformInitializer( fixed_image, moving_image, tx, sitk.CenteredTransformInitializerFilter.GEOMETRY) registration.SetInitialTransform(transform) else: tx.SetTranslation([options.y_offset, options.x_offset]) tx.SetCenter(ops_itk.calculate_center_of_image(moving_image)) registration.SetInitialTransform(tx) # OBSERVERS registration.AddCommand(sitk.sitkStartEvent, start_plot) #registration.AddCommand(sitk.sitkEndEvent, end_plot) registration.AddCommand(sitk.sitkIterationEvent, lambda: plot_values(registration)) # START # ======================================================================== print("Starting registration") final_transform = registration.Execute(fixed_image, moving_image) print(('Final metric value: {0}'.format(registration.GetMetricValue()))) print(('Optimizer\'s stopping condition, {0}'.format( registration.GetOptimizerStopConditionDescription()))) end_plot(fixed_image, moving_image, final_transform) return final_transform