Error while reading an image with ITK Python

Dear All,

I am starting using itk with python, so it might sound silly, but I have the following problem.
I have installed itk using command:
python -m pip install itk

The packages have been successfully installed.
I tried the provided example:

import itk

input_filename = ‘/path_to_my_nifti file/.nii’
output_filename = '/path_to_another_nifti file/
.nii’

image = itk.imread(input_filename)
median = itk.MedianImageFilter.New(image, Radius = 2)
itk.imwrite(median, output_filename)

And I am getting such an error:

File “itk_testing.py”, line 11, in
image = itk.imread(input_filename)
File “/home/adam/anaconda3/envs/itk_test_27/lib/python2.7/site-packages/itkExtras.py”, line 449, in imread
reader = itk.ImageFileReader.New(FileName=fileName)
File “/home/adam/anaconda3/envs/itk_test_27/lib/python2.7/site-packages/itkTemplate.py”, line 383, in New
return self._NewImageFileReader(*args, **kwargs)
File “/home/adam/anaconda3/envs/itk_test_27/lib/python2.7/site-packages/itkTemplate.py”, line 433, in _NewImageFileReader
ImageType = itk.Image[PixelType, dimension]
File “/home/adam/anaconda3/envs/itk_test_27/lib/python2.7/site-packages/itkTemplate.py”, line 274, in getitem
(str(parameters), self.name))
KeyError: ‘itkTemplate : No template (, 3) for the itk::Image class’

I tried it in virtual envs with both with python 2.7 and python 3.6.
I do not know if I understood correctly that I do not need to build ITK, and it is enough to pip install the libraries?
I do not have problems with using SimpleITK with python, but it does not have all the libraries that I need to use.

Could you please help me with that?

Cheers,
Adam

This is a surpising error. It looks like it doesn’t find any pixel type (since the template used is (,3).

Do you know the pixel type of your image? If so, could you try reading it with:

reader=itk.ImageFileReader[YourPixelType,3].New(input_filename)
reader.Update()
image = reader.GetOutput()

If this does not work, can you try to run the following snippet of code and post the result here:

imageIO = itk.ImageIOFactory.CreateImageIO( input_filename, itk.ImageIOFactory.ReadMode )
if not imageIO:
    raise RuntimeError("No ImageIO is registered to handle the given file.")
imageIO.SetFileName( input_filename )
imageIO.ReadImageInformation()
dimension = imageIO.GetNumberOfDimensions()
componentAsString = imageIO.GetComponentTypeAsString(imageIO.GetComponentType())
pixel = imageIO.GetPixelTypeAsString(imageIO.GetPixelType())
print("Dimension: %d; Component: %s; PixelType: %s"%(dimension, componentAsString, pixel))
2 Likes

HI Francois,

Thank you so much for getting back to me.

I have checked the pixel type using sitk and it is 32-bit signed integer.

image=sitk.ReadImage(input_filename)
print(image.GetPixelIDTypeAsString())

image=itk.ImageFileReader[itk.SI,3].New(input_filename)
median = itk.MedianImageFilter.New(image, Radius =2)
itk.imwrite(median, output_filename)

gives me an error:

Traceback (most recent call last):
File “/home/adam/anaconda3/envs/lung_segment_36/lib/python3.6/site-packages/itkTemplate.py”, line 266, in getitem
return(self.template[tuple(cleanParameters)])
KeyError: (, 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/adam/anaconda3/envs/lung_segment_36/lib/python3.6/site-packages/itkTemplate.py”, line 270, in getitem
return(self.template[tuple(cleanParameters)])
KeyError: (, 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/adam/PycharmProjects/untitled/itk_testing.py”, line 13, in
reader = itk.ImageFileReader[ itk.SI, 3].New(input_filename)
File “/home/adam/anaconda3/envs/lung_segment_36/lib/python3.6/site-packages/itkTemplate.py”, line 274, in getitem
(str(parameters), self.name))
KeyError: ‘itkTemplate : No template (, 3) for the itk::ImageFileReader class’

When I executed the code which you have kindly suggested, I got results:

Dimension: 3; Component: int; PixelType: scalar

When I replaced itk.SI with imageIO.GetPixelType() in the first code, after executing the later bit, I got the following error:

Traceback (most recent call last):
File “/home/adam/anaconda3/envs/lung_segment_36/lib/python3.6/site-packages/itkTemplate.py”, line 266, in getitem
return(self.template[tuple(cleanParameters)])
KeyError: (1, 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/adam/anaconda3/envs/lung_segment_36/lib/python3.6/site-packages/itkTemplate.py”, line 270, in getitem
return(self.template[tuple(cleanParameters)])
KeyError: (1, 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/adam/PycharmProjects/untitled/itk_testing.py”, line 26, in
reader = itk.ImageFileReader[ imageIO.GetPixelType(), 3].New(input_filename)
File “/home/adam/anaconda3/envs/lung_segment_36/lib/python3.6/site-packages/itkTemplate.py”, line 274, in getitem
(str(parameters), self.name))
KeyError: ‘itkTemplate : No template (1, 3) for the itk::ImageFileReader class’

Thanks for your help. DO you have any further ideas?

Cheers,
Adam

Thanks for trying everything and reporting the error messages. That is really helpful to understand what is happening.
ITK does not wrap the type “Signed integer”. So when you try to read your image and it cannot find the corresponding image type, it fails and gives you this weird error message. The error message is definitely something we are planning on improving as the current one is not that helpful.

Anyway, the way to work around this problem is to open your image with a different component type. I would recommend float if you do not want to lose any precision. You can specify the pixel type you want to use with itk.imread(input_filename, ITK.F).

Hope this helps.
Note: If your image maximum and minimum values fit in an image with components that are “short integer”, I would recommend actually using that pixel type instead of float (itk.SS or itk.USdepending if you want to use thesignedorunsigned` version).

3 Likes

HI Francois,

That is the solution! Thank you so much for your help!
Now it works and I can continue exploring itk with python, thanks to you!

Best wishes,
Adam

1 Like