如何使用 NiBabel 编写彩色 3D NIfTI? [英] How to write a color 3D NIfTI with NiBabel?

查看:27
本文介绍了如何使用 NiBabel 编写彩色 3D NIfTI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 nibabel 写出 3D 灰度 .nii 文件并在 NIfTI 查看器(Mango、MCicron)中打开它们没有问题.但是,我无法写出 3D 颜色,因为每个 RGB 平面都被解释为不同的体积.例如.输出:

I've had no problem writing out 3D grayscale .nii files with nibabel and opening them in NIfTI viewers (Mango, MCIcron). However I haven't been able to write out 3D color, as each RGB plane is being interpreted as a different volume. E.g. the output from this:

import nibabel as nib
import numpy as np
nifti_path = "/my/local/path"
test_stack = (255.0 * np.random.rand(20, 201, 202, 3)).astype(np.uint8)
ni_img = nib.Nifti1Image(test_stack, np.eye(4))
nib.save(ni_img, nifti_path)

被视为 3 个独立的 20x201x202 卷.我还尝试将颜色平面放在第一个轴上(即 np.random.rand(3, 20, 201, 202)),但遇到了同样的问题.环顾四周,似乎有一个数据集"字段需要为 24 位 RGB 平面图像设置为 128.nibabel 的一个好处是它如何根据提供给它的 numpy 数组自动设置标头.然而,这是一个模棱两可的情况,如果我打印标题信息,我可以看到它将数据类型设置为 2 (uint8),这大概是观众将其解释为单独的卷,而不是 RGB24 的原因.我在 API 用于设置数据类型,但是文档 确实提到了那些有巨大勇气"的人可以访问原始字段.这样做,即

is seen as 3 separate 20x201x202 volumes. I also tried putting the color planes in the first axis (i.e. np.random.rand(3, 20, 201, 202)), but get the same issue. Looking around a bit it seems there is a "dataset" field that needs to be set to 128 for 24-bit RGB planar images. One nice thing about nibabel is how it automatically sets up the header based on the numpy array fed to it. However this is an ambiguous case and if I print the header information I can see it setting the datatype to 2 (uint8), which presumably is why viewers are interpreting it as separate volumes, not RGB24. I don't see any official support in the API for setting the datatype, but the documentation does mention access to the raw fields for those with "great courage". Doing this, i.e.

hdr = ni_img.header
raw = hdr.structarr
raw['datatype'] = 128

用于更改标题值

print(hdr)

给出数据类型:RGB"但在写入时

gives "datatype : RGB" but when writing

nib.save(ni_img, nifti_path)

我收到一个错误:

File "<python path>libsite-packages
ibabelarraywriters.py", line 126, in scaling_needed
raise WriterError('Cannot cast to or from non-numeric types')
nibabel.arraywriters.WriterError: Cannot cast to or from non-numeric types

如果某些 arr_dtype != out_dtype,则会引发异常,所以大概我对原始标头的黑客攻击导致了一些不一致.

The exception is raised if some arr_dtype != out_dtype, so presumably my hacking of the raw header is causing some inconsistency down the line.

那么,有没有合适的方法来做到这一点?

So, is there a proper way to do this?

推荐答案

感谢 Neuroimaging 分析邮件列表中的 matthew.brett,我能够像这样写出 3-d color NIfTI:

Thanks to matthew.brett on the Neuroimaging analysis mailing list, I'm able to write out 3-d color NIfTI like so:

# ras_pos is a 4-d numpy array, with the last dim holding RGB
shape_3d = ras_pos.shape[0:3]
rgb_dtype = np.dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')])
ras_pos = ras_pos.copy().view(dtype=rgb_dtype).reshape(shape_3d)  # copy used to force fresh internal structure
ni_img = nib.Nifti1Image(ras_pos, np.eye(4))
nib.save(ni_img, output_path)

这篇关于如何使用 NiBabel 编写彩色 3D NIfTI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆