时间:2021-05-22
我就废话不多说了,大家还是直接看代码吧~
# encoding=utf8'''查看和显示nii文件'''import matplotlibmatplotlib.use('TkAgg') from matplotlib import pylab as pltimport nibabel as nibfrom nibabel import nifti1from 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.shapeOrthoSlicer3D(img.dataobj).show() num = 1for i in range(0,queue,10): img_arr = img.dataobj[:,:,i] plt.subplot(5,4,num) plt.imshow(img_arr,cmap='gray') num +=1plt.show()3D显示结果:
ADNI数据维度(256,256,170)分段显示:
补充知识:python nii图像扩充
我就废话不多说了,大家还是直接看代码吧~
import osimport nibabel as nibimport numpy as npimport 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= 10rotate_theta = np.array([0, math.pi/2]) # augmentationaug_cnt = 0for 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格式图像实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
读取nii或者nii.gz文件中的信息,并且输出图像。importmatplotlibfrommatplotlibimportpylabaspltimportn
PIL:使用python自带图像处理库读取出来的图片格式numpy:使用python-opencv库读取出来的图片格式tensor:pytorch中训练时所采取
这里介绍一个nii文件保存为png格式的方法。这篇文章是介绍多个nii文件保存为png格式的方法:系统:Ubuntu16.04软件:python3.5先用pip
之前介绍过单个nii文件转换成png图像:这里介绍将多个nii文件(保存在一个文件夹下)转换成png图像。且图像单个文件夹的名称与nii名字相同。importn
因为后期主要的研究方向是医学图像处理,而现有手头的大部分数据都是nii格式或者是hdr,img格式的数据,所以首先第一步我们需要解决图像的读写问题。其实使用Op