亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python 讀取.nii格式圖像實(shí)例

 更新時間:2020年07月01日 09:27:07   作者:益繁亦不凡  
這篇文章主要介紹了python 讀取.nii格式圖像實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

# encoding=utf8
'''
查看和顯示nii文件
'''
import matplotlib
matplotlib.use('TkAgg')
 
from matplotlib import pylab as plt
import nibabel as nib
from nibabel import nifti1
from nibabel.viewers import OrthoSlicer3D
 
example_filename = '../ADNI_nii/ADNI_002_S_0413_MR_MPR____N3__Scaled_2_Br_20081001114937668_S14782_I118675.nii'
 
img = nib.load(example_filename)
print (img)
print (img.header['db_name'])  #輸出頭信息
width,height,queue=img.dataobj.shape
OrthoSlicer3D(img.dataobj).show()
 
num = 1
for i in range(0,queue,10):
 
  img_arr = img.dataobj[:,:,i]
  plt.subplot(5,4,num)
  plt.imshow(img_arr,cmap='gray')
  num +=1
plt.show()
 

3D顯示結(jié)果:

ADNI數(shù)據(jù)維度(256,256,170)分段顯示:

補(bǔ)充知識:python nii圖像擴(kuò)充

我就廢話不多說了,大家還是直接看代碼吧~

import os
import nibabel as nib
import numpy as np
import math
 
src_us_folder = 'F:/src/ori'
src_seg_folder = 'G:/src/seg'
 
aug_us_folder = 'G:/aug/ori'
aug_seg_folder = 'G:/aug/seg'
 
img_n= 10
rotate_theta = np.array([0, math.pi/2])
 
# augmentation
aug_cnt = 0
for k in range(img_n):
  src_us_file = os.path.join(src_us_folder, (str(k) + '.nii'))
  src_seg_file = os.path.join(src_seg_folder, (str(k) + '_seg.nii'))
  # load .nii files
  src_us_vol = nib.load(src_us_file)
  src_seg_vol = nib.load(src_seg_file)
  # volume data
  us_vol_data = src_us_vol.get_data()
  us_vol_data = (np.array(us_vol_data)).astype('uint8')
  seg_vol_data = src_seg_vol.get_data()
  seg_vol_data = (np.array(seg_vol_data)).astype('uint8')
  # get refer affine matrix
  ref_affine = src_us_vol.affine
 
  ############### flip volume ###############
  flip_us_vol = np.fliplr(us_vol_data)
  flip_seg_vol = np.fliplr(seg_vol_data)
  # construct new volumes
  new_us_vol = nib.Nifti1Image(flip_us_vol, ref_affine)
  new_seg_vol = nib.Nifti1Image(flip_seg_vol, ref_affine)
  # save
  aug_us_file = os.path.join(aug_us_folder, (str(aug_cnt) + '.nii'))
  aug_seg_file = os.path.join(aug_seg_folder, (str(aug_cnt) + '_seg.nii'))
  nib.save(new_us_vol, aug_us_file)
  nib.save(new_seg_vol, aug_seg_file)
 
  aug_cnt = aug_cnt + 1
 
  ############### rotate volume ###############
  for t in range(len(rotate_theta)):
    print 'rotating %d theta of %d volume...' % (t, k)
    cos_gamma = np.cos(t)
    sin_gamma = np.sin(t)
    rot_affine = np.array([[1, 0, 0, 0],
                [0, cos_gamma, -sin_gamma, 0],
                [0, sin_gamma, cos_gamma, 0],
                [0, 0, 0, 1]])
    new_affine = rot_affine.dot(ref_affine)
    # construct new volumes
    new_us_vol = nib.Nifti1Image(us_vol_data, new_affine)
    new_seg_vol = nib.Nifti1Image(seg_vol_data, new_affine)
    # save
    aug_us_file = os.path.join(aug_us_folder, (str(aug_cnt) + '.nii'))
    aug_seg_file = os.path.join(aug_seg_folder, (str(aug_cnt) + '_seg.nii'))
    nib.save(new_us_vol, aug_us_file)
    nib.save(new_seg_vol, aug_seg_file)
 
    aug_cnt = aug_cnt + 1

以上這篇python 讀取.nii格式圖像實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pycharm訪問mysql數(shù)據(jù)庫的方法步驟

    pycharm訪問mysql數(shù)據(jù)庫的方法步驟

    這篇文章主要介紹了pycharm訪問mysql數(shù)據(jù)庫的方法步驟。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • python實(shí)現(xiàn)密度聚類(模板代碼+sklearn代碼)

    python實(shí)現(xiàn)密度聚類(模板代碼+sklearn代碼)

    這篇文章主要介紹了python實(shí)現(xiàn)密度聚類(模板代碼+sklearn代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python常見的函數(shù)及格式化輸出

    Python常見的函數(shù)及格式化輸出

    這篇文章主要介紹了Python常見的upper()、lower()、title()等函數(shù),感興趣的朋友可以一起來學(xué)習(xí)學(xué)習(xí)文章內(nèi)容
    2021-09-09
  • python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(九):數(shù)據(jù)庫客戶端 DB-API

    python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(九):數(shù)據(jù)庫客戶端 DB-API

    這篇文章主要介紹了python 數(shù)據(jù)庫客戶端 DB-API的相關(guān)資料,需要的朋友可以參考下
    2014-06-06
  • python模塊之re正則表達(dá)式詳解

    python模塊之re正則表達(dá)式詳解

    正則表達(dá)式是一種小型的、高度專業(yè)化的編程語言,并不是python中特有的,是許多編程語言中基礎(chǔ)而又重要的一部分。在python中,主要通過re模塊來實(shí)現(xiàn)。這篇文章主要介紹了python模塊之re正則表達(dá)式詳解,需要的朋友可以參考下
    2017-02-02
  • python數(shù)據(jù)庫操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))

    python數(shù)據(jù)庫操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))

    這篇文章主要介紹了python數(shù)據(jù)庫操作常用功能使用方法:獲取mysql版本、創(chuàng)建表、插入數(shù)據(jù)、slect獲取數(shù)據(jù)等,下面看示例吧
    2013-12-12
  • Python @property裝飾器原理解析

    Python @property裝飾器原理解析

    這篇文章主要介紹了Python @property裝飾器原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • python中np是做什么的

    python中np是做什么的

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python中np的作用的相關(guān)文章,有興趣的朋友們跟著學(xué)習(xí)下。
    2020-07-07
  • python socket 聊天室實(shí)例代碼詳解

    python socket 聊天室實(shí)例代碼詳解

    在本篇文章里小編給大家整理了關(guān)于python socket 聊天室的相關(guān)知識點(diǎn),需要的朋友們參考下。
    2019-11-11
  • 詳解Django解決ajax跨域訪問問題

    詳解Django解決ajax跨域訪問問題

    這篇文章主要介紹了詳解Django解決ajax跨域訪問問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論