InsightSoftwareConsortium / ITKElastix

An ITK Python interface to elastix, a toolbox for rigid and nonrigid registration of images
Apache License 2.0
210 stars 23 forks source link

Error when using GradientDifference or NormalizedGradientCorrelation Metric #140

Open phflot opened 2 years ago

phflot commented 2 years ago

I am trying to use the GradientDifference and NormalizedGradientCorrelation metrics but could not quite figure out how to prepare the input. I am following the ITK_Example07_MultimetricMultiImageRegistration example and use a parameters_Bspline_Multimetric.txt where I have only changed the metric. When I am using the NormalizedGradientCorrelation metric alone with 2D float32 images in the same way as in the example, I get the error

File: _deps/elx-src/Components/Metrics/NormalizedGradientCorrelation/elxNormalizedGradientCorrelationMetric.hxx
Line: 55
Description: ITK ERROR: NormalizedGradientCorrelationMetric(0x555fb32f4fd0): FixedImage must be 3D

So I figured that it probably needs the two gradient channels as input. When I run the following code

fixed_grad = itk.GradientImageFilter(fixed)
moving_grad = itk.GradientImageFilter(moving)

elastix_object = itk.ElastixRegistrationMethod.New(fixed_grad, moving_grad,
                                                   log_to_console=True,
                                                   number_of_threads=12)

it throws itk.support.extras.TemplateTypeError: itk.ElastixRegistrationMethod is not wrapped for input type None. The images from the gradient filter have vector pixel type so, combining the two error messages, the next thing I have tried is to convert the gradient filter to 3D float images. But then I end up with the following error:

File: _deps/elx-src/Components/Metrics/NormalizedGradientCorrelation/elxNormalizedGradientCorrelationMetric.hxx
Line: 61
Description: ITK ERROR: NormalizedGradientCorrelationMetric(0x5649875b3f90): Metric can only be used for 2D-3D registration. FixedImageSize[2] must be 1

Do you have any idea what I am missing or could point me to resources that I could check out? Ideally, could you consider adding an example for the gradient-based metrics? Thanks and best wishes, Philipp

thewtex commented 2 years ago

Hello Philipp,

To compute the image gradients, instead of:

fixed_grad = itk.GradientImageFilter(fixed)

use:

fixed_grad = itk.gradient_image_filter(fixed)

However, this will produce an image of covariant vectors. You may have intended,

fixed_grad = itk.gradient_magnitude_image_filter(fixed)

or

fixed_grad = itk.gradient_magnitude_recursive_gaussian_image_filter(fixed, sigma=2.0)

(less noisy)