Python中利用SimpleITK读取DICOM文件 - 简书
0.122018.08.20 10:43:53字数 90阅读 8,297
有帮助的话请点赞收藏,谢谢
当同一文件夹下有多个DICOM序列时:
import osimport SimpleITK as sitkreader = sitk.ImageSeriesReader()seriesIDs = reader.GetGDCMSeriesIDs(dcm_dir)dcm_series = reader.GetGDCMSeriesFileNames(dcm_dir, seriesIDs[0])reader.SetFileNames(dcm_series)img = reader.Execute()img_array = sitk.GetArrayFromImage(img) # z, y, xorigin = img.GetOrigin() # x, y, zspacing = img.GetSpacing() # x, y, z
当文件夹下只有一个DICOM序列时,可以简化为:
import SimpleITK as sitkimport numpy as npreader = sitk.ImageSeriesReader()dcm_series = reader.GetGDCMSeriesFileNames(dcm_dir) reader.SetFileNames(dcm_series)img = reader.Execute()img_array = sitk.GetArrayFromImage(img) # z, y, xorigin = img.GetOrigin() # x, y, zspacing = img.GetSpacing() # x, y, z
有时会出现跳层的问题,编写了个函数来happy safe reading
def safe_sitk_dcm_read(dcm_dir, *args, **kwargs): dcm_list = [os.path.join(dcm_dir, i) for i in os.listdir(dcm_dir)] indices = np.array([pydicom.dcmread(i).InstanceNumber for i in dcm_list]) dcm_list = np.array(dcm_list)[indices.argsort()[::-1]] return sitk.ReadImage(dcm_list, *args, **kwargs)
Reference:
https://github.com/SimpleITK/SimpleITK/issues/436
http://www.yueye.org/2017/python-simpleitk-read-process-dicom.html
赞 (0)