Saving Individual Labels From FreeSurfer’s aseg – Updated

In a previous post I wrote about converting FreeSurfer aseg (sub-cortical segmentation) labels to volumetric masks. I demonstrated a graphical way of pulling out individual labels; however, it can be fairly tedious. There are around 50 different labels in the aseg file so sorting through them can take a while. There is a much faster – and scriptable – method for pulling out individual labels as ROIs.

Let’s assume that you know the numbers of the labels you want, for this example I’ll pull out the left caudate, which is label 11 in aseg. The commands assume you have FSL installed (as well as FreeSurfer).

We’ll use the program fslmaths, which is a very general image calculator.

First you will need an aseg file as a NIfTI.

Navigate to your FreeSurfer subjects directory:

cd $FREESURFER_HOME/subjects/

Then navigate to the participant’s mri directory you want

cd s001/mri

Then run: mri_convert aseg.mgz aseg.nii.gz

Alternatively, you can simply create the aseg NIfTI file by typing in the full paths to the input and output files when running mri_convert

For example: mri_convert $FREESURFER_HOME/subjects/s001/mri/aseg.mgz /[path to working directory]/aseg.nii.gz

However, that aseg file is in FreeSurfer space. If you want it in T1 native space (or some other space, diffusion for example) you could run something like this:

mri_convert -rl $FREESURFER_HOME/subjects/s001/mri/rawavg.mgz -rt nearest $FREESURFER_HOME/subjects/s001/mri/aseg.mgz /[path to working directory/s001_aseg2raw.nii.gz

Generally I apply a transformation (registration matrix) from Freesurfer space (conformed) to target space (diffusion, native, fMRI) on the whole aseg or aparc+aseg file (use nearest neighbor interpolation) with flirt -applyxfm or something similar (i.e., I don’t use mri_convert – rl (reslice like) and then do the next steps.

Now you are ready to pull out the appropriate ROI(s) – this example gives you the left caudate (in native T1 space).

fslmaths /[path to aseg file]/s001_aseg2raw.nii.gz -uthr 11 -thr 11 /[path to working directory]/ROIS/l_caudate_freesurfer_rawavg.nii.gz

This runs this command with an upper (-uthr) and lower (-thr) threshold of 11 to zero out everything except the 11th label (the left caudate).

Then you can run fslmaths to turn the mask file into 0s and 1s (from its 0s and 11s in the case of the left caudate – modify this command as appropriate). The -div 11 option tells the program to divide the mask file (which is currently all 11s and 0s) by 11, which will result in a mask with just 0s and 1s. Some imaging programs do not like ROIs (binary masks) with values other than 0s and 1s:

fslmaths l_caudate_freesurfer_rawavg.nii.gz –div 11 l_caudate_freesurfer_rawavg.nii.gz

You don’t have to write over the mask file if you do not want to; feel free to create a new file just in case something goes wrong. You can delete any temp files later.

Here are screenshots of the whole process.

Before pulling out the left caudate
After pulling out just the left caudate
This whole process takes only a couple minutes to run and, as I wrote earlier, it is scriptable so you could run a whole batch of brains in a matter of minutes.

About Jared Tanner

I have a PhD in Clinical and Health Psychology with an emphasis in neuropsychology at the University of Florida. I previously studied at Brigham Young University. I am currently a Research Assistant Professor at the University of Florida. I spend the bulk of my research time dealing with structural magnetic resonance images of the brain. My specialty is with traditional structural MR images, such as T1-weighted and T2-weighted images, as well as diffusion weighted images. I also look at the cognitive and behavioral functioning of individuals with PD and older adults undergoing orthopedic surgery. Funding for the images came from NINDS K23NS060660 (awarded to Catherine Price, University of Florida). Brain images may not be used without my written permission (grant and software requirements).