pyradiomics官方文档学习(2)--Hello Feat
2021-01-31 本文已影响0人
北欧森林
pyradiomics
官方文档里有几个示例文件,里面涉及了包括yaml文件设置、feature extraction、可视化等一系列影像组学常规操作,是非常好的学习资料。
源码请参考:https://github.com/AIM-Harvard/pyradiomics
这一节演示了对单类特征分别提取的方法
- 加载各种所需的module
from __future__ import print_function
import os
import collections
import SimpleITK as sitk
import numpy
import six
import radiomics
from radiomics import firstorder, glcm, imageoperations, shape, glrlm, glszm
- 加载示例文件(Getting the testcase)(头颅CT及对应的label文件)
imageName, maskName = radiomics.getTestCase('brain1')
if imageName is None or maskName is None: # Something went wrong, in this case PyRadiomics will also log an error
raise Exception('Error getting testcase!') # Raise exception to prevent cells below from running in case of "run all"
image = sitk.ReadImage(imageName)
mask = sitk.ReadImage(maskName)
- 图像预处理
3.1 参数设置(Extraction Settings)
settings = {}
settings['binWidth'] = 25
settings['resampledPixelSpacing'] = None
# settings['resampledPixelSpacing'] = [3, 3, 3] # This is an example for defining resampling (voxels with size 3x3x3mm)
settings['interpolator'] = 'sitkBSpline'
settings['verbose'] = True
3.2 重采样(If enabled, resample the image) #示例文件里运行和不运行这块代码结果都一样。应该是作者使用的是‘标准的’图像。
# Resample if necessary
interpolator = settings.get('interpolator')
resampledPixelSpacing = settings.get('resampledPixelSpacing')
if interpolator is not None and resampledPixelSpacing is not None:
image, mask = imageoperations.resampleImage(image, mask, **settings)
- 提取原始图像特征值(Calculate features using original image)
# Crop the image
# bb is the bounding box, upon which the image and mask are cropped
bb, correctedMask = imageoperations.checkMask(image, mask, label=1)
if correctedMask is not None:
mask = correctedMask
croppedImage, croppedMask = imageoperations.cropToTumorMask(image, mask, bb)
- 提取一阶特征值(Calculate Firstorder features) #没有明白这里为啥只有一个mean,按理说应该有18个特征?!
firstOrderFeatures = firstorder.RadiomicsFirstOrder(croppedImage, croppedMask, **settings)
# Set the features to be calculated
firstOrderFeatures.enableFeatureByName('Mean', True)
# firstOrderFeatures.enableAllFeatures()
# Print out the docstrings of the enabled features
print('Will calculate the following first order features: ')
for f in firstOrderFeatures.enabledFeatures.keys():
print(f)
print(getattr(firstOrderFeatures, 'get%sFeatureValue' % f).__doc__)
# Calculate the features and print(out result)
print('Calculating first order features...',)
result = firstOrderFeatures.execute()
print('done')
print('Calculated first order features: ')
for (key, val) in six.iteritems(result):
print(' ', key, ':', val)
Calculating first order features...
done
Calculated first order features:
Mean : 825.2354363065023
- 提取形态特征(Calculate Shape Features)
shapeFeatures = shape.RadiomicsShape(croppedImage, croppedMask, **settings)
# Set the features to be calculated
# shapeFeatures.enableFeatureByName('Volume', True)
shapeFeatures.enableAllFeatures()
![](https://img.haomeiwen.com/i24502658/260c8bcbfad33a8d.png)
# Print out the docstrings of the enabled features
print('Will calculate the following shape features: ')
for f in shapeFeatures.enabledFeatures.keys():
print(f)
print(getattr(shapeFeatures, 'get%sFeatureValue' % f).__doc__)
# Calculate the features and print(out result)
print('Calculating shape features...',)
result = shapeFeatures.execute()
print('done')
print('Calculated shape features: ')
for (key, val) in six.iteritems(result):
print(' ', key, ':', val)
![](https://img.haomeiwen.com/i24502658/293526c0be2329df.png)
- 提取GLCM特征值(Calculate GLCM Features)
glcmFeatures = glcm.RadiomicsGLCM(croppedImage, croppedMask, **settings)
# Set the features to be calculated
# glcmFeatures.enableFeatureByName('SumEntropy', True)
glcmFeatures.enableAllFeatures()
# Print out the docstrings of the enabled features
print('Will calculate the following GLCM features: ')
for f in glcmFeatures.enabledFeatures.keys():
print(f)
print(getattr(glcmFeatures, 'get%sFeatureValue' % f).__doc__)
# Calculate the features and print(out result)
print('Calculating GLCM features...',)
result = glcmFeatures.execute()
print('done')
print('Calculated GLCM features: ')
for (key, val) in six.iteritems(result):
print(' ', key, ':', val)
![](https://img.haomeiwen.com/i24502658/06439f80340899fc.png)
- 提取GLRLM特征值(Calculate GLRLM Features)
glrlmFeatures = glrlm.RadiomicsGLRLM(croppedImage, croppedMask, **settings)
# Set the features to be calculated
# glrlmFeatures.enableFeatureByName('ShortRunEmphasis', True)
glrlmFeatures.enableAllFeatures()
# Print out the docstrings of the enabled features
print('Will calculate the following GLRLM features: ')
for f in glrlmFeatures.enabledFeatures.keys():
print(f)
print(getattr(glrlmFeatures, 'get%sFeatureValue' % f).__doc__)
# Calculate the features and print(out result)
print('Calculating GLRLM features...',)
result = glrlmFeatures.execute()
print('done')
print('Calculated GLRLM features: ')
for (key, val) in six.iteritems(result):
print(' ', key, ':', val)
![](https://img.haomeiwen.com/i24502658/4cd284c4c2af4260.png)
- 提取GLSZM值(Calculate GLSZM Features)
glszmFeatures = glszm.RadiomicsGLSZM(croppedImage, croppedMask, **settings)
# Set the features to be calculated
# glszmFeatures.enableFeatureByName('LargeAreaEmphasis', True)
glszmFeatures.enableAllFeatures()
# Print out the docstrings of the enabled features
print('Will calculate the following GLSZM features: ')
for f in glszmFeatures.enabledFeatures.keys():
print(f)
print(getattr(glszmFeatures, 'get%sFeatureValue' % f).__doc__)
# Calculate the features and print(out result)
print('Calculating GLSZM features...',)
result = glszmFeatures.execute()
print('done')
print('Calculated GLSZM features: ')
for (key, val) in six.iteritems(result):
print(' ', key, ':', val)
![](https://img.haomeiwen.com/i24502658/97d944249690054c.png)
以下是加了滤波器后的特征值提取
- 提取LoG滤波图像一阶特征值(Calculate Firstorder on LoG filtered images)
logFeatures = {}
sigmaValues = [1.0, 3.0, 5.0]
for logImage, imageTypename, inputSettings in imageoperations.getLoGImage(image, mask, sigma=sigmaValues):
logImage, croppedMask = imageoperations.cropToTumorMask(logImage, mask, bb)
logFirstorderFeatures = firstorder.RadiomicsFirstOrder(logImage, croppedMask, **inputSettings)
logFirstorderFeatures.enableAllFeatures()
logFeatures[imageTypename] = logFirstorderFeatures.execute()
# Show result
for sigma, features in six.iteritems(logFeatures):
for (key, val) in six.iteritems(features):
laplacianFeatureName = '%s_%s' % (str(sigma), key)
print(' ', laplacianFeatureName, ':', val)
![](https://img.haomeiwen.com/i24502658/38b6012f11f8c4c3.png)
- 提取小波滤波图像的特征值(Calculate Features using Wavelet filter)
一阶特征值(Calculate Firstorder on filtered images)
waveletFeatures = {}
for decompositionImage, decompositionName, inputSettings in imageoperations.getWaveletImage(image, mask):
decompositionImage, croppedMask = imageoperations.cropToTumorMask(decompositionImage, mask, bb)
waveletFirstOrderFeaturs = firstorder.RadiomicsFirstOrder(decompositionImage, croppedMask, **inputSettings)
waveletFirstOrderFeaturs.enableAllFeatures()
print('Calculate firstorder features with ', decompositionName)
waveletFeatures[decompositionName] = waveletFirstOrderFeaturs.execute()
Calculate firstorder features with wavelet-LLH
Calculate firstorder features with wavelet-LHL
Calculate firstorder features with wavelet-LHH
Calculate firstorder features with wavelet-HLL
Calculate firstorder features with wavelet-HLH
Calculate firstorder features with wavelet-HHL
Calculate firstorder features with wavelet-HHH
Calculate firstorder features with wavelet-LLL
# Show result
for decompositionName, features in six.iteritems(waveletFeatures):
for (key, val) in six.iteritems(features):
waveletFeatureName = '%s_%s' % (str(decompositionName), key)
print(' ', waveletFeatureName, ':', val)
![](https://img.haomeiwen.com/i24502658/2cd53219e1f97157.png)