FreeSurfer Tools for Mask Manipulation

FreeSurfer includes a useful tool for manipulating mask files. Let’s say you wanted to pull the left caudate from the aseg.mgz file in order to use it for fiber tracking (that’s my default example in part because it’s salient for my research).

I want to be able to work with the FreeSurfer created files in other programs so I’ll convert to nii.gz.

mri_convert /Applications/freesurfer/subjects/{subjectID}/mri/orig.mgz /Applications/freesurfer/subjects/{subjectID}/mri/orig.nii.gz

Now I want to convert the aseg.mgz file over. I’ll use a different method than mri_convert or mri_binarize (although both could work).

mri_label2vol –subject {subjectID} –seg /Applications/freesurfer/subjects/{subjectID}/mri/aseg.mgz –fillthresh .8 –identity –temp /Applications/freesurfer/subjects/{subjectID}/mri/orig.mgz –o /Applications/freesurfer/subjects/{subjectID}/mri/aseg2vol.nii.gz

Technically, I didn’t need the –subject on there but I like to include it just as a redundancy.

Here’s the output in ITK-SNAP:

But I don’t need all of the brain structures, I just want the left caudate (label 11).

mri_binarize –i /Applications/freesurfer/subjects/{subjectID}/mri/aseg2vol.nii.gz –match 11 –o /Applications/freesurfer/subjects/{subjectID}/mri/left_caudate.nii.gz

You actually can run that command with the basic aseg.mgz (and thus skip the mri_label2vol step above) but I wanted to show both. I’d recommend using mri_label2vol for binary mask creation but for subcortical structures it shouldn’t matter. Running this conversion both ways (mri_label2vol and mri_binarize) results in a perfect mask overlap (the blue and red caudates – one from each method – appear purple in this screenshot).

Now say you wanted to make a mask a little bit bigger or a little bit smaller (to either pick up surrounding voxels or to exclude part of a mask). You can use mri_binarize to do that (you could even do this with the step above, I’m separating it out for simplicity):

mri_binarize –i /Applications/freesurfer/subjects/{subjectID}/mri/left_caudate2.nii.gz –erode 1 –match 2 –o /Applications/freesurfer/subjects/{subjectID}/mri/left_caudate3.nii.gz

In that command I input (–i), erode 1 voxel in 3D (–erode 1) from the left caudate (–match 2; that is a 2 because I created this binary mask earlier and gave the left caudate a value of 2), and then specify an output (–o).

Here you can see the results (visualization in FSLView, which I’m not overly fond of but it makes viewing mask overlap very easy):

As you can see, the eroded caudate is one voxel smaller in 3D than the original caudate.

You can created a summed overlap file (or combine any mask files in the same space together) by using fslmaths (you can also do this with the mri_binarize –merge option but I’m just introducing a range of tools):

fslmaths {input file} -add {adding file} {output}

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