剂量预测项目需要将从TPS导出的DCIOM_RT 数据保存为nii,以便后续处理。
开贴记录:
1、dose文件解析
dose文件是按照3mm体素进行分布的,其大小和原始CT图像并不一致,这里我们使用SimpleITK 包,读取病人CT图像和Dose文件,并通过sitk中的重采样将dose图像和CT图像进行对齐。代码如下:
def resize_image_itk(ori_img, target_img, resamplemethod=sitk.sitkNearestNeighbor):
"""
用itk方法将原始图像resample到与目标图像一致
:param ori_img: 原始需要对齐的itk图像
:param target_img: 要对齐的目标itk图像
:param resamplemethod: itk插值方法: sitk.sitkLinear-线性 sitk.sitkNearestNeighbor-最近邻
:return:img_res_itk: 重采样好的itk图像
使用示范:
import SimpleITK as sitk
target_img = sitk.ReadImage(target_img_file)
ori_img = sitk.ReadImage(ori_img_file)
img_r = resize_image_itk(ori_img, target_img, resamplemethod=sitk.sitkLinear)
"""
target_Size = target_img.GetSize() # 目标图像大小 [x,y,z]
target_Spacing = target_img.GetSpacing() # 目标的体素块尺寸 [x,y,z]
target_origin = target_img.GetOrigin() # 目标的起点 [x,y,z]
target_direction = target_img.GetDirection() # 目标的方向 [冠,矢,横]=[z,y,x]
# itk的方法进行resample
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(ori_img) # 需要重新采样的目标图像
# 设置目标图像的信息
resampler.SetSize(target_Size) # 目标图像大小
resampler.SetOutputOrigin(target_origin)
resampler.SetOutputDirection(target_direction)
resampler.SetOutputSpacing(target_Spacing)
# 根据需要重采样图像的情况设置不同的dype
if resamplemethod == sitk.sitkNearestNeighbor:
resampler.SetOutputPixelType(sitk.sitkUInt8) # 近邻插值用于mask的,保存uint8
else:
resampler.SetOutputPixelType(sitk.sitkFloat32) # 线性插值用于PET/CT/MRI之类的,保存float32
resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity))
resampler.SetInterpolator(resamplemethod)
itk_img_resampled = resampler.Execute(ori_img) # 得到重新采样后的图像
return itk_img_resampled
def read_Image_and_RTdose(img_path='', dose_path='',savepath = ''):
""" read image and rtdose file,and convert them to .nii or .npy
read image and rtdose file,and convert them to .nii or .npy
:param img_path: the directory of image path(Notice:it is a directory). for exampel:"./data/patient1/img"
:param dose_path: the directory of RTdose.dcm path. for example:'./data/patient1/RTdose.dcm'
:return:None
"""
# 读取病人dicom
reader = sitk.ImageSeriesReader()
img_names = reader.GetGDCMSeriesFileNames(img_path)
reader.SetFileNames(img_names)
image = reader.Execute()
#image_array = sitk.GetArrayFromImage(image) #get image data
# 读取dose文件
dose = sitk.ReadImage(dose_path)
#dose_img = sitk.GetArrayFromImage(dose)#get image data
new_dose = resize_image_itk(dose, image, resamplemethod=sitk.sitkLinear) # 对齐后的dose图像
#sitk.WriteImage(image,'.dat/1/patient1.nii')
sitk.WriteImage(new_dose, savepath)
2、StrctrSets 文件解析
该部分解析已在前面讲解,请参照:StrctrSets 解析
3、对原始数据进行处理使其满足要求
原始的病人的所有数据都保存在一个文件夹中,如图:
为了便于处理,按照以下结构进行处理:
主要涉及到CT图像整合到patient文件夹 中,将原始的20210020_StrctrSets.dcm,20210020_Dose.dcm重命名为StrctrSets.dcm和dose.dcm
主要使用以下代码:
'''
此文件主要完成对原始数据的整理,讲一个文件夹中的.dcm数据进行整理
将病人的CT.dcm保存到一个文件夹'patient'中
最后得到的目录结构为:
--data
--patient1
--.DCM
--dose.dcm
--StrctrSets.dcm
--patient2
--.DCM
--dose.dcm
--StrctrSets.dcm
'''
import os
from shutil import move
import glob
import re
path = os.path.join(r'E:\数据集\食管癌\食管癌1\食管癌1\wang,defa','patient')
temp = glob.glob(r'E:\数据集\食管癌\食管癌1\食管癌1\wang,defa\*.DCM')
for tmp in temp:
if re.search('image',tmp):
move(tmp,path)
datapath = r'E:\数据集\食管癌\食管癌2\食管癌2'
patient_name = os.listdir(datapath) #获取所有病人文件夹
for name in patient_name:
getpath = os.path.join(datapath, name)
savepath = os.path.join(getpath,'patient')
if os.path.exists(savepath) ==False:
os.makedirs(savepath)
temp = glob.glob(os.path.join(getpath,'*.DCM'))
for tmp in temp:
if re.search('image', tmp):
print("now prcoess:")
print(tmp)
move(tmp, savepath)
'''
以上主要部分是将image整合到patient文件夹
'''
datapath = './origin_data' #此处我已经将文件进行了整理到新的存储位置,故重写了
patient_name = os.listdir(datapath) #获取所有病人文件夹
for name in patient_name:
getpath = os.path.join(datapath, name)
temp = glob.glob(os.path.join(getpath,'*.DCM'))
for tmp in temp:
if re.search('Dose', tmp):
print("now prcoess:")
print(tmp)
savename = os.path.join(getpath,'dose.dcm')
os.rename(tmp,savename)
if re.search('StrctrSets', tmp):
print("now prcoess:")
print(tmp)
savename = os.path.join(getpath, 'StrctrSets.dcm')
os.rename(tmp, savename)
'''
以上主要部分是将20210020_StrctrSets.dcm,20210020_Dose.dcm重命名为StrctrSets.dcm和dose.dcm
'''
4、批量处理整合后的数据
通过以上的步骤后,已经可以将得到原始数据进行批量处理。把CT.dcm、dose.dcm、StrctrSets.dcm 转换为nii保存到新的文件夹
'''
此文件用于将原始数据进行解析
将RTdose,RTstruct,以及RTDICOM 转换为nii或npy
数据文件结构为:
--data
--patient1
--DICOM
--RTdose.dcm
--RTstruct.dcm
--patient2
--DICOM
--RTdose.dcm
--RTstruct.dcm
'''
import os
import pydicom
import dicom2nifti
import nibabel as nib
import SimpleITK as sitk
'''
以下部分为解析RTdose 并将RTdose.dcm的剂量数据通过simpleItk 进行重采样后与原始图像对齐
'''
from dcmrtstruct2nii import dcmrtstruct2nii, list_rt_structs
def resize_image_itk(ori_img, target_img, resamplemethod=sitk.sitkNearestNeighbor):
"""
用itk方法将原始图像resample到与目标图像一致
:param ori_img: 原始需要对齐的itk图像
:param target_img: 要对齐的目标itk图像
:param resamplemethod: itk插值方法: sitk.sitkLinear-线性 sitk.sitkNearestNeighbor-最近邻
:return:img_res_itk: 重采样好的itk图像
使用示范:
import SimpleITK as sitk
target_img = sitk.ReadImage(target_img_file)
ori_img = sitk.ReadImage(ori_img_file)
img_r = resize_image_itk(ori_img, target_img, resamplemethod=sitk.sitkLinear)
"""
target_Size = target_img.GetSize() # 目标图像大小 [x,y,z]
target_Spacing = target_img.GetSpacing() # 目标的体素块尺寸 [x,y,z]
target_origin = target_img.GetOrigin() # 目标的起点 [x,y,z]
target_direction = target_img.GetDirection() # 目标的方向 [冠,矢,横]=[z,y,x]
# itk的方法进行resample
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(ori_img) # 需要重新采样的目标图像
# 设置目标图像的信息
resampler.SetSize(target_Size) # 目标图像大小
resampler.SetOutputOrigin(target_origin)
resampler.SetOutputDirection(target_direction)
resampler.SetOutputSpacing(target_Spacing)
# 根据需要重采样图像的情况设置不同的dype
if resamplemethod == sitk.sitkNearestNeighbor:
resampler.SetOutputPixelType(sitk.sitkUInt8) # 近邻插值用于mask的,保存uint8
else:
resampler.SetOutputPixelType(sitk.sitkFloat32) # 线性插值用于PET/CT/MRI之类的,保存float32
resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity))
resampler.SetInterpolator(resamplemethod)
itk_img_resampled = resampler.Execute(ori_img) # 得到重新采样后的图像
return itk_img_resampled
def read_Image_and_RTdose(img_path='', dose_path='',savepath = ''):
""" read image and rtdose file,and convert them to .nii or .npy
read image and rtdose file,and convert them to .nii or .npy
:param img_path: the directory of image path(Notice:it is a directory). for exampel:"./data/patient1/img"
:param dose_path: the directory of RTdose.dcm path. for example:'./data/patient1/RTdose.dcm'
:return:None
"""
# 读取病人dicom
reader = sitk.ImageSeriesReader()
img_names = reader.GetGDCMSeriesFileNames(img_path)
reader.SetFileNames(img_names)
image = reader.Execute()
#image_array = sitk.GetArrayFromImage(image) #get image data
# 读取dose文件
dose = sitk.ReadImage(dose_path)
#dose_img = sitk.GetArrayFromImage(dose)#get image data
new_dose = resize_image_itk(dose, image, resamplemethod=sitk.sitkLinear) # 对齐后的dose图像
#sitk.WriteImage(image,'.dat/1/patient1.nii')
sitk.WriteImage(new_dose, savepath)
datapath = './origin_data'
patient_name = os.listdir(datapath) #获取所有病人文件夹
for name in patient_name:
getpath = os.path.join(datapath, name) #病人路径
savepath = os.path.join('./ProcessData', name+'/dose.nii.gz')
tmppath = os.path.join('./ProcessData', name)
if os.path.exists(tmppath)==False:
os.makedirs(tmppath)
read_Image_and_RTdose(img_path=os.path.join(getpath,'patient'),
dose_path=os.path.join(getpath,'dose.dcm'),
savepath=savepath)
name, image = dcmrtstruct2nii(os.path.join(getpath,'StrctrSets.dcm'),
dicom_file=os.path.join(getpath,'patient'),
output_path=tmppath)
'''
解析RT—struct.dcm文件 并转换为nii保存
需要修改dcmrtstruct2nii 源码中 读取dicom 文件时加入参数force=true
'''
from dcmrtstruct2nii import dcmrtstruct2nii, list_rt_structs
path = './RTDICOM/20220356_StrctrSets.dcm'
# #save_path = ''
# #print(list_rt_structs(path)) #显示出有哪些结构体
#
# name, image = dcmrtstruct2nii(path, dicom_file='./wang,defa/patient',output_path='./data/1')
# #参数分别为struct文件、病人图像文件夹,输出文件夹
到此文件解析为nii已经结束,下一步是将ptv,OAR进行拼接,保存为nii,具体方法,请见后续。