python+itk+读取dicom数据,并保存为nii文件

上代码

import itk
import numpy as np


if __name__ == '__main__':
    file_path = "D:/test/004HeRuiPing_CT"
    image = itk.imread(file_path,pixel_type=itk.D)
    spacing = image["spacing"]
    direction = image["direction"]
    origin = image["origin"]
    size = image.GetLargestPossibleRegion().GetSize()
    print(size)

    niftiIO = itk.NiftiImageIO.New()
    writer = itk.ImageFileWriter[itk.Image[itk.D,3]].New()
    writer.SetImageIO(niftiIO)
    writer.SetInput(image);
    writer.SetFileName("hrp.nii.gz");
    writer.Update()

结果

在这里插入图片描述
python+itk+读取dicom数据,并保存为nii文件_第1张图片

读取nii数据

import itk

if __name__ == '__main__':
    # region 读取nii文件
    file_path = "hrp.nii.gz"
    niftiIO = itk.NiftiImageIO.New()
    reader = itk.ImageFileReader[itk.Image[itk.D,3]].New()
    reader.SetImageIO(niftiIO)
    reader.SetFileName(file_path)
    reader.Update()
    image = reader.GetOutput()
    size = image.GetLargestPossibleRegion().GetSize()
    print(size)
    # endregion

读取dicom

import itk

if __name__ == '__main__':
    # region 读取dicom文件
    file_path = "D:/test/004HeRuiPing_CT"
    dicomIO = itk.GDCMImageIO.New()
    reader = itk.ImageSeriesReader[itk.Image[itk.D,3]].New()
    reader.SetImageIO(dicomIO)

    namesGenerator = itk.GDCMSeriesFileNames.New()
    namesGenerator.SetInputDirectory(file_path)
    filenames = namesGenerator.GetInputFileNames()
    # print(filenames)
    # input(type(filenames))
    reader.SetFileNames(filenames)
    try:
        reader.Update()
    except itk.ExceptionObject as e:
        print(e)
    image = reader.GetOutput()
    size = image.GetLargestPossibleRegion().GetSize()
    print(size)
    # endregion

你可能感兴趣的:(python,医学图像处理,itk)