用的radiomics拓展包,print出来的mask和image的size和spacing都是对的,但是报错如下图,查了很多地方都没有找到原因,数据应该也没有问题,希望能有大佬赐教
"""
Coding By EggTargaryen
需要的包: SimpleItk, pyradiomics, tqdm, pandas, scikit-image, scipy, trimesh, xlrd 1.2.0
"""
from ctypes import sizeof
from email.mime import image
import sys
import numpy as np
import SimpleITK as sitk
import os
import radiomics as rad
from tqdm import tqdm
import pandas as pd
from glob import glob
import cv2
import skimage.transform as skTrans
def img_resmaple(image, outspacing=[0.75, 0.75, 3]): ""“ 将体数据重采样的指定的spacing大小\n paras: outpacing:指定的spacing, return:重采样后的数据 ”"" outsize = [0, 0, 0] # 读取文件的size和spacing信息 inputsize = image.GetSize() inputspacing = image.GetSpacing() transform = sitk.Transform() transform.SetIdentity() # 计算改变spacing后的size outsize[0] = round(inputsize[0] * inputspacing[0] / outspacing[0]) outsize[1] = round(inputsize[1] * inputspacing[1] / outspacing[1]) outsize[2] = round(inputsize[2] * 10 / outspacing[2]) # 设定重采样的一些参数 resampler = sitk.ResampleImageFilter() resampler.SetTransform(transform) resampler.SetInterpolator(sitk.sitkLinear) resampler.SetOutputOrigin(image.GetOrigin()) resampler.SetOutputSpacing(outspacing) resampler.SetOutputDirection(image.GetDirection()) resampler.SetSize(outsize) newimage = resampler.Execute(image) return newimage
def feature_extractor(image: sitk.Image, mask: sitk.Image, settings: dict) -> dict: ""“ 特征提取,返回特征字典 :param image: sitk对象 :param mask: sitk对象 :param settings: 特征提取设置 :return: ”"" extractor = rad.featureextractor.RadiomicsFeatureExtractor(**settings) extractor.enableAllImageTypes() feature = extractor.execute(image, mask) return feature
def read_nii(path: str) -> sitk.Image: ""“ 读取nii文件,返回sitk对象 :param path: nii文件路径 :return: sitk对象 ”"" image = sitk.ReadImage(path) return image
if name == ‘main’: file_path = r"F:\ktz\1-432-Pre-VP\CTpre 1-132-VP\6-3023732-Pre" series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(file_path) # 查看该文件夹下的序列数量 file_reader = sitk.ImageFileReader() nb_series = len(series_IDs) for i in range(len(series_IDs)): series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(file_path, series_IDs) file_reader.SetFileName(series_file_names[0]) file_reader.ReadImageInformation() series_description = file_reader.GetMetaData(“0020|0011”) if (int(series_description) == int(4)): seq=i # 通过ID获取该ID对应的序列所有切片的完整路径, series_IDs[0]代表的是第一个序列的ID # 如果不添加series_IDs[0]这个参数,则默认获取第一个序列的所有切片路径 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(file_path, series_IDs[seq]) # 新建一个ImageSeriesReader对象 series_reader = sitk.ImageSeriesReader() # 通过之前获取到的序列的切片路径来读取该序列 series_reader.SetFileNames(series_file_names) # 获取该序列对应的3D图像 image = series_reader.Execute() print(image.GetSpacing()[2]) # 查看该3D图像的尺寸 image = img_resmaple(image, outspacing=[0.75, 0.75, 3])
mask = read_nii(glob(r"F:\ktz\1-432-Pre-VP\CTpre 1-132-VP\6-3023732-Pre\6-3023732-Pre2.nii.gz")[0])
mask = img_resmaple(mask, outspacing=[0.75, 0.75, 3])
settings = { ‘binWidth’: 25, # ‘resampledPixelSpacing’: [0.75, 0.75, 3], # 重采样Spacing需要仔细考虑 ‘interpolator’: sitk.sitkBSpline, ‘minimumROIDimensions’: 3, ‘correctMask’: True, ‘normalize’: True,
} header = [‘ID’] case = “6-3023732-Pre” extracted_features = [] index = 0 if(image.GetSize()!=mask.GetSize()): print(“mask不匹配!”) feature = feature_extractor(image, mask, settings) _temp = [case] for key, val in feature.items(): if ‘diagnostics’ not in key: if int(index) == 0: header.append(key) temp.append(val) assert len(temp) == len( header), ‘Error: Feature number is not equal! Header Len = {}, Feature Len = {}’.format( len(temp), len(header)) extracted_features.append(temp) df = pd.DataFrame(extracted_features, columns=header) df.to_csv(r"C:\Users\12594\Desktop\6-3023732-Pre.csv", index=False, encoding=‘utf-8’)