# [Neuroimaging] Affine transformations

Matthew Brett matthew.brett at gmail.com
Fri Aug 31 09:15:24 EDT 2018

```Hi,

On Fri, Aug 31, 2018 at 1:51 PM, Stefan Hoffmann
<stefan.hoffmann at uni-jena.de> wrote:
>
> Dear all,
>
> I'm training Neural Networks for Image Registration of T1/T2 images for my
> master thesis. I'm using the nibabel transformation functions to:
>
> Scale, Rotate, Shift
>
> batches of my MRI scans using homogeneous coordinates. Therefore I need to
> rotate and scale around the center of the cropped parts of the scans, while
> also shifting them.
>
> Is there a smart way doing so?

I don't know about smart, but the standard way to do something like
that is to first do a translation to move your desired rotation point
to 0, 0, 0, then apply your rotations, then reverse the translation.

Let's say your center point was at voxels (2, 3, 4).  Then:

T = [[1, 0, 0, -2],
[0, 1, 0, -3],
[0, 0, 1, -4],
[0, 0, 0, 1]]

The inverse (numpy.linalg.inv(T)) is:

Tinv = [[1, 0, 0, 2],
[0, 1, 0, 3],
[0, 0, 1, 4],
[0, 0, 0, 1]]

Let's say you have a rotation affine R (for rotation around this point).

Your full affine is then Tinv.dot(Z).dot(R).dot(T)

I think you'll find that the center doesn't matter for the zooms
because, if Z is a zooming affine, and hence of this form:

Z = [[a, 0, 0, 0],
[0, b, 0, 0],
[0, 0, c, 0],
[0, 0, 0, 1]]

then Tinv.dot(Z).dot(T) == Z for any translations in T.

Is that what you mean?

Cheers,

Matthew
```