Binary mask for specific AAL ROIs


I am trying to perform some analysis about main tracts in the CNS, and I need, for tckgen code, to give a mask for seeds to start and finish (-seed_image). I was hoping to feed the code with a binary mask using the AAL atlas. Is there a way, using FSL, FreeSurfer or MRtrix3 commands, to transform voxels values to 1 in the ROIs that I am interested in, and transform the rest of the voxels to 0? Using fslmaths -roi is a problem, as I can only transform one ROI in particular, and not a bunch of them.

If anyone has an idea, I am willing to listen to it!



Dear @Marcos_Uceta_Garcia,

If you have the indexes of the AAL regions you want to use in a text file (one index per line), you can loop through them to threshold and binarize to obtain separate ROIs. For example to extract mutiple regions from the AAL atlas:

for i in `list_indices.txt` ; do fslmaths AAL_atlas.nii.gz -thr ${i} -uthr ${i} -bin ROI_${i}.nii.gz ; done

When you have the set of seed regions you can add these together into one image.

fslmaths ROI_1.nii.gz -add ROI_2.nii.gz -add ROI_3.nii.gz seed_mask.nii.gz

Hope this helps,

Hi, Nick!

Thanks for the reply! The solutions works perfectly. I’ll add it to the pipeline, and see what happens.

1 Like

(in case the number of regions is variable…)
it’s also possible to do a big OR operation on a list of voxel values using mrcalc and some bash hocus pocus:

# simple example with parcellation indices 1018 and 1020:
mrcalc AAL_atlas.nii.gz 1018 -eq AAL_atlas.nii.gz 1020 -eq -or seed_mask.nii.gz
# for arbitrary number of (at least 2) indices, stored in i:
i=(1018 1019 1020 2018 2019 2020);
mrcalc $(printf 'AAL_atlas.nii.gz %d -eq ' $i[@]) $(printf ' -or%.0s' {1..$((${#i[@]}-1))}) seed_mask.nii.gz

If you’re after the MRtrix equivalent solution, this should do the job:

mrcalc 0 $(for i in `cat list_indices.txt`; do echo -n "AAL_atlas.nii.gz $i -eq -or "; done) -datatype bit seed_mask.nii.gz

or if you prefer a quick re-usable script:


# usage: 
#   label2mask atlas id_list.txt out_mask

mrcalc 0 $(for i in `cat "$2"`; do echo -n "$1 $i -eq -or "; done) -datatype bit "$3"

That’s exactly what I was looking for. Thanks!


By indices in list_indices.txt, is this text file similar to a look-up table?

What is contained in this file?


I was assuming the same format as used in @NicDC’s answer, which I expect to be a simple list of numbers, one for each label to be included. These could be one per line, or separated by space – I think both would work. So no, that wouldn’t be similar to a lookup table, which would also contain text labels and other bits & pieces such as RGB components, etc.

Hope that answers your question?


Just to clarify: yes, it works both as a one-per-line list (\n list) or as a separated-by-space list (\b list). I tried both and it worked the same way. Just add the label for each ROI and you’ll be good to go.