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

查看:74
本文介绍了如何使用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>\lib\site-packages\nibabel\arraywriters.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维彩色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天全站免登陆