[Neuroimaging] How to set RGB values in NiBabel

Stephen Taylor stephen.taylor at imm.ox.ac.uk
Tue Apr 23 03:07:34 EDT 2019


Hi,

I don't understand how to set RGB values directly in NiBabel. I have seen the previous post ( https://mail.python.org/pipermail/neuroimaging/2016-November/001230.html) that gives random colours in a 3d matrix but I don't understand how to assign a specific RGB value in a particular voxel.

So building on the previous example in the link, I create a 2 x 1 x 1 volume and change the values of the colours directly:

import nibabel as nib
import numpy as np
nifti_path = "test.nii"

shape_3d = (2,1,1)
rgb_dtype = np.dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')])
rgb_arr = np.random.randint(0, 256, size=shape_3d + (3,)).astype('u1')
rgb_arr[0][0][0] = [100,150,200]
rgb_arr[1][0][0] = [10,20,30]

rgb_typed = rgb_arr.copy().view(rgb_dtype).reshape(shape_3d)

img = nib.Nifti1Image(rgb_typed, np.eye(4))
print(rgb_typed)
nib.save(img, nifti_path)

The image that is returned ('test.nii') when inspected in Fiji/ImageJ looks like this:

[image.png]

and the  rgb_typed  array is:

[[[(100, 150, 200)]]

 [[( 10,  20,  30)]]]

However, in the actual image (shown above) the green colour RGB values are 100,200,020 (not as I expected 100,150,200)and the red colour is 150,010,030 (not as I expected 10,20,30) so the RGB values seem to be mixed up. Clearly I am doing something wrong. How do I directly set the colours of a voxel in a volume?

Thanks, for any help,

Steve
----------------------------------
Head of Analysis, Visualisation  and Informatics,
MRC Weatherall Institute of Molecular Medicine,
Oxford.





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20190423/9fd1a740/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 1550 bytes
Desc: image001.png
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20190423/9fd1a740/attachment.png>


More information about the Neuroimaging mailing list