[Neuroimaging] Extracting the brain coordinates from a nifti volume

serafim loukas seralouk at hotmail.com
Tue Jun 16 12:42:46 EDT 2020


Great— Thank you!

Cheers,
Makis

> On 16 Jun 2020, at 16:39, Matthew Brett <matthew.brett at gmail.com> wrote:
> 
> Hi,
> 
> On Tue, Jun 16, 2020 at 10:28 AM serafim loukas <seralouk at hotmail.com> wrote:
>> 
>> Hi Neuroimaging community,
>> 
>> I have a single nifti (nii) file and I just want to extract the brain coordinates (in mm) for all voxels (i.e. the whole nii volume).
>> How can I do that using nilearn/nibabel?
> 
> I think this will do what you want:
> 
> [ins] In [1]: import numpy as np
> [ins] In [2]: import nibabel as nib
> [ins] In [3]: img = nib.load('my_image.nii')
> [ins] In [4]: vox_indices = np.indices(img.shape).transpose(1, 2, 3, 0)
> [ins] In [8]: mm = nib.affines.apply_affine(img.affine, vox_indices)
> 
> Cheers,
> 
> Matthew
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging



More information about the Neuroimaging mailing list