0

我正在尝试使用 sitk.LabelIntensityStatisticsImageFilter 来计算图像中标签的一阶统计信息。

我的代码是

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)

我收到此错误:

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)

显然这个模块是从 C/C++ 中的 itk 借来的?我该如何解决?

我还想计算在 simpleITK 中找不到的熵。有任何想法吗?

4

1 回答 1

4

SimpleITK 非常棒,但在我看来,它们的错误消息并不是很有帮助。老实说,当您将过滤器传递给 labelstatFilter 时,您所缺少的只是一个 ()。我只将这个特定的过滤器与连接的组件过滤器结合使用(例如):

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

因此,我不确定修复后您是否会收到另一个错误。尽管如此,我还是编写了一个函数,可以让您轻松测试它并检查一些常见错误(主要问题是图像必须重叠):

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)
于 2019-12-16T13:31:39.137 回答