20221128 影像组学特征提取

在做影像组学类项目时,由于客户提供的数据资源不尽相同,对于以下这个案例,客户提供的原始图像是PNG文件,对应ROI也是2维的PNG文件图像,于是对于此类的图像数据有两步主要处理办法:

第一步:将所有的PNG文件转成NIFIT文件,在转换的过程中同步完成图像维度的转换、图像大小和方向的转换,确保原始图像和ROI是相同大小方向和维度的图像。

第二步:读取NIFIT文件,根据对应的姓名索引来匹配原始图像和ROI,同时根据

extractor = featureextractor.RadiomicsFeatureExtractor(**settings)
extractor.enableImageTypes(Original={}, Wavelet={},LBP2D={})

来建立特征提取extractor,同时设置图像标准化或调整分辨的参数等。最关键的是extractor的使用

featureVector = extractor.execute(image, mask)

 这样操作可以获取原始图像所勾画的ROI区域的图像特征信息。

以下是附上所有步骤的Python代码,仅供参考。

from radiomics import featureextractor
import os
import pandas as pd
import SimpleITK as sitk


def get_nii_Files(imageFolderPath, maskFolderPath, target_imagepath, target_maskpath):
    folders = os.listdir(imageFolderPath)
    print(folders)

    maskFiles = os.listdir(maskFolderPath)
    print(maskFiles)


    for folder in folders:
        print(folder)
        for maskFile in maskFiles:
            print(maskFile)
            if folder == maskFile:
                image = sitk.ReadImage(os.path.join(imageFolderPath, folder))
                image_arr = sitk.GetArrayFromImage(image)
                print(image_arr.shape)

                mask = sitk.ReadImage(os.path.join(maskFolderPath, maskFile))
                mask_arr = sitk.GetArrayFromImage(mask)
                print(mask_arr.shape)

                size = image.GetSize()
                origin = image.GetOrigin()
                spacing = image.GetSpacing()
                direction = image.GetDirection()

                pixelType = sitk.sitkUInt8
                image_new = sitk.Image(size, pixelType)
                mask_new = sitk.Image(size, pixelType)

                image_arr_new = image_arr[:, :, 0]
                mask_arr_new = mask_arr[:, :, 0]
                print(image_arr_new.shape)

                image_new = sitk.GetImageFromArray(image_arr_new)
                image_new.SetDirection(direction)
                image_new.SetSpacing(spacing)
                image_new.SetOrigin(origin)

                mask_new = sitk.GetImageFromArray(mask_arr_new)
                mask_new.SetDirection(direction)
                mask_new.SetSpacing(spacing)
                mask_new.SetOrigin(origin)

                folderName = folder.replace(folder.split('.')[1], '')[:-1]
                maskName = maskFile.replace(maskFile.split('.')[1], '')[:-1]

                sitk.WriteImage(image_new, os.path.join(target_imagepath, folderName+'.nii.gz'))
                sitk.WriteImage(mask_new, os.path.join(target_maskpath, maskName+'.nii.gz'))

def Get_feature(imageFilesPath, MaskFilesPath):
    imageFiles = os.listdir(imageFilesPath)
    maskFiles = os.listdir(MaskFilesPath)

    # Normalized processing and adjust 1×1 mm resolution.
    df = pd.DataFrame()
    settings = {}
    settings['binWidth'] = 25
    settings['Label'] = 1
    settings['resampledPixelSpacing'] = [1,1] # unit: mm
    # settings['interpolator'] = sitk.sitkNearestNeighbor
    settings['normalize'] = True
    # settings['force2D'] = True
    # settings['force2Ddimension'] = 0
    extractor = featureextractor.RadiomicsFeatureExtractor(**settings)
    # extractor.enableImageTypes(Original={},LoG={"sigma" : [1.0, 2.0, 3.0, 4.0 ,5.0]}, Wavelet={})
    extractor.enableImageTypes(Original={}, Wavelet={},LBP2D={})

    count = 1
    for imageFile in imageFiles:
        try:
            # print(imageFile, count/len(imageFiles))
            count += 1

            roiCount = 0
            for maskFile in maskFiles:

                imageName = imageFile.split('.')[0]
                maskName = maskFile.split('.')[0]

                # print(imageName, maskName)

                if imageName == maskName:
                    print(imageName, maskName)

                    image = os.path.join(imageFilesPath, imageName+'.nii.gz')
                    mask = os.path.join(MaskFilesPath, maskName+'.nii.gz')

                    print(os.path.isfile(image))
                    print(os.path.isfile(mask))

                    print(image, mask)

                    featureVector = extractor.execute(image, mask)
                    df_new = pd.DataFrame.from_dict(featureVector.values()).T
                    df_new.columns = featureVector.keys()
                    df_new.insert(0, 'imageFile', image)
                    df_new.insert(1, 'maskFile', mask)
                    df = pd.concat([df, df_new])

                    roiCount += 1

            if roiCount == 0:
                print('!!!!!!!!!!!!!!!!!!!')

        except Exception as e:
            print('Error.', e)

    print(df)
    # df.to_csv(r"F:\==new-project==\Feature-extraction\get_feature.csv", index = False)



if __name__ == '__main__':
    imageFolderPath = "F:\\==new-project==\\Feature-extraction\\GOUHUAQIAN"
    maskFolderPath = "F:\\==new-project==\\Feature-extraction\\GOUHUAHOU"
    target_imagepath = r'F:\==new-project==\Feature-extraction\image'
    target_maskpath = r'F:\==new-project==\Feature-extraction\mask'
    get_nii_Files(imageFolderPath, maskFolderPath, target_imagepath, target_maskpath)
    Get_feature(target_imagepath, target_maskpath)

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值