[Neuroimaging] RGB Nifti

Matthew Brett matthew.brett at gmail.com
Tue Feb 16 12:58:41 EST 2016


Hi,

On Tue, Feb 16, 2016 at 9:17 AM, Ariel Rokem <arokem at gmail.com> wrote:
> Gotcha.
>
> On Tue, Feb 16, 2016 at 8:53 AM, Fernando Pérez-García <fepegar at gmail.com>
> wrote:
>>
>> Hi Ariel,
>>
>> data.shape is (5256, 3216, 3) at that point.
>>
>> If I do just
>> nii = nib.Nifti1Image(data, np.eye(4))
>> nib.save(nii, p.replace('png', 'nii')),
>>
>> I'll get a 3D nifti image with three slices, with one value per pixel. I
>> want a 2D nifti image with one slice, three values per pixel. I do
>> accomplish what I want with my code, but it's not very efficient. Do you
>> think I should ask in the NumPy or SciPy mailing list?
>
>
> You can certainly ask on these lists as well -- lots of knowledgable people
> there.
>
> In the meanwhile, here's what I have managed to dig up on SO. Something
> along these lines might work:
>
> http://stackoverflow.com/a/10016379/3532933
>
> But I don't have the full solution quite yet.

How about:

dt = np.dtype(zip('RGB', ('u1',) * 3))
rgb_array = data.view(dt)   # You may need data.copy().view
nii = nib.Nifti1Image(rgb_array, np.eye(4))

?

Cheers,

Matthew


More information about the Neuroimaging mailing list