我就废话不多说了,大家还是直接看代码吧~
## using simpleITK to load and save data.
import SimpleITK as sitk
itk_img = sitk.ReadImage('./nifti.nii.gz')
img = sitk.GetArrayFromImage(itk_img)
print("img shape:",img.shape)
## save
out = sitk.GetImageFromArray(img)
# # out.SetSpacing(itk_img.GetSpacing())
# # out.SetOrigin(itk_img.GetOrigin())
sitk.WriteImage(out,'simpleitk_save.nii.gz')
读取DICOM序列
import sys
import SimpleITK as sitk
import numpy as np
dcm_directory = "./flair_dicom"
series_ids = sitk.ImageSeriesReader.GetGDCMSeriesIDs(dcm_directory)
print("series ids:",series_ids)
if not series_ids:
print("ERROR: given directory dose not a DICOM series.")
sys.exit(1)
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(dcm_directory,series_ids[0])
series_reader = sitk.ImageSeriesReader()
series_reader.SetFileNames(series_file_names)
image3D = series_reader.Execute()
size = image3D.GetSize()
print( "Image size:", size[0], size[1], size[2] )
image_array = sitk.GetArrayFromImage(image3D)
# SimpleITK读取的图像数据的坐标顺序为zyx,即从多少张切片到单张切片的宽和高;
# 而据SimpleITK Image获取的origin和spacing的坐标顺序则是xyz
origin = image3D.GetOrigin()
spacing = image3D.GetSpacing()
print("origin:",origin," spacing:",spacing)
读取DICOM单个文件
import SimpleITK as sitk
import numpy as np
image = sitk.ReadImage(slice_path)
image_array = sitk.GetArrayFromImage(image) # z, y, x
补充知识:itk 读取图像,两种格式之间转换
我就废话不多说了,大家还是直接看代码吧~
/*******************************************************
控制台运行程序
输入:
程序名称
读取图像文件名
保存图像文件名
输出:
保存另一种类型的图像
功能:
图像在jpeg和bmp两种格式之间互换
********************************************************/
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageIOFactory.h"
#include "itkBMPImageIOFactory.h"
#include "itkJPEGImageIOFactory.h"
#include <iostream>
int main(int argc , char *argv[])
{
if (argc < 3)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputImageFile outputImageFile " << std::endl;
return EXIT_FAILURE;
}
itk::BMPImageIOFactory::RegisterOneFactory();
itk::JPEGImageIOFactory::RegisterOneFactory();
typedef itk::Image< unsigned char, 2 > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
const char * inputFilename = argv[1];
const char * outputFilename = argv[2];
reader->SetFileName( inputFilename );
writer->SetFileName( outputFilename );
writer->SetInput( reader->GetOutput() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
system("pause");
return 0;
}
以上这篇使用SimpleITK读取和保存NIfTI/DICOM文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。
您可能感兴趣的文章:python读取dicom图像示例(SimpleITK和dicom包实现)python sitk.show()与imageJ结合使用常见的问题Python可视化mhd格式和raw格式的医学图像并保存的方法