Tuesday, May 8, 2012

NIfTI image handedness

The handedness of NIfTI images has given me some major headaches. Here's my take on the situation, in the hope others can avoid some of the problems I ran into. If you have other workarounds, solutions, or alternative explanations I'd love to hear them! Some of this is specific to R, some isn't.

Here is my illustration of the left- and right-handed orientation for brain imaging data. The difference is where the origin (voxel at 0,0,0) is located: does the i axis increase left-to-right or right-to-left?

Both orientations are valid in NIfTI files. There isn’t a flag in the NIfTI header to indicate whether the data in any particular image is in one handedness or the other; the qform and sform matrices are supposed to make this apparent.

My problems arose because I was reading left-handed images with oro.nifti and AnalyzeFMRI: the files are always read in as if they were right-handed, causing left-right flipping. This isn’t a matter of neurological vs. radiological convention: the flipped images are mirror-reversed, causing the left side of an overlay to appear on the right side of the anatomically template (i.e. the template and overlay do not agree on which side is left) if the images are output then viewed in a program such as MRIcron.

In my testing, image files can be read and written repeatedly from R without flipping: IF they are right-handed arrays. oro.nifti writes NIfTI files assuming the source R array is right-handed. NIfTI files are also read assuming they are right-handed, producing a flipped R array if they are not. 

My work-around has been to check the handedness of images whenever I read an image from a software package (spm, afni, etc) into R, and "manually" change the array to a right-handed orientation if it's not already. 

Here is R code and images showing the flipping.


  1. Thanks a lot for your afford - it seems to be taken care of this issue in the meantime - at least in the oro.nifti package. i ran into the same problem while plotting an nifti file with contour3d from the misc3d package. again, the x-axis is reversed.

    i 'fixed' this with reordering the x-axis in R:
    img <- readNifti(...)
    img <- img[nrow(img):1,,]

    it's not supposed to be written out to file again after this, it wouldn't be displayed correct in other programs.

    1. Excellent tip! As the commenter states, this code snippet doesn't rewrite the image files, but does flip the x-axis for display.