[Neuroimaging] affine issue (?)

Matthew Brett matthew.brett at gmail.com
Thu Jul 13 09:59:05 EDT 2017

```Hi,

On Thu, Jul 13, 2017 at 2:40 PM, Christophe Pallier
<christophe at pallier.org> wrote:
> Hello,
>
> I have an nii image (a contrast computed by SPM, in the MNI space),
> which, according to SPM, has the following affine:
>
>
>>> V.mat
> 2   0   0    -74
> 0   2   0   -108
> 0   0   2    -66
> 0   0   0    1
>
>
> However, the same image loaded with nibabel yields a different affine:
> [[   2.            0.            0.          -72.00000763]
>  [   0.            2.            0.         -106.        ]
>  [   0.            0.            2.          -64.        ]
>  [   0.            0.            0.            1.        ]]

Right - what JB said - the affine is the mapping from voxel coordinate
to mm coordinate.   For Matlab, the voxel coordinates are 1-based, so
the first voxel is coordinate (1, 1, 1) whereas for Python (or C) the
first coordinate is (0, 0, 0).

For example, you can think of the Matlab affine as the composition of
the translation taking the first coordinate to 0, 0, 0, followed by
the Python affine:

import numpy as np
py_affine = np.array([[ 2.,  0., 0.,  -72.00000763],
[ 0.,  2., 0., -106.        ],
[ 0.,  0., 2.,  -64.        ],
[ 0.,  0., 0.,    1.        ]])

from_111 = np.array([[1, 0, 0, -1],
[0, 1, 0, -1],
[0, 0, 1, -1],
[0, 0, 0, 1]])

print(py_affine.dot(from_111))

This gives:

[[   2.            0.            0.          -74.00000763]
[   0.            2.            0.         -108.        ]
[   0.            0.            2.          -66.        ]
[   0.            0.            0.            1.        ]]

Cheers,

Matthew
```