# [Neuroimaging] Affine transformations

Matthew Brett matthew.brett at gmail.com
Fri Aug 31 09:26:42 EDT 2018

```Hi,

On Fri, Aug 31, 2018 at 2:15 PM, Matthew Brett <matthew.brett at gmail.com> wrote:
> 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)

Sorry, editing cruft, I mean:

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

Cheers,

Matthew
```