简体   繁体   English

如何正确地将 RGB 图像转换为漂亮的格式?

[英]How to correctly convert RGB images into nifty format?

I have RGB images which I'm loading as a numpy array.我有 RGB 图像,我正在将其作为 numpy 数组加载。 I would like to convert these images into the nifty format, which I can open using ITK-SNAP ( http://www.itksnap.org/pmwiki/pmwiki.php ).我想将这些图像转换成漂亮的格式,我可以使用 ITK-SNAP ( http://www.itksnap.org/pmwiki/pmwiki.php ) 打开它。

Here is what I have tried to do:这是我尝试做的事情:

import nibabel as nib 
import numpy as np

x = load_jpg_image(filename='input.jpg')  # --> x is a numpy array containing the RGB image with shape (128, 128, 3) 
img = nib.Nifti1Image(x, eye(4)) 
nib.save(img, filename='output.nii')

However, ITK-SNAP interprets output.nii as a 3D grayscale volume rather than an RGB image.然而,ITK-SNAP 将output.nii解释为 3D 灰度体积而不是 RGB 图像。 To me, it seems that ITK-SNAP should be able to handle RGB data ( see this );对我来说,ITK-SNAP 似乎应该能够处理 RGB 数据( 见此); however, I don't understand how I should save img to make this possible.但是,我不明白我应该如何保存img才能使这成为可能。 I'm using ITK-SNAP 3.6.0.我正在使用 ITK-SNAP 3.6.0。

unfortunately NIfTI was never really overly developed for RGB images.不幸的是,NIfTI 从未真正为 RGB 图像过度开发。 You can see in the latest NIfTI2 spec , the RGB and RGBA voxel types are defined (RGB having 3 bytes per pixel, RGBA 4 bytes) but I'm not aware of any tools that process these images.您可以在最新的NIfTI2 规范中看到,定义了 RGB 和 RGBA 体素类型(RGB 每个像素 3 个字节,RGBA 4 个字节)但我不知道有任何工具可以处理这些图像。

The difference with your case is that the dimensions of the images are the number of pixels and the colour channels are within the pixel.与您的情况不同的是,图像的尺寸是像素数,而颜色通道在像素内。 It looks like ITK-snap displays colour NIfTI images correctly from version 2 -- I guess they follow this format.看起来 ITK-snap 可以正确显示来自版本 2的彩色 NIfTI 图像——我猜它们遵循这种格式。

It seems you can create RGB images by casting them in a custom dtype:看来您可以通过将它们转换为自定义数据类型来创建 RGB 图像:

import nibabel as nib 
import numpy as np

RGB_DTYPE = np.dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')])

x = load_jpg_image(filename='input.jpg')  # --> x is a numpy array containing the RGB image with shape (128, 128, 3) 

# cast to custom type:
x = x.copy().view(dtype=RGB_DTYPE)  # copy used to force fresh internal structure

img = nib.Nifti1Image(x, eye(4)) 
nib.save(img, filename='output.nii')

ITK-SNAP can handle this type of image by right-clicking the image name on the left panel and selecting the option: Multi-Component Display -> RGB. ITK-SNAP 可以通过右键单击左侧面板上的图像名称并选择选项来处理此类图像:Multi-Component Display -> RGB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM