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}