Converting OASIS brains to NIFTI

The OASIS dataset consists of a number of T1-weighted mri brain scans, which has been kindly offered online at The dataset can be downloaded for free after accepting an academic agreement available at the website. The data was released in analyze 7.5 file format. This format, despite having been used by the neuroimaging community for many years, suffers from not including orientation information. The nifti format addresses this concern, but when converting, some care has to be taken to ensure that there are no left-right flips. Fortunately, for this dataset, during acquisition a capsule of vitamin E was placed next to the head of each subject, on the left side, which is very helpful to identify the correct side (Marcus et al., 2007).

There are still problems, though. The nifti format has not been fully implemented in all common software packages and worse, some packages interpret differently the information contained in the header. Images that look fine in fsl‘s FSLview or FreeSurfer‘s Freeview may look stretched or shrunken in spm for instance. And images that look fine in both, may still be oriented incorrectly in Mango. Although a bit cumbersome, the procedure below ensures that the oasis images can be converted from analyze to nifti in a way that it can correctly read and shown by Mango, fsl, FreeSurfer and spm. The procedure uses exclusively fsl tools:

  1. Convert to nifti:
    fslchfiletype NIFTI_GZ OAS1_0001_MR1_mpr-1_anon.hdr
  2. Make sure there is no undesired orientation information:
    fslorient -deleteorient OAS1_0001_MR1_mpr-1_anon.nii.gz
  3. Set the sform_code as 2, which is for “aligned anatomy”. Although this is still in native, not aligned space, it ensures that software will read them appropriately:
    fslorient -setsformcode 2 OAS1_0001_MR1_mpr-1_anon.nii.gz
  4. Set the sform as the following matrix:
    fslorient -setsform  0 0 -1.25 0  1 0 0 0  0 1 0 0  0 0 0 1  OAS1_0001_MR1_mpr-1_anon.nii.gz
  5. Swap the order of the data. Again, this isn’t really necessary, except to ensure that different applications will all read correctly:
    fslswapdim OAS1_0001_MR1_mpr-1_anon.nii.gz RL PA IS OAS1_0001_MR1_mpr-1_anon.nii.gz
  6. fsl tries to preserve orientation and, when the voxels are reordered, it modifies the header accordingly, resulting in no net transformation when seen with fsl tools. To resolve this, it’s necessary to change the header again, now the qform:
    fslorient -setqform -1.25 0 0 0  0 1 0 0  0 0 1 0  0 0 0 1  OAS1_0001_MR1_mpr-1_anon.nii.gz

These steps can be placed inside a simple loop within the shell for either Linux and Mac, like below (click here to download):


# Directory where the OASIS data is located

# Directory to save results

# Go to the directory with the data

# For each subject
for s in * ; do

  # Some feedback in the screen
  echo ${s}

  # Create directory to save the results, if not existing
  mkdir -p ${NIFTIDIR}/${s}

  # Directory of the original, raw data
  cd ${ROOTDIR}/${s}/RAW

  # For each acquisition
  for a in *.hdr ; do

    # Do each of the 6 steps described in the blog
    ${FSLDIR}/bin/fslchfiletype NIFTI_GZ ${a} ${NIFTIDIR}/${s}/${a%.hdr}
    ${FSLDIR}/bin/fslorient -deleteorient ${NIFTIDIR}/${s}/${a%.hdr}
    ${FSLDIR}/bin/fslorient -setsformcode 2 ${NIFTIDIR}/${s}/${a%.hdr}
    ${FSLDIR}/bin/fslorient -setsform  0 0 -1.25 0  1 0 0 0  0 1 0 0  0 0 0 1  ${NIFTIDIR}/${s}/${a%.hdr}
    ${FSLDIR}/bin/fslswapdim ${NIFTIDIR}/${s}/${a%.hdr} RL PA IS ${NIFTIDIR}/${s}/${a%.hdr}
    ${FSLDIR}/bin/fslorient -setqform -1.25 0 0 0  0 1 0 0  0 0 1 0  0 0 0 1  ${NIFTIDIR}/${s}/${a%.hdr}

echo "Done!"

The reference for the oasis dataset is:

8 thoughts on “Converting OASIS brains to NIFTI

  1. This really helped!

    We downloaded 850 T1 images from oasis3 (in nii.gz format). 720 of these where run through fslreorient2std and bet without problem but 130 of them express similar issues as the oasis1 dataset: That you can’t even run fslresorient2std since there are header elements missing.

    Running the same script as above on these images fixes that issue, you can at least run fslreorientstd on them. However the orientations of the images does not become correct. In fsleyees the view order is correct (sag, cor, ax) but the sag has the nose to the left (should be right) and the axial is flipped upside down.

    Is there a way to edit the script above to also fix these oasis3 datasets?

    Thanks again!

    • Hi Robin,

      It’s can be done, no doubt, but you will need to tweak. The solution shown here was also after some trial and error.

      That said, for some participants some fields seem to be missing, you may want to contact the OASIS folks and ask them to check is somehow the files haven’t been corrupted. They may also be interested in (re-)releasing in BIDS using a recent converter (such as dcm2niix), which could also obviate many such issues (though not necessarily…).

      Sorry not being able to help in greater detail. I haven’t been working with this dataset in a while…


      • Thanks for your answer!

        While your script saved the oasis1 part it turned out that the troublesome subset of oasis3 could be saved by some afni commands:

        3dZeropad -prefix padded.nii.gz -I 50 -S 50 -A 50 -P 50 -L 50 -R 50 input.nii.gz

        3drotate -rotate ’90A’ ’90L’ ‘0S’ -prefix output.nii.gz padded.nii.gz

        I.e. zeropad to avoid getting brains outside the fov and then performing the rotations. Then I fslreorient2std and then bet.

        Thanks again:)

        • Interesting-
          I have been trying to go the other way down the road and convert the OASIS3 images back to DICOM. The orientation issues described here have been playing havoc with model orientation in my 3D renderer and defacer.
          I have been looking at this for the last few days trying to work out where my code has gone wrong – thinking that my nifti to dicom converting code was introducing some undue image flips somewhere.
          Annoyingly, in windows there are very few NIFTI viewers- I only seem to be able to use Mango, so I can’t check with multiple viewers.

          Good to know which side the vitamin E tablet is supposed to be…

          Even better to know that the original data is known to be at issue!

        • Mango is quite reliable, but it can only go as far as the file is well formed. I would at all costs avoid converting back to DICOM. I think it is never worth the effort…

  2. Never is a strong word, but I agree that it should be avoided.
    In this case I needed a fair number of DICOM datasets to help develop a defacing algorithm- It is intended to run on DICOM files. I was unable to get hold of original DICOM datasets so I used Nifti files instead- creating it’s own set of problems.

    Is there a standard way to deal with NII files with ‘wrong’ orientation data? Defacing is clearly very orientation-sensitive…

    • Hi Oliver,

      I don’t think there is a standard way. However, there is (generally) a finite number of ways in which the data can be flipped or axes swapped leading to an orientation that is amenable to registration to a standard brain, in which defacing is simpler. Maybe a couple of trial-and-error…

      AFNI has a tool for L-R flips (I haven’t tested):,151714,151714

      All the best,


      • The trial and error approach is pretty much what I have used to get it working in the first place.

        I even incorporated the JSON data accompanying many of the OASIS and all of the SIMON acquisitions- they often contain the original DICOM image orientation (patient) tag data.

        What I have works quite nicely if you don’t consider volume location within the scanner, and saves individual slices out to individual DICOM files. In other words, it’s fine so long as I don’t compare volumes.

        The flip is actually quite easy to perform- It is a one-liner in python (numpy). The question is _when_ to apply.

        Given the problematic volumes are few in number, I have elected to ignore them for the moment. Perhaps I’ll batch process them later to ‘correct’ the nii header, in a sadly manual way.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s