First we define the standard DICOM patient-based coordinate system. This is what DICOM means by x, y and z axes in its orientation specification. From section C.184.108.40.206.1 of the DICOM object definitions (2009):
If Anatomical Orientation Type (0010,2210) is absent or has a value of BIPED, the x-axis is increasing to the left hand side of the patient. The y-axis is increasing to the posterior side of the patient. The z-axis is increasing toward the head of the patient.
(we’ll ignore the quadupeds for now).
In a way it’s funny to call this the ‘patient-based’ coordinate system. ‘Doctor-based coordinate system’ is a better name. Think of a doctor looking at the patient from the foot of the scanner bed. Imagine the doctor’s right hand held in front of her like Spiderman about to shoot a web, with her palm towards the patient, defining a right-handed coordinate system. Her thumb points to her right (the patient’s left), her index finger points down, and the middle finger points at the patient.
The resulting pixel array then has size (‘Rows’, ‘Columns’), with row-major storage (rows first, then columns). We’ll call this the DICOM pixel array.
See wikipedia direction cosine for a definition of direction cosines.
From section C.220.127.116.11.1 of the DICOM object definitions (2009):
The Image Position (0020,0032) specifies the x, y, and z coordinates of the upper left hand corner of the image; it is the center of the first voxel transmitted. Image Orientation (0020,0037) specifies the direction cosines of the first row and the first column with respect to the patient. These Attributes shall be provide as a pair. Row value for the x, y, and z axes respectively followed by the Column value for the x, y, and z axes respectively.
From Section C.18.104.22.168.1 we see that the ‘positive row axis’ is left to right, and is the direction of the rows, given by the direction of last pixel in the first row from the first pixel in that row. Similarly the ‘positive column axis’ is top to bottom and is the direction of the columns, given by the direction of the last pixel in the first column from the first pixel in that column.
Let’s rephrase: the first three values of ‘Image Orientation Patient’ are the direction cosine for the ‘positive row axis’. That is, they express the direction change in (x, y, z), in the DICOM patient coordinate system (DPCS), as you move along the row. That is, as you move from one column to the next. That is, as the column array index changes. Similarly, the second triplet of values of ‘Image Orientation Patient’ (img_ornt_pat[3:] in Python), are the direction cosine for the ‘positive column axis’, and express the direction you move, in the DPCS, as you move from row to row, and therefore as the row index changes.
Further down section C.22.214.171.124.1 (RCS below is the reference coordinate system - see DICOM object definitions section 3.17.1):
The Image Plane Attributes, in conjunction with the Pixel Spacing Attribute, describe the position and orientation of the image slices relative to the patient-based coordinate system. In each image frame the Image Position (Patient) (0020,0032) specifies the origin of the image with respect to the patient-based coordinate system. RCS and the Image Orientation (Patient) (0020,0037) attribute values specify the orientation of the image frame rows and columns. The mapping of pixel location (i, j) to the RCS is calculated as follows:
- : The coordinates of the voxel (i,j) in the frame’s image plane in units of mm.
- : The three values of the Image Position (Patient) (0020,0032) attributes. It is the location in mm from the origin of the RCS.
- : The values from the row (X) direction cosine of the Image Orientation (Patient) (0020,0037) attribute.
- : The values from the column (Y) direction cosine of the Image Orientation (Patient) (0020,0037) attribute.
- : Column index to the image plane. The first column is index zero.
- : Column pixel resolution of the Pixel Spacing (0028,0030) attribute in units of mm.
- : Row index to the image plane. The first row index is zero.
- - Row pixel resolution of the Pixel Spacing (0028,0030) attribute in units of mm.
We stop to ask ourselves, what does DICOM mean by voxel (i, j)?
Isn’t that obvious? Oh dear, no it isn’t. See the DICOM voxel to patient coordinate system mapping formula above. In particular, you’ll see:
That is, if we have the DICOM pixel data as defined above, and we call that pixel_array, then voxel (i, j) in the notation above is given by pixel_array[j, i].
What does this mean? It means that, if we want to apply the formula above to array indices in pixel_array, we first have to apply a column / row flip to the indices. Say (sorry) is the affine to go from array indices in pixel_array to mm in the DPCS. Then, given above:
The (i, j), columns, rows in DICOM is rather confusing, so we’re going to rephrase the affine mapping; we’ll use for the row index (instead of above), and for the column index (instead of ).
Next we define a flipped version of ‘ImageOrientationPatient’, , that has flipped columns. Thus if the vector of 6 values in ‘ImageOrientationPatient’ are , then:
Now the first column of F contains what the DICOM docs call the ‘column (Y) direction cosine’, and second column contains the ‘row (X) direction cosine’. We prefer to think of these as (respectively) the row index direction cosine and the column index direction cosine.
Now we can rephrase the DICOM affine mapping with:
For later convenience we also define values useful for 3D volumes:
Let us say, we have a single DICOM file, or a list of DICOM files that we believe to be a set of slices from the same volume. We’ll call the first the single slice case, and the second, multi slice.
In the multi slice case, we can assume that the ‘ImageOrientationPatient’ field is the same for all the slices.
We want to get the affine transformation matrix that maps from voxel coordinates in the DICOM file(s), to mm in the DICOM patient coordinate system.
By voxel coordinates, we mean coordinates of form - the row, column and slice indices - as for the DICOM affine formula.
In the single slice case, the voxel coordinates are just the indices into the pixel array, with the third (slice) coordinate always being 0.
In the multi-slice case, we have arranged the slices in ascending or descending order, where slice numbers range from 0 to - where is the number of slices - and the slice coordinate is a number on this scale.
We know, from DICOM affine formula, that the first, second and fourth columns in are given directly by the (flipped) ‘ImageOrientationPatient’, ‘PixelSpacing’ and ‘ImagePositionPatient’ field of the first (or only) slice.
Our job then is to fill the first three rows of the third column of . Let’s call this the vector with values .
See also the definitions in DICOM affine formula. In addition
For the single slice case we just fill with - on the basis that the Z dimension should be right-handed orthogonal to the X and Y directions.
For the multi-slice case, we can fill in by using the information from , because is the translation needed to take the first voxel in the last (slice index = ) slice to mm space. So:
From this it follows that:
We may have the problem (see e.g. Sorting files into volumes) of trying to sort a set of slices into anatomical order. For this we want to use the orientation information to tell us where the slices are in space, and therefore, what order they should have.
To do this sorting, we need something that is proportional, plus a constant, to the voxel coordinate for the slice (the value for the slice index).
Our DICOM might have the ‘SliceLocation’ field (0020,1041). ‘SliceLocation’ seems to be proportianal to slice location, at least for some GE and Philips DICOMs I was looking at. But, there is a more reliable way (that doesn’t depend on this field), and uses only the very standard ‘ImageOrientationPatient’ and ‘ImagePositionPatient’ fields.
Consider the case where we have a set of slices, of unknown order, from the same volume.
Now let us say we have one of these slices - slice . We have the affine for this slice from the calculations above, for a single slice ().
Now let’s say we have another slice from the same volume. It will have the same affine, except that the ‘ImagePositionPatient’ field will change to reflect the different position of this slice in space. Let us say that there a translation of slices between and . If ( for slice ) is then for is given by:
and ‘ImagePositionPatient’ for is:
Remember that the third column of gives the vector resulting from a unit change in the slice voxel coordinate. So, the ‘ImagePositionPatient’ of slice - say slice - can be thought of the addition of two vectors , where is the position of the first voxel in some slice (here slice 1, therefore ) and is times the third colum of . Obviously can be negative or positive. This leads to various ways of recovering something that is proportional to plus a constant. The algorithm suggested in this ITK post on ordering slices - and the one used by SPM - is to take the inner product of with the unit vector component of third column of - in the descriptions here, this is the vector :
This is the distance of ‘ImagePositionPatient’ along the slice direction cosine.
The unknown terms pool into a constant, and the operation has the neat feature that, because the terms, by definition, sum to 1, the whole can be expressed as - i.e. it is equal to the slice voxel size () multiplied by , plus a constant.
Again, see derivations/spm_dicom_orient.py for the derivations.