# Aligning images with an SBA atlas template

A brain atlas is a collection of measurements, annotations and analysis results from one or more brains, registered to a common coordinate system. An SBA atlas template augments a given brain atlas with standard meta data to define all properties of the atlas that are relevant for making it interoperable with other atlases and with the SBA ecosystem. The common coordinate system is often referred to as reference space, and the measurements that come natively with the atlas are called reference data. We distinguish two steps in the alignment of images with an SBA atlas template:

1. Registration of the image to the reference space, involving linear or nonlinear deformation of the image so that it best coincides with the reference data. For nonlinear deformations, this step includes the resampling of the deformed image in the common coordinate system. Many software packages are available for this part of the registration process (elastix, ANTS, and many more). SBA supports this process by having reference MRI volumes available for download for most atlases. Many publicly available datasets are already registered to an atlas; for these images this step can be skipped.

2. Alignment of the registered image with the SBA atlas template. This step is necessary because the atlas-registered image of step 1 may still be incompatible with SBA Composer in terms of its anatomical orientation, choice of length unit, choice of origin and voxel alignment. The remainder of this section discusses this step.

The file that results from step 1 contains an image (3-dimensional array of values) and an affine matrix that transforms coordinates from voxel indices $[i,j,k]$ to real-world coordinates. An excellent introduction to affine transformations from a neuro-imaging perspective is published as part of the nibabel documentation. There are four aspects that may cause the atlas-registered image to deviate from the coordinate system used by SBA Composer.

1. The anatomical orientation of the axes. Different atlas providers have different preferences. In SBA templates the brain is always in RAS+ orientation but when exchanging data, alternative orientations can be specified.
2. The unit length. In SBA templates, real-world coordinates are expressed in millimeters. When exchanging data, alternative length units can be specified.
3. The choice of origin. The origin, or coordinate $[0,0,0]$ in reference space, can either be chosen to represent an anatomical landmark (such as anterior commissure or bregma), or it can be set to a 'convenience landmark', such as the corner or center of the defining MRI image. Each SBA template defines a list of origin landmarks that may be used by imported data sets. One of these, preferrably an anatomical landmark, is chosen as the default.
4. The voxel alignment. Normally, a voxel coordinate $[i,j,k]$ refers to the center of that voxel. In some cases it is useful to have $[i,j,k]$ refer to the its corner.

When importing an image in SBA Composer, a dialog is presented to adjust each of the four above items. The following paragraphs discuss several items in detail.

## Anatomical orientation

Every mammalian brain, positioned with the eyes looking straight ahead, can be assigned a Left-Right axis, a Posterior-Anterior axis and an Inferior-Superior axis. These six orientations are coded by their first character, resulting in the set {L, R, P, A, I, S}. When a 3-dimensional brain image is acquired, each of its three dimensions can be assigned an orientation code that best matches the direction that it points to. This results in a three-letter orientation code, such as 'RAS'. The affine transformation that transforms the image from voxel space to real-world space may rotate the image, and therefore there are separate orientation codes for the image before and after the transformation.

Most neuroimaging file formats have fields or conventions to specify anatomical orientation. In Nifti-1 files for example, the image after transformation is required to be aligned to RAS orientation. However, in practice this requirement is often ignored. SBA Composer provides an input dialog in which the orientation before transformation can be specified. If it is not RAS, a rotation is applied to swap the axes to RAS orientation.

## Voxel alignment

As explained in the nibabel documentation, a raw image lives in a voxel space that is spanned by its three discrete voxel dimensions. The affine transformation to real-world space maps the center of each voxel to a real-world coordinate. In some cases it is desired to align not the center but the corner of a voxel to a real-world coordinate. This is used when aligning a corner voxel to the corner of the bounding box of an reference image.

Internally, SBA Composer handles the two cases as follows. Two types of voxel alignment are defined:

1. center alignment, where voxel coordinate $[i,j,k]$ represents the center of voxel $[i,j,k]$. We denote this alignment with subscript M (middle).
2. corner alignment, where voxel coordinate $[i,j,k]$ represents the corner of voxel $[i,j,k]$. We denote this alignment with subscript L (lower/left).

Corner refers to the corner in the negative direction of each axis, so that we can relate a center-aligned coordinate $x_M$ to a corner-aligned coordinate $x_L$ by:

$$x_L = x_M + \begin{bmatrix} 0.5 \\ 0.5 \\ 0.5 \end{bmatrix}$$

To view and combine images in atlas space, an affine transform is needed that projects voxels to real-world atlas space coordinates. We denote this transform as the augmented matrix $(A|\vec{b})$, where $A$ is a 3x3 linear transformation matrix and $b$ is a 3-element translation vector. If a transformation $(A|\vec{b}_L)$ is defined for corner-aligned voxels, we can easily modify it to work for center-aligned voxels.

\begin{align} \vec{y} &= A \vec{x}_L + \vec{b}_L \\ &= A \left( \vec{x}_M + \begin{bmatrix} 0.5 \\ 0.5 \\ 0.5 \end{bmatrix} \right) + \vec{b}_L \\ &= A \vec{x}_M + \left( \vec{b}_L + A \begin{bmatrix} 0.5 \\ 0.5 \\ 0.5 \end{bmatrix} \right) \end{align}

We can define a $\vec{b}_M$ that implements this change:

$$\vec{b}_M = \vec{b}_L + A \begin{bmatrix} 0.5 \\ 0.5 \\ 0.5 \end{bmatrix}$$

The affine matrix $(A|\vec{b}_M)$ can then be applied in a center-alignment context.

## The origin landmark

While traditional histology-based atlases always defined a stereotaxic coordinate space with a carefully selected origin, such as 'bregma' or 'interaural', this is much less the case for MRI-based atlases. Sometimes the origin is implicitly defined as one of the corners of the volume covered by the MRI, and in other cases it is defined as coinciding with an anatomical landmark such as the anterior commissure. When there are multiple possible choices of origin, SBA picks one as its default and provides a list of alternative origin landmarks.

Internally, when a dataset is imported with a non-default origin landmark, SBA Composer adjusts the affine matrix as follows to switch between origins. Let $y_1$ be the real-world coordinate of origin landmark 1, expressed in SBA's default-landmark space, and $y_2$ the same for origin landmark 2, and let $(A|\vec{b}_1)$ be the affine matrix that is valid in landmark-1 space. In that space, the voxel $\vec{x}_1$ that projects to $\vec{0}$ is implicitly given by

$$\vec{0} = A \vec{x}_1 + \vec{b}_{1} \textrm{.}$$

In default-landmark space, $x_1$ projects to $y_1$ instead of $\vec{0}$:

$$\vec{y}_1 = A \vec{x}_1 + \vec{b} \textrm{.}$$

For landmark 2, the voxel $\vec{x}_2$ that projects to it is implicitly defined in

$$\vec{y}_2 = A \vec{x}_2 + \vec{b} \textrm{.}$$

Finally, in landmark-2 space, $\vec{x}_2$ projects to zero:

$$\vec{0} = A \vec{x}_2 + \vec{b}_{2} \textrm{,}$$

After eliminating the unknown variables $\vec{x}_1$, $\vec{x}_2$ and $\vec{b}$ from the above three equations, $\vec{b}_{2}$ is given by:

$$\vec{b}_{2} = \vec{b}_1 + \vec{y}_1 - \vec{y}_2 \textrm{.}$$

Before applying this computation, $\vec{b}_1$ must be corrected (if necessary) to have the same voxel alignment as $\vec{b}_2$.