InsightSoftwareConsortium / ITKElastix

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

Need Help: ITK ERROR: ElastixRegistrationMethod(00000187F232D140) #141

Open yunfei920406 opened 2 years ago

yunfei920406 commented 2 years ago

Dear Developers: I am a Ph.D. candidate from Fudan University, China. Thanks for your kind providing us with the good package: ITK-Elastix, which is very helpful for medical registration. However, I encountered some questions after the installation. My programing environment is Anaconda+Python 3.7.12. After successful installation via the command:

"pip install itk-elastix", I met some errors when I tried to register the images via the following codes:

# Import Packages
import numpy as np
import matplotlib.pyplot as plt
import itk
b0_path = r"C:\Users\yunfe\Desktop\Image Registration\b0.mha"
b800_path = r"C:\Users\yunfe\Desktop\Image Registration\b800.mha"

# Load images with itk floats (itk.F). Necessary for elastix
fixed_image = itk.imread(b0_path, itk.F)
moving_image = itk.imread(b800_path, itk.F)
parameter_object = itk.ParameterObject.New()
default_rigid_parameter_map = parameter_object.GetDefaultParameterMap('rigid')
parameter_object.AddParameterMap(default_rigid_parameter_map)
# Call registration function
result_image, result_transform_parameters = itk.elastix_registration_method(
    fixed_image, moving_image,
    parameter_object=parameter_object,
    log_to_console=False)_

The errors are as follows:

image

When I tried with other codes:

# Load Elastix Image Filter Object
elastix_object = itk.ElastixRegistrationMethod.New(fixed_image, moving_image)
# elastix_object.SetFixedImage(fixed_image)
# elastix_object.SetMovingImage(moving_image)
elastix_object.SetParameterObject(parameter_object)

# Set additional options
elastix_object.SetLogToConsole(False)

# Update filter object (required)
elastix_object.UpdateLargestPossibleRegion()

# Results of Registration
result_image = elastix_object.GetOutput()
result_transform_parameters = elastix_object.GetTransformParameterObject()

Similar errors also appeared:

image

NHPatterson commented 2 years ago

The python traceback doesn't tell us your error. If you turn on elastix_object.SetLogToConsole(True )or SetLogToFileOn(True) you will get a more informative error message.

JSousa-UoL commented 2 years ago

I'm having the exact same issue. Even if I set the SetLogToConsole to True, it doesn't return any information. It just breaks at line where elastix_object.UpdateLargestPossibleRegion() and it just says what's in the screenshots above: Internal elastix error.

NHPatterson commented 2 years ago

If you are using a jupyter notebook, you may not be seeing everything that is printed to console. It may be better to run your code as a command line script by placing the python bits into a .py file and running from the terminal:

conda activate myenv
python mycode.py

Here is a reproducible example script creating a registration as a command line script that both prints to console and writes the console log to file:

if __name__ == "__main__":
    import itk
    import numpy as np

    im = np.random.randint(0, 255, (1024, 1024), dtype=np.uint8).astype(np.float32)

    im = itk.GetImageFromArray(im)

    itk.elastix_registration_method(im, im)

    parameter_object = itk.ParameterObject.New()
    default_rigid_parameter_map = parameter_object.GetDefaultParameterMap('rigid')
    parameter_object.AddParameterMap(default_rigid_parameter_map)

    result_image, result_transform_parameters = itk.elastix_registration_method(
        im, im,
        parameter_object=parameter_object,
        log_to_console=True,
        output_directory="/Users/nhp/Documents",
        log_to_file=True)

If you add log_to_file=True and output_dir=/my/cool/dir (changing to a directory that is easy for you to navigate to) to your registration, it will create a file called 'elastix.log' in that directory. This will contain what would normally be printed to console. Open it and scroll to the bottom and you may find a more informative error.

JSousa-UoL commented 2 years ago

Thank you so much for your comment @NHPatterson

My issue was exactly running the registration in the Jupyter Notebook and not having anything printed. Copied the content to a .py file and managed to have a detailed description of what was happening in the terminal (problems with the parameters).

Maybe a suggestion for the authors to write a quick comment in the example notebooks (maybe the first one) saying that setting console logging to True won't work in the Jupyter Notebooks.