Omar Kamal
Omar Kamal

Reputation: 55

sitk.LabelIntensityStatisticsImageFilter not working

I am trying to use sitk.LabelIntensityStatisticsImageFilter to calculate First order statistics for a label in an image.

My code is

import SimpleITK as sitk

ADC600 = '/Users/omarkamal/PycharmProjects/DWI/Pt1/ADC600.nii'
ADC600_r = sitk.ReadImage(ADC600)

label = '/Users/omarkamal/PycharmProjects/DWI/Pt1/seg.nii.gz'
label = sitk.ReadImage(label)

labelstatsFilter = sitk.LabelIntensityStatisticsImageFilter

labelstatsFilter.Execute(ADC600_r, label)
Mean = labelstatsFilter.GetMean(1)
Kurtosis = labelstatsFilter.GetKurtosis(1)

print(Mean, Kurtosis)

I get this error:

Traceback (most recent call last):
  File "/Users/omarkamal/PycharmProjects/DWI/Stats Extractor.py", line 21, in <module>
    labelstatsFilter.Execute(ADC600_r, label)
  File "/Users/omarkamal/anaconda3/envs/dicom/lib/python3.7/site-packages/SimpleITK/SimpleITK.py", line 41307, in Execute
    return _SimpleITK.LabelIntensityStatisticsImageFilter_Execute(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'LabelIntensityStatisticsImageFilter_Execute'.
  Possible C/C++ prototypes are:
    itk::simple::LabelIntensityStatisticsImageFilter::Execute(itk::simple::Image const &,itk::simple::Image const &)
    itk::simple::LabelIntensityStatisticsImageFilter::Execute(itk::simple::Image const &,itk::simple::Image const &,double,bool,bool,uint32_t)

Apparently this module is borrowed from itk in C/C++ ? How can I fix that?

Also I want to calculate entropy which I can't find in simpleITK. Any ideas?

Upvotes: 0

Views: 920

Answers (1)

NBStephens
NBStephens

Reputation: 378

SimpleITK is pretty great but, in my opinion, their error messages are not very helpful. Honestly, all you are missing is a () when you pass the filter to the labelstatFilter. I have only ever used this particular filter in conjunction with a connected components filter (e.g):

    cc = sitk.ConnectedComponent(img, True)
    stats = sitk.LabelIntensityStatisticsImageFilter()
    stats.Execute(cc, img)

As such, I am unsure if you will get another error after the fix. Still, I've written up a function that should allow you to easily test it out and check for some common errors (the primary issue being the images have to overlap):

def sitk_get_stats(inputImage, label):
    """
        Function to get mean and Kurtosis from a labeled image
        :param inputImage: Grey value volume readable by SimpleITK.
        :param label: Grey value volume readable by SimpleITK that overlaps with the inputImage.
        :return: Returns the mean and kurtosis of a labeled image.
        """

    inputImage = str(inputImage)
    label = str(label)
    img = sitk.ReadImage(inputImage)
    label = sitk.ReadImage(label)


    # Get the size of the image in x, y, z. They have to overlap or you'll get an error.
    size = img.GetSize()
    print("Original image size: {}, {}, {}".format(size[0], size[1], size[2]))

    size = label.GetSize()
    print("Label image size: {}, {}, {}".format(size[0], size[1], size[2]))

    #You were missing the () in your original implementation.
    labelstatsFilter = sitk.LabelIntensityStatisticsImageFilter()

    # SimpleITK collapses a lot of complexity by filling in defaults for you. 
    # You can often adjust the default parameters, which you can expose by printing out the filter .
    print(labelstatsFilter)

    #Execute the filter. 
    labelstatsFilter.Execute(label, img)

    #Get the mean intensity value for the first label.
    Mean = labelstatsFilter.GetMean(1)

    #Get the kurtosis for the first label.
    Kurtosis = labelstatsFilter.GetKurtosis(1)
    print(Mean, Kurtosis)

    #Return the values that you are curious about. 
    return  Mean, Kurtosis


#Define you input image.
ADC600 = '/Users/omarkamal/PycharmProjects/DWI/Pt1/ADC600.nii'

#Define the overlapping label. 
label = '/Users/omarkamal/PycharmProjects/DWI/Pt1/seg.nii.gz'

#Put the results into objects so you can do something with them later. 
mean, kurtosis = sitk_get_stats(inputImage=ADC600, label=label)

Upvotes: 4

Related Questions