[Neuroimaging] Extracting the brain coordinates from a nifti volume

Matthew Brett matthew.brett at gmail.com
Tue Jun 16 10:39:43 EDT 2020


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


More information about the Neuroimaging mailing list