# Coordinate Transforms

Coordinate transforms lie at the heart of how MLR displays data on different base anatomies and how it keeps track of the location of ROIs. MLR coordinate transforms are based on the transformation information that is part of the Nifti image standard. You may want to also read the mrAlign manual.

## Nifti Qform and Sform

Nifti specifies two coordinate transformations in the header of every image file:

**Qform**is the coordinate transformation that takes the coordinates of the image into the coordinates of the magnet. It should be set in the data by the MRI image acquisition software.**Sform**in the context of MLR is a coordinate transformation that aligns the images to a standard reference, in our case the volume anatomy for the subject. It is usually set by mrAlign.

Both of these transformations have an associated code, qform_code or sform_code, that in MLR can be any one of:

**0**the transformation has never been set**1**a transformation into magnet coordinates**3**a transformation into Talairach coordinates

So, when your images come off of the magnet, they should start out with having their Qform set. This transformation then tells how to go from the coordinates of the image into the coordinates of the magnet.

What does this mean?

Well, if you have an image, then each voxel is labeled according to the [X_{img} Y_{img} Z_{img}] location of where it is in the matrix specifying the image:

The Qform gives us a transformation matrix (though internally stored as a quaternion), that can convert the [X_{img} Y_{img} Z_{img}] coordinate of the voxel in the image to the location in mm from the center of the bore [X_{mag} Y_{mag} Z_{mag}] where the center of that voxel lies.

Note how the magnet coordinates are in neurological coordinates. That is, the x dimension is such that the right of the brain is in the right of the image. Actually, it is more descriptive to say that the magnet coordinates use the LPI convention, because the first (x) dimension goes from Left to right, the second (y) dimension goes from Posterior to anterior and the third (z) dimension goes from Inferior to superior.

The transformation from image coordinates to magnet coordinats is done by multiplying the 4×4 affine transformation matrix specified by the Qform times the image coordinates [X_{img} Y_{img} Z_{img}]:

The Qform above is composed of a 3×3 portion that rotates and scales (specified by the rs's) and a 3×1 portion that translates (specified by the t's). Note that the coordinates have been made into **homogeneous** coordinates. This means that they have an added 1. This is a trick, the purpose of which is to allow us to do the translation and rotation in a single matrix multiplication.

The above Qform is really the combination of three transformations. A translation which specifies the distance in number of voxels to the center of the voxel at location [1 1 1]. A scaling which specifies the distance in mm between centers of voxels (this is usually the same as your voxel dimensions, unless, for instance, you put a gap between slices). And a rotation around the center of the bore of the magnet.

The units of the t_{ximg} are in voxels, the s's are in mm and the t_{mm} are in mm in the rotated reference frame.

There is one little hitch with this. The Qform specifies the transformation where the image coordinates specify the origin as (0,0,0) rather than (1,1,1) which is what Matlab uses. So, actually to get into the coordinates of the magnet from the coordinates in the image, you need to shift the origin by -1 first:

But, this is a detail. So we'll ignore it below.

## Aligning across sessions

So, now we know how to transform the coordinates of an image into the magnet coordinates; we can now transform from one images coordinates into another images coordinates as long as they are collected on the same day. For example, if we want to find the location of a voxel in our inplane anatomy in the EPI image collected from the same day, we can transform **through** the magnet coordinates:

The Qform of the inplane (Qform_{inplane}) transforms into the magnet coordinates and the **inverse** of the Qform_{EPI} takes you from the magnet coordinates to the EPI image coordinates:

Note that this does not assume that the inplane and EPI are taken with the same slice prescriptions. Since it uses the Qforms set by the magnet software, it will transform from inplane coordinates to EPI coordinates regardless of how the slices were placed in the inplane and EPI images. That is, as long as the subject didn't move their head in between the scans.

Now, what happens when we want to find where an inplane voxel comes from in the volume anatomy that was taken during a different session where the subject was not placed in exactly the same location in the magnet. In this case we have:

In this case, while the Qform_{inplane} and the Qform_{volume} take the inplane and volume coordinates into the magnet coordinates, there is a problem: the subject's head was **not** in the same location on the two days that the inplane and volume images were taken. Thus, we can't compute the transformation from the inplane to the volume coordinates. We need an alignment:

mrAlign computes the alignment between the inplane images and the volume images based on trying to find the best match between the two images. A good starting place for this, is to initialize the alignment from the headers, which means to use the Qform information. As noted above this won't give a perfect alignment because the subject's head was in a different location on the two days. But, it's a good starting place and mrAlign will find a good alignment from there. Now, we know the alignment from the inplane to the volume and from the volume to the magnet on the day the volume was taken (Qform_{volume}). So, we can compute the transformation from the inplane to the magnet coordinates **as if the head were in the same place on the day we acquired the inplane as the day we acquired the volume**. mrAlign puts that transform into the Sform_{inplane}.

So, the Sform contains the transformation of an image into the magnet space as if the head were in the same place as the day that the volume anatomy was taken. The Sform_{volume} is simply set to the Qform_{volume} since it already specifies the transformation of the volume coordinates to the magnet coordinates on the day that the volume was taken. (This is what mrAlign does when you set “Set Base Coordinate Frame” in the File menu).

So, now that we have the Sforms set, the transformation from the inplane to the volume is easy. It looks like this:

So as long as the Sform is set in the same way, we can now compute the transformation from the inplane image coordinates to the volume coordinates. That is, to find which voxel in the volume ([X_{volume} Y_{volume} Z_{volume}]) the voxel from the inplane image [X_{inplane} Y_{inplane} Z_{inplane}] comes from, the following computation is done:

You should never actually have to work with the Qforms and Sforms directly, though. If you ever need to explicitly do transformations, you should get the transformation matrices using viewGet. MLR will compute the correct transformations as discussed above using the Sforms. It will also take care of the shift by 1 (to account for Matlab's 1 based numbering system and the Qform/Sform's 0 based numbering system). There are transformations base2scan and scan2scan among many others. If you need the scan2base instead of the base2scan, simply use inv(base2scan). So to transform a coordinate in your base [X Y Z] to find its coordinate in the scan, you would do:

base2scan = viewGet(v,'base2scan'); scanCoord = base2scan * [X Y Z 1]';

You may want to round the scanCoords to the nearest voxel. Note that if you want to transform a lot of coordinates at the same time, X Y and Z should be **row** vectors (where the length is the number of coordinates). For example, to convert the points (1,5,1), (12,10,3) and (24,30,5):

X = [1 12 24]; Y = [5 10 30]; Z = [1 3 5]; baseCoords = [X;Y;Z;ones(1,length(X))]; base2scan = viewGet(v,'base2scan'); scanCoords = base2scan * baseCoords;

All coordinate transformations in MLR are handled in a similar way, be they from a base anatomy coordinate to a scan (epi) coordinate, or even from one scan coordinate collected on one day to a scan coordinate collected on a different day. ROIs inherit the Sform (and the voxel size) of the base anatomy that they were defined on, and then from there the transformations work in an analogous way. If you want to get ROI coordinates in the scan or in a base anatomy, you should always use MLR functions loadROITSeries or getROICoordinates which use the function xformROIcoords to transform the ROI coordinates. This is because xformROIcoords does some extra processing to try to maintain ROI volume across different voxel sizes.

-jg.