Converting FreeSufer aseg Labels to Individual Volume Masks

Note: There are faster (and better) methods than this for creating volumetric masks from aseg labels. I wrote this guide in part to demonstrate some of the features of multiple imaging programs. See this post for a faster method. You could also use the FreeSurfer command: mri_binarize. That will be a topic for a future post.

Say you wanted to pull an ROI (region of interest: brain structure mask) from FreeSurfer to use for fiber tracking or some other purpose (including volumetric analyses of subcortical brain structures).

Navigate to your FreeSurfer subjects directory using the command line (e.g., cd /Applications/freesurfer/subjects/). I’ll use subject S001 as an example.

Change into the participant’s mri directory: cd S001/mri

List (ls) the contents of the directory. The file you most likely want for subcortical structures is called aseg.mgz.

If you want to see what this file looks like in FreeSurfer, enter the following command into Terminal (again this is using S001 as an example; you can also run this command from anywhere; in other words, you do not have to be in any particular location in your computer directories):

tkmedit S001 norm.mgz –aseg

Now say you want the caudates to use as ROIs for fiber tracking or for some other use. You need to get the volumes into diffusion space. However, I want to give an example of transforming the segmentations into native space first. Note: getting the aseg.mgz file into native (T1) space is only important if you want to manually clean the segmentations or if you want to use FLIRT to register them to diffusion space (i.e., if you want to manually modify them using ITK-SNAP or some other tool like that). [As an additional note: I’ve had better success with ROIs using FLIRT (part of FSL) to register them into diffusion space than using mri_convert (part of FreeSurfer). Using FLIRT and FNIRT – tools for linear and nonlinear registration, respectively – will be a future guide]. Use the following command:

mri_convert -rl rawavg.mgz -rt nearest aseg.mgz aseg2raw.nii.gz

That will use FreeSurfer’s mri_convert to reslice aseg.mgz (the automatic segmentation file) like (-rl) a native space volume (rawavg.mgz) and save it as aseg2raw.nii.gz. It is important when dealing with mask files like aseg.mgz, to use the –rt option and set it as nearest (which uses a nearest neighbor resampling and should keep all numerical values in the aseg.mgz value integers. This is important for future processing to run smoothly.

However, if you think that FreeSurfer did a good job (it usually does) with the structures and you just want to use the raw FreeSurfer mask for fiber tracking, you could directly convert the aseg.mgz file into diffusion space without going to T1 space first (this is not my preferred method but it will work for this guide).

In order to do this you can run the mri_convert command but reslicing like one of your diffusion images (such as the nodif.nii.gz file you created with FSL).

For example:

mri_convert -rl [path to subject]/DATA/nodif.nii.gz -rt nearest aseg.mgz aseg2dif.nii.gz

Now you can check the transformed file in ITK-SNAP (version 2.0.0 was used for this pipeline). Open ITK-SNAP and load the nodif.nii.gz file (File >> Open Grayscale Image >> Browse (or type in the path to the file: e.g., /Users/Shared/diffusion/subjects/S001/DATA/nodif.nii.gz) >> Next >> Finish).

The image should load:

Then load the segmentation image (Segmentation >> Load From Image >> Browse… (or type in the path to the aseg2dif.nii.gz image: e.g., /Applications/freesurfer/subjects/S001/mri/aseg2dif.nii.gz) >> Next >> Finish)

If you get an error message of any type you either selected the wrong image or you ran the transform (using mri_convert) incorrectly. It should load just fine if you did everything correctly.

Notice all the different colors/labels. The names are not associated with the labels but each structure has a different label number. If you know the number of the structure(s) you want, you can proceed. If you don’t know the numbers it is easy to find what you want in ITK-SNAP. Navigate through the brain until you find the structure(s) you want. In this case I want the left and right caudates – I’ll work with them separately though. The left hemisphere should be on the right – this is radiological orientation (for the axial slices picture a person lying on their back on a table and you are looking up their nostrils. Their left is on the right to you. For the coronal slices just pretend you are looking the person in the face).

Note: there is a faster (but not graphical) way to pull out these labels. Read my post here.

Notice in the screenshot that the image pointers are in the left caudate; also notice the label number.

Make note of the label number (11 in this instance). This is the number FreeSurfer (at least with version 4.5) assigns all left caudates. The label number for the right caudate is 50.

Now click on the label editor button in ITK-SNAP (see above screenshot). Delete all labels except the one(s) you want. I think it is easiest to do left and right sides at different times (i.e., do one side first, then reload the base image and aseg2dif segmentation and pull out the other structure(s) you want).

When you have deleted the labels your screen should only have one left:

Now you can rename the label (e.g., Left Caudate) if you want to but you do not have to.

Important: Click on the Label Id tab and change the label to 1 (and click set).

You are creating a binary mask file for this structure. If you do not change the label ID for the structure you want, you will have a binary file with zeros and some other number instead of just zeros and ones. Many imaging programs that deal with the masks require them to be only zeros and ones.

Close the label editor menu and verify that you have the structure you want. You can render it in 3D if you want. See screenshot below. If you like how the caudate looks and think it was a reasonable registration and transformation into diffusion space, then you can proceed. Otherwise, and only if you know what you are doing, you can manually edit the caudate a bit should it need some cleaning. It is not recommended that you do much or any editing based on the diffusion image. If the structure is really off then edit it in T1 space. This is one reason I recommend working in T1 space first and then FLIRTing into diffusion space.

OK, I like this caudate (at least enough for this example) so now I’m ready to export it. Go to Segmentation >> Save As Image >> [then specify a path and file name for the mask; e.g., /Users/Shared/diffusion/subjects/S001/ROIS/S001_l_caudate_from_freesurfer_diffusion.nii.gz) >> Save

Now I repeat the process for other structures I want. In this case, the right caudate (label 50). Go to File >> Unload All Images (cmd+u). Your ITK-SNAP will go blank. Reload the base nodif.nii.gz image (it’s fastest to go to File >> Open Recent). Load in the segmentation (aseg2dif.nii.gz): Segmentation >> Load From Image >> History >> [image] (it will be in the history unless you’ve loaded a bunch of segmentations in the meantime). Delete the labels you do not want, change the label to 1, and then save it out (if you like the segmentation and the registration). It is best to keep your naming convention consistent.

That’s it! You now have FreeSurfer structures saved out as volumetric masks using ITK-SNAP (which allows easy visualization and cleaning).

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