# Docs on qform and sform

## qform, sform

- Links to FAQ questions
- Recommended usage for qforms and sforms in applications
- Illustration about the qform field
- Links to Bboard questions
- See also

### nifti1.h header documentation

/

---------------------------------------------------------------------------/

/* 3D IMAGE (VOLUME) ORIENTATION AND LOCATION IN SPACE:

---------------------------------------------------

There are 3 different methods by which continuous coordinates can

attached to voxels. The discussion below emphasizes 3D volumes, and

the continuous coordinates are referred to as (x,y,z). The voxel

index coordinates (i.e., the array indexes) are referred to as (i,j,k),

with valid ranges:

i = 0 .. dim[1]-1

j = 0 .. dim[2]-1 (if dim[0] >= 2)

k = 0 .. dim[3]-1 (if dim[0] >= 3)

The (x,y,z) coordinates refer to the CENTER of a voxel. In methods

2 and 3, the (x,y,z) axes refer to a subject-based coordinate system,

with

+x = Right +y = Anterior +z = Superior.

This is a right-handed coordinate system. However, the exact direction

these axes point with respect to the subject depends on qform_code

(Method 2) and sform_code (Method 3).

- B.: The i index varies most rapidly, j index next, k index slowest.

Thus, voxel (i,j,k) is stored starting at location

(i + j*dim[1] + k*dim[1]*dim[2]) * (bitpix/8)

into the dataset array.- B.: The ANALYZE 7.5 coordinate system is

+x = Left +y = Anterior +z = Superior

which is a left-handed coordinate system. This backwardness is

too difficult to tolerate, so this NIFTI-1 standard specifies the

coordinate order which is most common in functional neuroimaging.- B.: The 3 methods below all give the locations of the voxel centers

in the (x,y,z) coordinate system. In many cases, programs will wish

to display image data on some other grid. In such a case, the program

will need to convert its desired (x,y,z) values into (i,j,k) values

in order to extract (or interpolate) the image data. This operation

would be done with the inverse transformation to those described below.- B.: Method 2 uses a factor
`qfac`

which is either -1 or 1; qfac is

stored in the otherwise unused pixdim[0]. If pixdim[0]=0.0 (which

should not occur), we take qfac=1. Of course, pixdim[0] is only used

when reading a NIFTI-1 header, not when reading an ANALYZE 7.5 header.- B.: The units of (x,y,z) can be specified using the xyzt_units field.
METHOD 1 (the "old" way, used only when qform_code = 0):

-------------------------------------------------------

The coordinate mapping from (i,j,k) to (x,y,z) is the ANALYZE

7.5 way. This is a simple scaling relationship:x = pixdim[1] * i

y = pixdim[2] * j

z = pixdim[3] * kNo particular spatial orientation is attached to these (x,y,z)

coordinates. (NIFTI-1 does not have the ANALYZE 7.5 orient field,

which is not general and is often not set properly.) This method

is not recommended, and is present mainly for compatibility with

ANALYZE 7.5 files.METHOD 2 (used when qform_code > 0, which should be the "normal" case):

---------------------------------------------------------------------

The (x,y,z) coordinates are given by the pixdim[] scales, a rotation

matrix, and a shift. This method is intended to represent

"scanner-anatomical" coordinates, which are often embedded in the

image header (e.g., DICOM fields (0020,0032), (0020,0037), (0028,0030),

and (0018,0050)), and represent the nominal orientation and location of

the data. This method can also be used to represent "aligned"

coordinates, which would typically result from some post-acquisition

alignment of the volume to a standard orientation (e.g., the same

subject on another day, or a rigid rotation to true anatomical

orientation from the tilted position of the subject in the scanner).

The formula for (x,y,z) in terms of header parameters and (i,j,k) is:[ x ] [ R11 R12 R13 ] [ pixdim[1] * i ] [ qoffset_x ]

[ y ] = [ R21 R22 R23 ] [ pixdim[2]j ] + [ qoffset_y ]pixdim[3] * k ] [ qoffset_z ]

[ z ] [ R31 R32 R33 ] [ qfacThe qoffset_* shifts are in the NIFTI-1 header. Note that the center

of the (i,j,k)=(0,0,0) voxel (first value in the dataset array) is

just (x,y,z)=(qoffset_x,qoffset_y,qoffset_z).The rotation matrix R is calculated from the quatern_* parameters.

This calculation is described below.The scaling factor qfac is either 1 or -1. The rotation matrix R

defined by the quaternion parameters is "proper" (has determinant 1).

This may not fit the needs of the data; for example, if the image

grid is

i increases from Left-to-Right

j increases from Anterior-to-Posterior

k increases from Inferior-to-Superior

Then (i,j,k) is a left-handed triple. In this example, if qfac=1,

the R matrix would have to be[ 1 0 0 ]

[ 0 -1 0 ] which is "improper" (determinant = -1).

[ 0 0 1 ]## If we set qfac=-1, then the R matrix would be

[ 1 0 0 ]

[ 0 -1 0 ] which is proper.

[ 0 0 -1 ]This R matrix is represented by quaternion [a,b,c,d] = [0,1,0,0] (which encodes a 180 degree rotation about the x-axis).

METHOD 3 (used when sform_code > 0):

-----------------------------------

The (x,y,z) coordinates are given by a general affine transformation

of the (i,j,k) indexes:x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3] y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3] z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3]

The srow_* vectors are in the NIFTI_1 header. Note that no use is

made of pixdim[] in this method.WHY 3 METHODS?

--------------

Method 1 is provided only for backwards compatibility. The intention

is that Method 2 (qform_code > 0) represents the nominal voxel locations

as reported by the scanner, or as rotated to some fiducial orientation and

location. Method 3, if present (sform_code > 0), is to be used to give

the location of the voxels in some standard space. The sform_code

indicates which standard space is present. Both methods 2 and 3 can be

present, and be useful in different contexts (method 2 for displaying the

data on its original grid; method 3 for displaying it on a standard grid).In this scheme, a dataset would originally be set up so that the

Method 2 coordinates represent what the scanner reported. Later,

a registration to some standard space can be computed and inserted

in the header. Image display software can use either transform,

depending on its purposes and needs.In Method 2, the origin of coordinates would generally be whatever

the scanner origin is; for example, in MRI, (0,0,0) is the center

of the gradient coil.In Method 3, the origin of coordinates would depend on the value

of sform_code; for example, for the Talairach coordinate system,

(0,0,0) corresponds to the Anterior Commissure.

### DEFINED CODES

/* [qs]form_code value:

/ /x,y,z coordinate system refers to:/-----------------------

// /---------------------------------------*/#define NIFTI_XFORM_UNKNOWN 0 /

! Arbitrary coordinates (Method 1)./#define NIFTI_XFORM_SCANNER_ANAT 1 /

! Scanner-based anatomical coordinates/#define NIFTI_XFORM_ALIGNED_ANAT 2 /

! Coordinates aligned to another file's, or to anatomical "truth"./#define NIFTI_XFORM_TALAIRACH 3 /

! Coordinates aligned to Talairach-Tournoux Atlas; (0,0,0)=AC, etc./#define NIFTI_XFORM_MNI_152 4 /

! MNI 152 normalized coordinates./