Saving Individual Labels From FreeSurfer’s aseg – Method 2

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

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 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 Postdoctoral Associate 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).

9 comments on “Saving Individual Labels From FreeSurfer’s aseg – Method 2

  1. Pingback: Converting FreeSufer aseg Labels to Individual Volume Masks

  2. Hi
    I was wondering whether it’s possible to get the talairach coordinates associated with each label in the segmentation. I use BV, not FSL, and I would like to use Freesurfer’s segmentation for analysis (in Matlab). If I could somehow create a text file with the talairach coordinates of each label (or of a specific label, by label number), it would be great.

  3. Hi,

    I tried using the indices from the Freesurfer Color LUT to create a ROI not listed in aseg.stats. However, the ROI is coming up empty (no regions designated as 1s or white). If the index is listed in aseg.stats, the ROI comes out fine. Do you know how the Freesurfer LUT is used to determine which regions get segmented in aseg.mgz?

    Thanks in advance,


  4. Thanks for the question Vy. Are you creating a new ROI from scratch or did you combine aseg ROIs to make a new one?

  5. Hi Jared,

    This is a great tutorial and saved me a lot of work. Now that I have a mask for my ROI, I am trying to get a ascii list of coordinates for each voxel in my mask. How would you go about doing that?

  6. Hi Jared,

    I’m a little confused by the discussion of T1 native space…I’m using FS 4.5 and my T1.mgz file has the same dimensions as my aseg.mgz (256x256x256), which is different from the rawavg.mgz file (256x256x176). I’m actually a little unsure of what anyone means when they refer to the various kinds of spaces, and I haven’t been able to find a good explanation on the Freesurfer wiki. Are you aware of any resource that discusses the various spaces?

  7. Jeez, this is old. Just in case anyone stumbles across this post like I did, you can simplify your fslmaths stream, by taking the aseg.nii.gz and creating a binary mask in one step.

    Before (two steps):
    fslmaths aseg2raw.nii.gz -uthr 11 -thr 11 l_caudate.nii.gz
    fslmaths l_caudate.nii.gz -div 11 l_caudate.nii.gz

    After (one step):
    fslmaths aseg2raw.nii.gz -uthr 11 -thr 11 -bin l_caudate.nii.gz

    Note the -bin flag, which tells FSL to make every non-zero voxel into a 1 (i.e. binarise).

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge