How to convert from voxel index to MNI coordinates and vice versa.

Submitted by YAN Chao-Gan on

Please see the figure below (aal.nii opened by MRIcroN).
The MNI coordinates of the current position is -66 -92 -40, and its value is 0. (-66x-92x-40= 0 on the titile).
The voxel index is 25 34 32. (X 25 Y 34 Z 32).

1. Read the file header:

[Data Header]=y_Read('aal.nii');

2. Convert from voxel index to MNI coordinates:
MNI_Coordinates = Header.mat*[25;34;32;1]
MNI_Coordinates =
-66
-92
-40
1

3. Convert from MNI coordinates to voxel index:
Voxel_Index = inv(Header.mat)*[-66;-92;-40;1]
Voxel_Index =
25
34
32
1