# [Neuroimaging] Affine transformations

Stefan Hoffmann stefan.hoffmann at uni-jena.de
Fri Aug 31 09:57:59 EDT 2018

```Dear Matthew,

thanks for the fast response.

Given center point at voxels (2, 3, 4) so that:

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

and calculating Tinv.dot(Z).dot(R).dot(T).

Would an extra translation

T_extra = [[1, 0, 0, -7],
[0, 1, 0, 7],
[0, 0, 1, -8],
[0, 0, 0, 1]]

after the rotation be implemented as

Tinv.dot(Z).dot(R).dot(T).dot(T_extra)

or can it all be done at once with one T:

T= [[1, 0, 0, -9],
[0, 1, 0, 4],
[0, 0, 1, -12],
[0, 0, 0, 1]]

Also I'd like to ask how to implement the multiplication it in Python?

I know there's scipy affine_transformation on the one hand, and also
the affine_tarnsform funtion in nipy, but I'm not sure how to use them.

Shall I give them the affine matrix
Tinv.dot(Z).dot(R).dot(T).dot(T_extra) or it's inverse?

best regards,
Stefan

Quoting Matthew Brett <matthew.brett at gmail.com>:

> 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
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging

```