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

1.读取DICOM序列

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

import SimpleITK as sitk
import numpy as np

reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames('D:/dicom')
reader.SetFileNames(dicom_names)
image = reader.Execute()

img_array = sitk.GetArrayFromImage(image)

 保存 mha nii

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

2.读多个序列 DICOM :

import SimpleITK as sitk


reader = sitk.ImageSeriesReader()
series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs('D:/dicom')  
# 查看该文件夹下的序列数量
nb_series = len(series_IDs)
print(nb_series)
#生产图像对应的label
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(file_path, series_IDs[0])
reader.SetFileNames(series_file_names)
image = reader.Execute()

3.读 单张 dicom

import SimpleITK as sitk
    import numpy as np

    image = sitk.ReadImage(slice_path)
    image_array = sitk.GetArrayFromImage(image)

4. read mhd

import SimpleITK as sitk

image = sitk.ReadImage(mhd_path)

Part2: 数据处理:

1.  图像重采样 ResampleImageFilter

import SimpleITK as sitk

    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames('D:/dicom') 
    reader.SetFileNames(dicom_names)
    image = reader.Execute()

    resample = sitk.ResampleImageFilter()
    resample.SetOutputDirection(image.GetDirection())
    resample.SetOutputOrigin(image.GetOrigin())
    newspacing = [1, 1, 1]
    resample.SetOutputSpacing(newspacing)
    newimage = resample.Execute(image)

2.重新采样,插值:

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

#设置transform
#transform = sitk.Euler3DTransform()
#resample.SetTransform( transform )
resample.SetDefaultPixelValue(0)
new = resample.Execute(image)
print(new.GetSize())
data = sitk.GetArrayFromImage(new)

3. 二值化 BinaryThreshold

#  SIMPLEITK处理
binaryimg = sitk.BinaryThreshold(image, -300, 299, 255, 0)

#  处理结果保存
sitk.WriteImage(binaryimg, "result.mha")

 4.镜像翻转

image_arr = sitk.GetArrayFromImage(image) # 
size = image.GetSize()
origin = image.GetOrigin() #order: x, y, z
spacing = image.GetSpacing() #order:x, y, z
direction = image.GetDirection()
print(spacing) 

pixelType = sitk.sitkUInt8
image_new = sitk.Image(size,pixelType)

image_arr_new = image_arr[:,:,::-1]
image_new = sitk.GetImageFromArray(image_arr_new)
image_new.SetDirection(direction)
image_new.SetSpacing(spacing)
image_new.SetOrigin(origin)

sitk.WriteImage(image_new,folderPath + "reverseX.nii.gz")