ITK 实现多张图像转成单个nii.gz或mha文件案例
主要实现的部分是利用NameGeneratorType读入系列图像,见头文件#include "itkNumericSeriesFileNames.h"。
需要包含的头文件有:
#include "itkImage.h" #include "itkImageSeriesReader.h" #include "itkImageFileWriter.h" #include "itkNumericSeriesFileNames.h" #include "itkPNGImageIO.h"//转成JPG格式,将PNG替换成JPEG就可以。 int main( int argc, char ** argv ) { // 需要四个参数,分别是程序起点,第一张图像的编号和最后一张图像的变化,输出文件的名称(包含路径) if( argc < 4 ) { std::cerr << "Usage: " << std::endl; std::cerr << argv[0] << " firstSliceValue lastSliceValue outputImageFile " << std::endl; return EXIT_FAILURE; } //定义读入图像类型,创建对应的reader typedef unsigned char PixelType; const unsigned int Dimension = 3; typedef itk::Image< PixelType, Dimension > ImageType; typedef itk::ImageSeriesReader< ImageType > ReaderType; typedef itk::ImageFileWriter< ImageType > WriterType; ReaderType::Pointer reader = ReaderType::New(); WriterType::Pointer writer = WriterType::New(); //输入参数定义 const unsigned int first = atoi( argv[1] ); const unsigned int last = atoi( argv[2] ); const char * outputFilename = argv[3];//输出的文件名加上对应格式的后缀即可,如mha或nii.gz //系列图像读入 typedef itk::NumericSeriesFileNames NameGeneratorType; NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New(); nameGenerator->SetSeriesFormat( "vwe%03d.png" ); nameGenerator->SetStartIndex( first ); nameGenerator->SetEndIndex( last ); nameGenerator->SetIncrementIndex( 1 );//张数的增长间距 //读入图像,写出图像,进行Update reader->SetImageIO( itk::PNGImageIO::New() ); reader->SetFileNames( nameGenerator->GetFileNames() ); 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; } return EXIT_SUCCESS; }
补充知识:将一组png图片转为nii.gz
主要之前使用matlab 对numpy数组存放方式不是很了解.应该是[z,x,y]这样在itksnamp上看就对了
import SimpleITK as sitk import glob import numpy as np from PIL import Image import cv2 import matplotlib.pyplot as plt # plt 用于显示图片 def save_array_as_nii_volume(data, filename, reference_name = None): """ save a numpy array as nifty image inputs: data: a numpy array with shape [Depth, Height, Width] filename: the ouput file name reference_name: file name of the reference image of which affine and header are used outputs: None """ img = sitk.GetImageFromArray(data) if(reference_name is not None): img_ref = sitk.ReadImage(reference_name) img.CopyInformation(img_ref) sitk.WriteImage(img, filename) image_path = './oriCvLab/testCvlab/img/' image_arr = glob.glob(str(image_path) + str("/*")) image_arr.sort() print(image_arr, len(image_arr)) allImg = [] allImg = np.zeros([165, 768,1024], dtype='uint8') for i in range(len(image_arr)): single_image_name = image_arr[i] img_as_img = Image.open(single_image_name) # img_as_img.show() img_as_np = np.asarray(img_as_img) allImg[i, :, :] = img_as_np # np.transpose(allImg,[2,0,1]) save_array_as_nii_volume(allImg, './testImg.nii.gz') print(np.shape(allImg)) img = allImg[:, :, 55] # plt.imshow(img, cmap='gray') # plt.show()
以上这篇ITK 实现多张图像转成单个nii.gz或mha文件案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)