Converting mm coordinates from one T1 to another T1of same subjecr


I have the mm coordinates of an ROI in scanner space, and I would like to convert them to mm coordinates in the scanner space of a T1 from the same subject but collected on a different day.

Here’s the process I’ve tried that I think should work, but doesn’t

register the two T1s together

flirt -in T1_1.nii.gz -ref T1_2.nii.gz -omat reg.mat -out T1_1_to_T1_2.nii.gz

I check the registration and it looks good

Then use this registration matrix to transform the points

import nibabel as nib
pt = [1,2,3]
xfm = np.loadtxt('path/to/reg.mat')
new_pt = nib.affines.apply_affine(xfm, pt)

This yields points outside the brain in T1_2 space

Any advice on what’s going wrong here?

Hmm, not sure exactly what is going wrong, but I have some followup questions:

Have you tried this code from the apply_affine documentation page? This is a simple implementation of the function that works in a 3d case, which seems to be appropriate baed on the shape of your pt vector.

res =[:3,:3], pts.T) + aff[:3,3:4]
transformed_pts = res.T
(this aff = your xfm)

Do you get different results doing it like this?

Can you print what your xfm matrix is?

Could you get/create .nii files for your ROIs, in which case you can use flirt to apply the reg.mat transformation?


The code above gives me a 3x3 array out. I’m having to transform my point to an np array before I can use .T on it

xfm = np.loadtxt(xfm_path)
og_pt = [-42, 48, 56]
og_pt = np.array(og_pt)
new_pt2 =[:3,:3], og_pt.T) + xfm[:3,3:4]


[[264.05661083 173.12695084 162.96734284]
 [222.15469493 131.22503494 121.06542694]
 [292.13923783 201.20957784 191.04996984]]

Here’s what my affine looks like

[-9.76562500e-01 -2.00296293e-10 -0.00000000e+00  9.51157379e+01]
 [ 2.00296293e-10 -9.76562500e-01 -0.00000000e+00  1.33161728e+02]
 [ 0.00000000e+00  0.00000000e+00  1.00000000e+00 -4.92365761e+01]
 [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  1.00000000e+00]]