Referentials



Here is a description of the coordinates sytems used in Aims and Anatomist, and what I have understood of how SPM handles its referentials.

AIMS and Anatomist

Internally

Anatomist uses AIMS to handle its referentials so behaves exactly the same way.

Aims tries to work internally in an image-specific referential, but with always the same orientation. This orientation is axial with the following coordinates system:

  • X axis: right to left
  • Y axis: front to back
  • Z axis: top to bottom
  • origin: the center of the first voxel: the voxel in the top, right, front corner
  • If you look at it you will realize that this referential is in radiological convention ans is indirect. This is, in my opinion, a bad choice, but it's a bit too late to change.

    Once loaded in memory, all voxels should be organized in this order. As a consequence, images in Anatomist are always displayed in radiological mode, whatever the actual orientation of data on disk.

    Externally

    Images on disk, depending on their format and acquisition modes, are not necessarily in this orientation. When a different orientation is detected, images are flipped in memory at load-time to fit the standard AIMS orientation. And when images are written back to disk, they may also be flipped back according to the specific format needs.

    Transformations

    By default, AIMS doesn't apply any transformation other than flipping images at load time as described just before.

    But transformations can be provided in some Aims commands or loaded in Anatomist to apply coordinates changes. Then coords transformations are applied on the fly when processing or displaying data which are not in the same referential.

    There is no special referential (such as a common central working referential).

    Transformation files used by AIMS (.trm files) are ASCII files looking like this:

    Tx Ty Tz
    R11 R12 R13
    R21 R22 R23
    R31 R32 R33
    
    Tx, Ty, Tz are the translation components while the Rij coefficients are the linear matrix part. When used, these coefficients are applied as a "standard" 4x4 transformation matrix:
        [ R11  R12  R13  Tx ]
    M = [ R21  R22  R23  Ty ]
        [ R31  R32  R33  Tz ]
        [   0    0    0   1 ]
    

    SPM

    Internally

    Internally, SPM thinks things is always in the same orientation, which is also axial but with different axes:

  • X axis: left to right
  • Y axis: back to front
  • Z axis: bottom to top
  • origin: the center of the voxel specified by the origin field of the SPM image header. This origin is specified in voxels and starts counting from 1 (not 0) like a matlab array index does.
  • This is a neurological convention orientation. The axes happen to be exactly the contrary of what is done in AIMS. Bad luck... But this referential is direct so cannot be considered worse than in AIMS...

    Working on the coordinate transformations for years and regularly getting headaches from it, I am still not 100% sure of what I say here, so if I'm wrong, please correct me by sending a message on BrainVisa forum (http://brainvisa.info/forum/). Especially, I'm not sure that SPM99 and SPM2 really use the same referentials.

    Externally

    SPM handles input images in two different orientations: axial radiological and axial neurological orientations. This orientation is not specified in SPM format image files, so you have to tell how they are oriented. This is done in SPM by a flipping flag set somewhere in SPM defaults configuration (default.analyze.flip in SPM2).

    This flipping flag has changed in form and meaning between SPM99 and SPM2.

    As I have understood:

  • SPM99:
  • SPM2:
  • Transformations

    SPM uses a common central referential to work in. Every image can provide a transformation matrix to this referential. Such a transformation is specified in an optional .mat file with the same name as the SPM format image. If this transformation is not provided, then the origin translation is considered to be the only transformation needed to reach the central referential. If the .mat file is specified, information contained in it overrides some of the header information (including the origin).

    Normalization files (*_sn3d.mat for SPM99, *_sn.mat for SPM2) contain transformations to the referential of a normalization template (either a standard one provided with the SPM software distribution, or a custom user-made one). This transformation contains an affine part (matrix), and optionally, depending on the normalization type, a non-linear part (coefficients on a functions base as far as I know but I don't know much about this part). Information about the input and template images are also included (dimensions, voxel sizes and origins).

    Normalized images are in the referential of the normalization template used, but not necessarily with the same bounding box, resolution and field of view.

    SPM99 and SPM2 use normalization files with different names and different contents. They are not compatible, even if there is some common and similar information in them.

    Changing between SPM and AIMS

    Due to the different internal orientations of the coordinate systems, going from SPM to AIMS and vice versa causes some serious problems.

    Normalization

    SPM normalization files are in matlab (.mat) format. AIMS cannot read the proprietary matlab format, so such files cannot be directly imported in AIMS.

    However, BrainVisa can use Matlab (if Matlab is installed) and convert SPM matrices to AIMS .trm format. Only the affine part can be converted, because AIMS only use matrices for transformations, and non-linear information cannot fit into a matrix.

    As the orientation is different in SPM and AIMS, a transformation to a template image is not the same as a transformation to a normalized image with a different field of view. So, when converting SPM normalization matrices, the normalized image must be also provided, otherwise BrainVisa can only give the transformation to the normalization template. Note the difference.

    Issues

    Unnormalized SPM images

    It is impossible to guess the orientation of such images if you don't know how they were acquired. This means you have to manually specify their orientation, either for all images in SPM, or in BrainVisa when importing them into a database. BrainVisa tags them so it knows everything afterwards and avoids mistakes. SPM does not.

    Normalized SPM images

    Normalizing the same image in radiological orientation with SPM99 and SPM2 results in normalized images in different orientations. When you import normalized images coming from another site, you have to know whether they have been normalized by SPM99 or by SPM2, and if the original image was in radiological or neurological orientation.

    I think the normalization file (*_sn3d.mat for SPM99, or *_sn.mat for SPM2) contains enough information to retreive the orientation of input and template images, so can disambiguate the situation.

    Reading SPM origin

    The origin field of SPM format is the position of the referential origin, in voxels and starting from 1 (not from 0). In fact it's a matlab array index. So it is given in the orientation of the image on disk. AIMS flips SPM images on several axes when loading them, so the origin information also has to be flipped. Flipping it needs to know the image dimensions.

    AIMS referentials have their origin in the first voxel, (almost) in the corner of the image, and normally don't use the SPM origin. But the origin information is read and maintained. Anatomist can, if asked for, make a transformation going from AIMS origin (corner) to the SPM origin. This allows to display several aligned SPM images in Anatomist with the correct correspondance. However after this translation, the coordinates are still in AIMS orientation (radiological and indirect), not in SPM, so the coordinates do not correspond to what they are in SPM.

    To compare coordinates of SPM images in Anatomist and SPM, another transformation has to be applied in Anatomist, with all the flips included. But this would result to a change in the referential orientation (it would become direct) and would produce some strange 3D rendering problems so it is not recommended to do so it you are not forced to.

    Other formats (GIS etc)

    Up to now, GIS images are considered being always in AIMS orientation. No flips are applied. Future releases may take orientation information into account.

    The new Minc IO plugin takes orientation into account and flips data accordingly when reading / writing files.

    I am not sure if other formats (Dicom, Ecat...) can specify an image orientation or not. If they do, the current release of AIMS will probably not take it into account.

    Technical details

    SPM normalization matrices conversion to AIMS world

    SPM99 and SPM2 don't use the same format of normalization files, but both provide more or less the following information:

  • An affine transformation matrix, called Affine, transforming coordinates from the template space to the input space, in voxels
  • A voxels-to-mm transformation matrix for the input image, transforming voxels of the image into a mm position in the SPM internal orientation, taking the origin into account. This matrix is called VF.mat in SPM2 and also performs flipping, and called MF in SPM99 but doesn't seem to contain the flipping information.
  • Another voxel-to-mm matrix for the template image: VG.mat in SPM2, or MG in SPM99.
  • Input and template image dimensions in voxels and a bit more: VF.dim and VG.dimin SPM2, or Dims in SPM99.
  • Notations:
  • 3 images: input (I call it Anatomy to be clearer), template, and normalized images. I use the suffixes A, T and N for coordinates on these 3 images.
  • I use the same name for a given referential and coordinates in this referential: for instance RAA is both the AIMS referential of the anstomical image and a coordinates vector in it. I don't bother about standardized math notations: I don't remember them and haven't been using math anymore for many years. Don't ask me too much.
  • Images dimensions and origins:
  • AIMS referentials:
  • SPM referentials:
  • Transformation matrices:
  • Resolution:

    We want to calculate AIMS, provided Affine, MA and MT

    Directly:

    RSA = Affine * RST, or RST = Affine^-1 * RSA
    RAN = AIMS * RAA
    RSCA = MA * RSA
    RSCT = MT * RST
    RSCA = ASA * RAA
    RSCN = ASN * RAN
    RSCN = TN * RSCT
    

    Combining them:

    RSCN = ASN * AIMS * RAA
    AIMS * RAA = ASN^-1 * RSCN
               = ASN^-1 * TN * RSCT
               = ASN^-1 * TN * MT * RST
               = ASN^-1 * TN * MT * Affine^-1 * RSA
               = ASN^-1 * TN * MT * Affine^-1 * MA^-1 * RSCA
               = ASN^-1 * TN * MT * Affine^-1 * MA^-1 * ASA * RAA
    AIMS = ASN^-1 * TN * MT * Affine^-1 * MA^-1 * ASA
    

    ASA is a combination of an origin translation OA, and axes flips FA from AIMS standard ref to SPM standard ref:

         [ 1  0  0  -Ox ]
    OA = [ 0  1  0  -Oy ]
         [ 0  0  1  -Oz ]
         [ 0  0  0   1  ]
    
    O = ( orgA - 1 ) .* vsA
    
         [ -1   0   0  Tx ]
    FA = [  0  -1   0  Ty ]
         [  0   0  -1  Tz ]
         [  0   0   0   1 ]
    
    T = ( dimA - 1 ) .* vsA
    
    ASA = OA * FA
    

    Similarly, for the normalized image:

    ASN = ON * TN
    
    using the normalized image sizes. To get the transformation to the template image only, use the sizes of the template image instead.