使用SimpleITK读取和保存NIfTI/DICOM文件实例

时间:2021-05-22

我就废话不多说了,大家还是直接看代码吧~

## using simpleITK to load and save data.import SimpleITK as sitkitk_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 sysimport SimpleITK as sitkimport numpy as npdcm_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的坐标顺序则是xyzorigin = image3D.GetOrigin()spacing = image3D.GetSpacing()print("origin:",origin," spacing:",spacing)

读取DICOM单个文件

import SimpleITK as sitkimport numpy as npimage = 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文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章