dcm图片到nrrd的格式转换(二维到三维)

处理过程:多张dcm图片形成一个dcm序列。调用sitk库的writeImage成为nrrd文件

 

import SimpleITK as sitk
import os
#直接用hndata里的ct图像dcm变成nrrd
def dcmtonrrd(pp,cpa,nb):#
    patient_id = os.listdir(pp)
    unexist = 0
    for i in range(len(patient_id)):
        tidy_ct_path = pp + '\\' + patient_id[nb] + cpa
        if os.path.exists(tidy_ct_path):
            print(tidy_ct_path)
            dcms_name = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(tidy_ct_path)
            dcms_read = sitk.ImageSeriesReader()#类似java里面的实例
            dcms_read.SetFileNames(dcms_name)#将list里面的元素设置
            dcms_series = dcms_read.Execute()#应该是这一系列图片标准化,各种信息的完善吧。就像dcm文件里面一些header还有相关内容的介绍
            print(r'D:\empirical\ct'+'\\'+'%d.nrrd'%(nb+1))
            sitk.WriteImage(dcms_series,r'D:\empirical\ct'+'\\%d.nrrd'%(nb+1))
            nb = nb+1
        else:
            print('目录%s不存在'%tidy_ct_path)
            unexist = unexist+1
            nb = nb+1
    print('每位patient对应的ct图片已合成三维图片nrrd格式,共计%d个nrrd文件'%(range(len(patient_id))-unexist))
if __name__ == '__main__':
    patient_path = r'D:\gdesign\H&N data'
    ct_path_add = r'\CT\image'
    number = 0
    dcmtonrrd(patient_path,ct_path_add,number)
    # print(ct)

 

你可能感兴趣的:(radiomics)