您好,登錄后才能下訂單哦!
小編給大家分享一下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文件有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。