0

I have RGB images which I'm loading as a numpy array. 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).

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. To me, it seems that ITK-SNAP should be able to handle RGB data (see this); however, I don't understand how I should save img to make this possible. I'm using ITK-SNAP 3.6.0.

hmn Falahi
  • 730
  • 5
  • 22
gab
  • 792
  • 1
  • 10
  • 36

2 Answers2

1

unfortunately NIfTI was never really overly developed for RGB images. 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.

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.

alle_meije
  • 2,424
  • 1
  • 19
  • 40
1

It seems you can create RGB images by casting them in a custom dtype:

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.

gab
  • 792
  • 1
  • 10
  • 36