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

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

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?