您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么用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文件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。