SimpleITK
SimpleITK copied to clipboard
Python SimpleITK loads a 4th dimention (which is not there)
I'm writing a library for medical images, which rely heavily on SimpleITK. I've encountered problem that completely bugs me:
I have three images, which I run through the STAPLEImageFilter with something like:
f = sitk.STAPLEImageFilter()
f.SetForegroundValue(255) # Hardcoded to output of dcmrtstruct2nii
staple_img = f.Execute(sitk.VectorOfImage(list_of_images))
Afterwards I successfully perform a thresholding of like:
def binarize_image(image: sitk.Image, threshold=0.95) -> sitk.Image:
return image > threshold
But then I write staple_img to the harddisk with sitk.WriteImage and loads it again with sitk.ReadImage and attempt to threshold with the exact same function, I get:
Traceback (most recent call last):
File "/home/mathis/projects/repos/StapleStructs/main.py", line 26, in <module>
main()
File "/home/mathis/projects/repos/StapleStructs/main.py", line 21, in main
bulk_binarize_images(labelimages, "test_data/staple/binary")
File "/home/mathis/projects/repos/StapleStructs/staple/staple.py", line 41, in bulk_binarize_images
bin_img = binarize_image(image)
File "/home/mathis/projects/repos/StapleStructs/staple/staple.py", line 34, in binarize_image
return image > threshold
File "/home/mathis/anaconda3/envs/research_log/lib/python3.8/site-packages/SimpleITK/SimpleITK.py", line 3971, in __gt__
return Greater( self, float(other) )
File "/home/mathis/anaconda3/envs/research_log/lib/python3.8/site-packages/SimpleITK/SimpleITK.py", line 29551, in Greater
return _SimpleITK.Greater(*args)
RuntimeError: Exception thrown in SimpleITK Greater: /tmp/SimpleITK/Code/Common/include/sitkMemberFunctionFactory.hxx:155:
sitk::ERROR: Pixel type: 64-bit float is not supported in 4D by N3itk6simple18GreaterImageFilterE.
Process finished with exit code 1
Looking at the loaded image's dimentions, it turns out that a 4th dimension is loaded - from a log message: 08/29/2022 08:49:11 AM staple INFO: [ ] Binarizing BODY, (512, 512, 206, 1)
Furthermore, when I spin up a terminal with the same python interpreter and manually load the image with the same commands there is no problem:
In [2]: import SimpleITK as sitk
In [4]: img = sitk.ReadImage("test_data/staple/heatmap/BODY.nii.gz")
In [5]: img.GetSize()
Out[5]: (512, 512, 206)
In [6]: img > 0.95
Out[6]: <SimpleITK.SimpleITK.Image; proxy of <Swig Object of type 'std::vector< itk::simple::Image >::value_type *' at 0x7fcbb4090300>
I've tried both with python 3.8.1 and 3.10 with SimpleITK version 2.1.1.2.
I admit that I have no clue where this might occur.
Mathis
Hello @mathiser,
This is indeed strange behavior.
It may be associated with the nifti format (this ITK issue), though I expect to get the same result from code run as a script and the same code run in the Python interpreter, so possibly not.
First, see if it is due to nifti, save the result using another file format, e.g. mha, and see if that makes a difference (it shouldn't).
In any case, please share the image (BODY.nii.gz) and a minimal working script that recreates the issue. If the script is just the four lines you posted, then just share the image and we'll look into the issue.
As a work around you could do something like this:
if staple_img.GetDimension() == 4:
staple_img = staple_img[:,:,:,0]
Closed with the underlying issue in ITK.