• simpleitk 读数据 图像 dicom nii 处理数据


    最近在使用 simpleITK 读取dicom nii 处理数据 非常方便,下面记录一下;

    1.读取DICOM序列

    医学图像中一个CT序列包含很多张图片,即一个case包含许多slice,使用SimpleITK可以直接读取一个序列

    1. import SimpleITK as sitk
    2. import numpy as np
    3. reader = sitk.ImageSeriesReader()
    4. dicom_names = reader.GetGDCMSeriesFileNames('D:/dicom')
    5. reader.SetFileNames(dicom_names)
    6. image = reader.Execute()
    7. img_array = sitk.GetArrayFromImage(image)

     保存 mha nii

    sitk.WriteImage(image,"111.mha")
    sitk.WriteImage(image, folderPath + 'test.nii.gz')

    2.读多个序列 DICOM :

    1. import SimpleITK as sitk
    2. reader = sitk.ImageSeriesReader()
    3. series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs('D:/dicom')
    4. # 查看该文件夹下的序列数量
    5. nb_series = len(series_IDs)
    6. print(nb_series)
    7. #生产图像对应的label
    8. series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(file_path, series_IDs[0])
    9. reader.SetFileNames(series_file_names)
    10. image = reader.Execute()

    3.读 单张 dicom

    1. import SimpleITK as sitk
    2. import numpy as np
    3. image = sitk.ReadImage(slice_path)
    4. image_array = sitk.GetArrayFromImage(image)

    4. read mhd

    1. import SimpleITK as sitk
    2. image = sitk.ReadImage(mhd_path)

    Part2: 数据处理:

    1.  图像重采样 ResampleImageFilter

    1. import SimpleITK as sitk
    2. reader = sitk.ImageSeriesReader()
    3. dicom_names = reader.GetGDCMSeriesFileNames('D:/dicom')
    4. reader.SetFileNames(dicom_names)
    5. image = reader.Execute()
    6. resample = sitk.ResampleImageFilter()
    7. resample.SetOutputDirection(image.GetDirection())
    8. resample.SetOutputOrigin(image.GetOrigin())
    9. newspacing = [1, 1, 1]
    10. resample.SetOutputSpacing(newspacing)
    11. newimage = resample.Execute(image)

    2.重新采样,插值:

    1. #设置一个Filter
    2. resample = sitk.ResampleImageFilter()
    3. #设置插值方式(1)
    4. resample.SetInterpolator(sitk.sitkLinear)
    5. #默认像素值(2)
    6. resample.SetDefaultPixelValue( 0 );
    7. #沿着x,y,z,的spacing(3)
    8. #The sampling grid of the output space is specified with the spacing along each dimension and the origin.
    9. newspacing = [0.5,0.5,0.5]
    10. resample.SetOutputSpacing(newspacing)
    11. #设置original(4)
    12. resample.SetOutputOrigin(image.GetOrigin())
    13. #设置方向(5)
    14. resample.SetOutputDirection(image.GetDirection())
    15. #有几个值(6)
    16. size = [880,880,1014]#注意你这个设置的是Filter,有了original,spacing,number,就应该是新的吧
    17. #经试验确实size是改变pixel value的当 【1000,1000,1000】时value是0.618怎么算到的?
    18. #原来的是 512 * 512 * 203 之前的voxle spacing 是 0.859375 * 0.859375 * 2.49997
    19. # 1000 10000 1000 这个是总的要求的属
    20. # 所以这个应该是之前的 不是算得的
    21. resample.SetSize(size)
    22. #设置输入的数据 ??
    23. #设置transform
    24. #transform = sitk.Euler3DTransform()
    25. #resample.SetTransform( transform )
    26. resample.SetDefaultPixelValue(0)
    27. new = resample.Execute(image)
    28. print(new.GetSize())
    29. data = sitk.GetArrayFromImage(new)

    3. 二值化 BinaryThreshold

    1. # SIMPLEITK处理
    2. binaryimg = sitk.BinaryThreshold(image, -300, 299, 255, 0)
    3. # 处理结果保存
    4. sitk.WriteImage(binaryimg, "result.mha")

     4.镜像翻转

    1. image_arr = sitk.GetArrayFromImage(image) #
    2. size = image.GetSize()
    3. origin = image.GetOrigin() #order: x, y, z
    4. spacing = image.GetSpacing() #order:x, y, z
    5. direction = image.GetDirection()
    6. print(spacing)
    7. pixelType = sitk.sitkUInt8
    8. image_new = sitk.Image(size,pixelType)
    9. image_arr_new = image_arr[:,:,::-1]
    10. image_new = sitk.GetImageFromArray(image_arr_new)
    11. image_new.SetDirection(direction)
    12. image_new.SetSpacing(spacing)
    13. image_new.SetOrigin(origin)
    14. sitk.WriteImage(image_new,folderPath + "reverseX.nii.gz")

    5. dcm to nii -- and nii to dcm

    1. file_path = './lung_img' #dicom存放文件夹
    2. series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(file_path)
    3. series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(file_path)
    4. series_reader = sitk.ImageSeriesReader()
    5. series_reader.SetFileNames(series_file_names)
    6. image3D = series_reader.Execute()
    7. sitk.WriteImage(image3D, './lung_img.nii.gz')
    8. filedir = './lung_img.nii.gz'
    9. outdir = './lung_img/'
    10. if not os.path.isdir(outdir):
    11. os.mkdir(outdir)
    12. img = sitk.ReadImage(filedir)
    13. img = sitk.GetArrayFromImage(img)
    14. for i in range(img.shape[0]):
    15. select_img = sitk.GetImageFromArray(img[i])
    16. sitk.WriteImage(select_img,outdir+str(img.shape[0]-i)+'.dcm')
    17. //由于nii.gz不包含所有的头信息,因此转为dicom文件不会有所有的头信息。

  • 相关阅读:
    03-Android App logger策略
    对话大众软件子公司:中国的智舱、智驾比欧洲早一代
    深入理解Linux网络技术内 幕(六)——PCI层和网络接口卡
    BCN点击试剂:1516551-46-4,BCN-succinimidylester,BCN NHS
    进程间通信---对管道的详细讲解(图文案例讲解)
    【Unity】进度条和血条的三种做法
    Go目录文件路径操作
    Linux实现查看文件内容的5种方式
    深眸科技以需求定制AI视觉解决方案,全面赋能产品外观缺陷检测
    Tomcat 如何加载 SpringMVC 组件
  • 原文地址:https://blog.csdn.net/q610098308/article/details/132740598