[Neuroimaging] How to set RGB values in NiBabel

Ariel Rokem arokem at gmail.com
Wed Apr 24 22:36:19 EDT 2019


Hi Steve,

Not 100% sure. Maybe our implementation of something like this in DIPY can
help?

https://github.com/nipy/dipy/blob/master/dipy/io/utils.py#L49-L91

Cheers,

Ariel



On Tue, Apr 23, 2019 at 7:24 AM Stephen Taylor <stephen.taylor at imm.ox.ac.uk>
wrote:

> 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: 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.
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20190424/1bf95aef/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 1550 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20190424/1bf95aef/attachment.png>


More information about the Neuroimaging mailing list