Python中利用SimpleITK读取DICOM文件

有帮助的话请点赞收藏,谢谢

当同一文件夹下有多个DICOM序列时:

import os
import SimpleITK as sitk
reader = 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, x
origin = img.GetOrigin() # x, y, z
spacing = img.GetSpacing() # x, y, z

当文件夹下只有一个DICOM序列时,可以简化为:

import SimpleITK as sitk
import numpy as np
reader = sitk.ImageSeriesReader()
dcm_series = reader.GetGDCMSeriesFileNames(dcm_dir)   
reader.SetFileNames(dcm_series)
img = reader.Execute()

img_array = sitk.GetArrayFromImage(img) # z, y, x
origin = img.GetOrigin() # x, y, z
spacing = 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

你可能感兴趣的:(Python中利用SimpleITK读取DICOM文件)