[Neuroimaging] RGB Nifti

Ariel Rokem arokem at gmail.com
Tue Feb 16 12:17:15 EST 2016


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.

Ariel


>
> Cheers,
>
> Fernando
>
> 2016-02-16 17:46 GMT+01:00 Ariel Rokem <arokem at gmail.com>:
>
>> Hi Fernando,
>>
>> Just trying to understand what you are trying to achieve:
>>
>> On Tue, Feb 16, 2016 at 8:44 AM, Fernando Pérez-García <fepegar at gmail.com
>> > wrote:
>>
>>> Hi Paulo,
>>>
>>> Thanks for your response. It seems that ConvertScalarImageToRGB reads a
>>> gray nifti and makes an RGB nifti (using ITK) with a certain colormap. The
>>> image I'm reading is a PNG file already RGB, as you can see in my code, so
>>> I think ANTS won't help here.
>>>
>>>
>>> Cheers,
>>>
>>> Fernando
>>>
>>> 2016-02-16 17:32 GMT+01:00 paulo rodrigues <khamael at gmail.com>:
>>>
>>>> Hi Fernando,
>>>>
>>>> Did you have a look at ants? It has some tools for that kind of
>>>> operations: check ConvertScalarImageToRGB
>>>>
>>>> Cheers,
>>>> Paulo
>>>>
>>>> On Tue, Feb 16, 2016 at 5:05 PM, Fernando Pérez-García <
>>>> fepegar at gmail.com> wrote:
>>>>
>>>>> Dear Nibabel experts,
>>>>>
>>>>> I'm trying to create an RGB nifti image from a PNG file. I've been
>>>>> messing around with a working RGB nifti, in order to mimic the datatype
>>>>> found in its header. So far I've managed to convert the image pixel-wise
>>>>> with for loops, which is very slow. Do you know how this could be done in a
>>>>> faster way?
>>>>>
>>>>>
>>>>> My code:
>>>>>
>>>>> import numpy as np
>>>>> import Image
>>>>> import nibabel as nib
>>>>>
>>>>> p = '/home/fernando/test/nii_rgb/TC1.png'
>>>>> im = Image.open(p)
>>>>>
>>>>> data = np.array(im)
>>>>> data = np.rot90(data)
>>>>>
>>>>
>> What is `data.shape` at this point?
>>
>> Why wouldn't the following work?
>>
>>     nii = nib.Nifti1Image(data, np.eye(4))
>>     nib.save(nii, p.replace('png', 'nii'))
>>
>> Cheers,
>>
>> Ariel
>>
>>
>>> rgb = np.zeros((data.shape[0], data.shape[1], 1, 1), [('R', 'u1'), ('G',
>>>>> 'u1'), ('B', 'u1')])
>>>>>
>>>>> for i in range(data.shape[0]):
>>>>>     for j in range(data.shape[1]):
>>>>>         rgb[i, j] = tuple(data[i, j, :])
>>>>>
>>>>> nii = nib.Nifti1Image(rgb, np.eye(4))
>>>>> nib.save(nii, p.replace('png', 'nii'))
>>>>>
>>>>>
>>>>>
>>>>> Thanks in advance,
>>>>>
>>>>> Fernando
>>>>>
>>>>> _______________________________________________
>>>>> Neuroimaging mailing list
>>>>> Neuroimaging at python.org
>>>>> https://mail.python.org/mailman/listinfo/neuroimaging
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> Neuroimaging mailing list
>>>> Neuroimaging at python.org
>>>> https://mail.python.org/mailman/listinfo/neuroimaging
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Neuroimaging mailing list
>>> Neuroimaging at python.org
>>> https://mail.python.org/mailman/listinfo/neuroimaging
>>>
>>>
>>
>> _______________________________________________
>> Neuroimaging mailing list
>> Neuroimaging at python.org
>> https://mail.python.org/mailman/listinfo/neuroimaging
>>
>>
>
> _______________________________________________
> 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/20160216/c85df39f/attachment.html>


More information about the Neuroimaging mailing list