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.