上代码
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()
结果
读取nii数据
import itk
if __name__ == '__main__':
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)
读取dicom
import itk
if __name__ == '__main__':
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()
reader.SetFileNames(filenames)
try:
reader.Update()
except itk.ExceptionObject as e:
print(e)
image = reader.GetOutput()
size = image.GetLargestPossibleRegion().GetSize()
print(size)