温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用SimpleITK读取和保存NIfTI/DICOM文件

发布时间:2020-07-01 16:25:14 来源:亿速云 阅读:1037 作者:清晨 栏目:开发技术

这篇文章将为大家详细讲解有关怎么用SimpleITK读取和保存NIfTI/DICOM文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

## 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文件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI