欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ITK 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例

 更新時(shí)間:2020年07月01日 10:27:12   作者:zhimingf  
這篇文章主要介紹了ITK 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

主要實(shí)現(xiàn)的部分是利用NameGeneratorType讀入系列圖像,見(jiàn)頭文件#include "itkNumericSeriesFileNames.h"。

需要包含的頭文件有:

#include "itkImage.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkPNGImageIO.h"http://轉(zhuǎn)成JPG格式,將PNG替換成JPEG就可以。

int main( int argc, char ** argv )
{
 // 需要四個(gè)參數(shù),分別是程序起點(diǎn),第一張圖像的編號(hào)和最后一張圖像的變化,輸出文件的名稱(包含路徑)
 if( argc < 4 )
 {
 std::cerr << "Usage: " << std::endl;
 std::cerr << argv[0] << " firstSliceValue lastSliceValue outputImageFile " << std::endl;
 return EXIT_FAILURE;
 }
//定義讀入圖像類(lèi)型,創(chuàng)建對(duì)應(yīng)的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();

//輸入?yún)?shù)定義
 const unsigned int first = atoi( argv[1] );
 const unsigned int last = atoi( argv[2] );
 const char * outputFilename = argv[3];//輸出的文件名加上對(duì)應(yīng)格式的后綴即可,如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 );//張數(shù)的增長(zhǎng)間距

//讀入圖像,寫(xiě)出圖像,進(jìn)行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;
}

補(bǔ)充知識(shí):將一組png圖片轉(zhuǎn)為nii.gz

主要之前使用matlab 對(duì)numpy數(shù)組存放方式不是很了解.應(yīng)該是[z,x,y]這樣在itksnamp上看就對(duì)了

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 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 19個(gè)Python?Sklearn中超實(shí)用的隱藏功能分享

    19個(gè)Python?Sklearn中超實(shí)用的隱藏功能分享

    今天跟大家介紹?19?個(gè)?Sklearn?中超級(jí)實(shí)用的隱藏的功能,這些功能雖然不常見(jiàn),但非常實(shí)用,它們可以直接優(yōu)雅地替代手動(dòng)執(zhí)行的常見(jiàn)操作
    2022-07-07
  • python2.7到3.x遷移指南

    python2.7到3.x遷移指南

    由于PYTHON2.7即將停止支持,小編給大家分享了一篇關(guān)python2.7到3.x遷移指南內(nèi)容,希望對(duì)各位有用。
    2018-02-02
  • Python輕松實(shí)現(xiàn)2位小數(shù)隨機(jī)生成

    Python輕松實(shí)現(xiàn)2位小數(shù)隨機(jī)生成

    在Python中,我們經(jīng)常需要生成隨機(jī)數(shù),特別是2位小數(shù)的隨機(jī)數(shù),這在模擬實(shí)驗(yàn)、密碼學(xué)、游戲開(kāi)發(fā)等領(lǐng)域都很有用,下面是如何在Python中生成2位小數(shù)的隨機(jī)數(shù)的代碼示例,需要的朋友可以參考下
    2023-11-11
  • Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法

    Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法

    這篇文章主要介紹了Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法,需要的朋友可以參考下
    2014-08-08
  • Python實(shí)現(xiàn)多進(jìn)程共享數(shù)據(jù)的方法分析

    Python實(shí)現(xiàn)多進(jìn)程共享數(shù)據(jù)的方法分析

    這篇文章主要介紹了Python實(shí)現(xiàn)多進(jìn)程共享數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Python多進(jìn)程共享數(shù)據(jù)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-12-12
  • python random從集合中隨機(jī)選擇元素的方法

    python random從集合中隨機(jī)選擇元素的方法

    今天小編就為大家分享一篇python random從集合中隨機(jī)選擇元素的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 講解python參數(shù)和作用域的使用

    講解python參數(shù)和作用域的使用

    本文會(huì)介紹如何將語(yǔ)句組織成函數(shù),還會(huì)詳細(xì)介紹參數(shù)和作用域的概念,以及遞歸的概念及其在程序中的用途。
    2013-11-11
  • Numpy隨機(jī)抽樣的實(shí)現(xiàn)

    Numpy隨機(jī)抽樣的實(shí)現(xiàn)

    本文主要介紹了Numpy隨機(jī)抽樣的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python+Flask實(shí)現(xiàn)自定義分頁(yè)的示例代碼

    Python+Flask實(shí)現(xiàn)自定義分頁(yè)的示例代碼

    分頁(yè)操作在web開(kāi)發(fā)中幾乎是必不可少的,而flask不像django自帶封裝好的分頁(yè)操作。所以本文將自定義實(shí)現(xiàn)分頁(yè)效果,需要的可以參考一下
    2022-09-09
  • Python中pymysql 模塊的使用詳解

    Python中pymysql 模塊的使用詳解

    pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。
    2019-08-08

最新評(píng)論