fsl.transform.flirt

This module contains functions for working with FLIRT affine transformation matrices. The following functions are available:

readFlirt

Reads a FLIRT matrix from a file.

writeFlirt

Writes the given FLIRT matrix to a file.

fromFlirt

Convert a FLIRT affine matrix into another affine.

toFlirt

Convert an affine matrix into a FLIRT matrix.

flirtMatrixToSform

Converts the given FLIRT transformation matrix into a transformation from the source image voxel coordinate system to the reference image world coordinate system.

sformToFlirtMatrix

Under the assumption that the given srcImage and refImage share a common world coordinate system (defined by their Nifti.voxToWorldMat attributes), this function will calculate and return a transformation matrix from the srcImage FSL coordinate system to the refImage FSL coordinate system, that can be saved to disk and used with FLIRT, to resample the source image to the space of the reference image.

FLIRT transformation matrices are affine matrices of shape (4, 4) which encode a linear transformation from a source image to a reference image. FLIRT matrices are defined in terms of FSL coordinates, which is a coordinate system where voxels are scaled by pixdims, and with a left-right flip if the image sform has a positive determinant.

FLIRT matrices thus encode a transformation from source image FSL coordinates to reference image FSL coordinates.

fsl.transform.flirt.readFlirt(fname)[source]

Reads a FLIRT matrix from a file.

fsl.transform.flirt.writeFlirt(xform, fname)[source]

Writes the given FLIRT matrix to a file.

fsl.transform.flirt.fromFlirt(xform, src, ref, from_='voxel', to='world')[source]

Convert a FLIRT affine matrix into another affine.

Given a FLIRT matrix, and the source and reference images with which the FLIRT matrix is associated, converts the matrix into an affine matrix which can transform coordinates from the source image from_ coordinate system to the reference image to coordinate system.

The from_ and to arguments specify the desired spaces that the returned affine should transform between. The default values (from_='voxel' and to='world' will generate an affine which transforms from voxels in the source image to world-coordinates in the reference image.

Valid values for the from_ and to arguments are:

  • voxel: The voxel coordinate system

  • fsl: The FSL coordinate system (voxels scaled by pixdims, with the X axis inverted if the image sform/qform has a positive determinant)

  • world The world coordinate system

See the Nifti class documentation and the Nifti.getAffine() method for more details.

Parameters:
  • xformnumpy array of shape (4, 4) containing a FLIRT transformation matrix.

  • srcNifti object, the xform source image

  • refNifti object, the xform reference image

  • from – Desired source coordinate system

  • to – Desired reference coordinate system

Returns:

numpy array of shape (4, 4) containing a matrix encoding a transformation from the source from_ to the reference to coordinate systems.

fsl.transform.flirt.toFlirt(xform, src, ref, from_='voxel', to='world')[source]

Convert an affine matrix into a FLIRT matrix.

Returns:

numpy array of shape (4, 4) containing a matrix encoding a transformation from the source from_ to the reference to coordinate systems.

Parameters:
  • srcNifti object, the xform source image

  • refNifti object, the xform reference image

  • fromxform source coordinate system

  • toxform target coordinate system

Returns:

A numpy array of shape (4, 4) containing a FLIRT transformation matrix from src to ref.

fsl.transform.flirt.flirtMatrixToSform(flirtMat, srcImage, refImage)[source]

Converts the given FLIRT transformation matrix into a transformation from the source image voxel coordinate system to the reference image world coordinate system.

To construct a transformation from source image voxel coordinates into reference image world coordinates, we need to combine the following:

  1. Source voxels -> Source scaled voxels

  2. Source scaled voxels -> Reference scaled voxels (the FLIRT matrix)

  3. Reference scaled voxels -> Reference voxels

  4. Reference voxels -> Reference world (the reference image sform)

Parameters:
  • flirtMat – A (4, 4) transformation matrix

  • srcImage – Source Image

  • refImage – Reference Image

fsl.transform.flirt.sformToFlirtMatrix(srcImage, refImage, srcXform=None)[source]

Under the assumption that the given srcImage and refImage share a common world coordinate system (defined by their Nifti.voxToWorldMat attributes), this function will calculate and return a transformation matrix from the srcImage FSL coordinate system to the refImage FSL coordinate system, that can be saved to disk and used with FLIRT, to resample the source image to the space of the reference image.

Parameters: