Building the ijk to RAS transform from a nrrd file

Hi Slicers,

I am working on a project with Slicer generated nrrd files and after looking at the nrrd header I notice there is no info about the ijk to RAS matrix yet when I load that nrrd file into Slicer I obtain the matrix. So I think I figured out how to build it in code and I am looking for validation from someone who is more experienced than me. This is what I am doing:

  1. I read the nrrd header and obtain the space direction vectors (say a,b,c)

  2. I build a 3x3 matrix (A) where the columns correspond to a, b, and c.

  3. Given the t translation column vector (as defined in the nrrd space origin header), I can obtain world coordinates x for the index column vector v by:

x = A*v + t

  1. since my nrrd volume appears to be in LPS frame (as per the space header), I have to multiply x by the LPS to RAS transform.

Say that’s T = [-1 0 0; 0 -1 0; 0 0 1], so:

x_ras = T * x

Is this correct?

Now, the IJK to RAS direction matrix reported in Slicer (Volumes module) seems to be:

T*A

is this correct?

Thanks!

Almost correct. IJKToRAS is a homogeneous transformation matrix, which contains axis directions in the top-left 3x3 sub-matrix, origin in the first 3 values of 4th column, and [0,0,0,1] in the 4th row (so you don’t “add” the translation component but concatenate). In Matlab syntax:

ijk_to_lps = [[space_directions, space_origin]; [0 0 0 1]]
lps_to_ras = diag([-1 -1 1 1])
ijk_to_ras = lps_to_ras * ijk_to_lps

Ok, the confusion was because in the volumes module the matrix appears as a 3x3 matrix. But it doesn’t matter, anyways the 4x4 form is equivalent as you show since you are operating in homogeneous coordinates.
Thank you Andras.