nibabel库 - 初步

 

我的目的主要是利用 nibabel 这个库来处理一些 NIFTI 格式的  医学方面的数据。

官方的网址如下:

https://nipy.org/nibabel/nibabel_images.html#the-image-object

 

首先, 自然是安装库, 然后我们使用 load 函数来读取一个 nii.gz格式的文件,

它 return 的是一个  <class 'nibabel.nifti1.Nifti1Image'>   类型的 object

import nibabel as nib
img = nib.load(example_file)

 

关于 这个return 回来的数据类型, 我们要知道如下的三点:  

(n维数组, 代表图像,  仿射变换  , meta_data)

A nibabel image object is the association of three things:

  • an N-D array containing the image data;

  • a (4, 4) affine matrix mapping array coordinates to coordinates in some RAS+ world coordinate space (Coordinate systems and affines);

  • image metadata in the form of a header.

 

#  demo 

简单的读取,  查看return 的 object 的类型 和 shape 

if __name__ == '__main__':

    demo_img, demo_seg = load_case(cid=123)
    print(type(demo_img))
    print(demo_img.shape

--------------- output  ----------

<class 'nibabel.nifti1.Nifti1Image'>
(61, 512, 512)

 

#  使用 get_fdata , 就可以转为float 为 dtype 的 ndarray类型的 object 

    hu_data = demo_img.get_fdata()
    print(hu_data.shape)
    print(type(hu_data))
    print(hu_data.dtype)


——————————————--------output-- - - -----------

<class 'nibabel.nifti1.Nifti1Image'>
(61, 512, 512)
<class 'numpy.ndarray'>
float64

#  你可能也见过这个函数 get_data 缺少了一个‘ f ’ , 

 

I'm working with Nibabel to read some .nii files.
 I came across different examples and some of them use get_data() function
 while some of them use get_fdata() function.
 I could not find what the difference is in their documentation (nibabel manuel).
 Can anyone explain?

在这里可以找到 https://nipy.org/nibabel/reference/nibabel.dataobj_images.html
(也就是get_data 和 get_fdata 的一些区别)

二者的一些说明如下:

get_data(caching='fill')

Return image data from image with any necessary scaling applied
get_fdata(caching='fill', dtype=)

Return floating point image data with necessary scaling applied

简单的来说, 一个是返回原数据, 一个则是返回浮点数。

就是这么点区别

不过有一点需要注意 , 这个get_data 函数要被取缔了:

We recommend you use the get_fdata method instead of the get_data method,
 because it is easier to predict the return data type. 
We will deprecate the get_data method around April 2018, 
and remove it around April 2020.

If you don’t care about the predictability of the return data type, 
and you want the minimum possible data size in memory, 
you can replicate the array 
that would be returned by img.get_data() by using np.asanyarray(img.dataobj).

 

 

 

 

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
安装nibabel时遇到的问题可以有多种原因。根据引用\[1\]和引用\[2\]的内容,可以尝试以下几种解决方法: 1. 使用conda安装:根据引用\[1\]中的建议,可以尝试使用以下命令安装nibabel: ``` conda install -c simpleitk simpleitk conda install -c conda-forge nibabel conda install pydicom ``` 2. 使用pip安装:如果使用pip安装时遇到问题,可以尝试使用引用\[2\]中提供的命令,使用不同的镜像源进行安装: ``` pip install nibabel -i http://pypi.douban.com/simple --trusted-host pypi.douban.com ``` 3. 检查网络连接:有时候安装失败是由于网络连接问题导致的。可以尝试检查网络连接是否正常,并且尝试使用其他网络环境进行安装。 4. 更新pip和conda:确保你的pip和conda是最新版本,可以使用以下命令进行更新: ``` pip install --upgrade pip conda update conda ``` 如果以上方法仍然无法解决问题,可以尝试搜索相关错误信息或者参考nibabel的官方文档或社区论坛,寻找其他解决方案。 #### 引用[.reference_title] - *1* [conda安装医学图像处理SimpleITK、nibabel、pydicom等](https://blog.csdn.net/guzhao9901/article/details/120990118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [医学图像处理-安装nibabel包命令](https://blog.csdn.net/weixin_44911552/article/details/127929358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值