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.

Hello, I want to ask you a question. Why can you extract ROI directly with AAL map? Have you registered DWI with MNI? How can I register from the atlas to DWI? Thank you very much for explaining


Once you have your subject’s T1 segmented in the atlas you want it to be (in my case, AAL), you can use that T1.nii to transform, interpolating with mrtransform + fslroi + transformations via fslmaths the AAL-T1 into AAL-b0 (DWI format) in order to work with it and the DWI per se. In fact, once you’ve finished with all transformations, you should have only one file: the AAL_b0 for WM of your subjects. With that, you can do whatever you want in further steps.

Hope I helped you with your question! If you have any unresolved point, please let me know.


1 Like


Another (and perhaps less complex) way to do it is to calculate the (linear or non-linear) transformation of your T1-weighted image in B0 space to the MNI T1-weighted standard template (make sure the AAL atlas is in the same MNI space), and then apply the inverse of this transform to move the AAL atlas into B0 space, using nearest neighbor interpolation. Then you can extract the labels for the tractography.


1 Like

Both methods are fine, and complementary (of course, mine is way more complex :joy:). Choose what you will, and use them wisely.

Thanks both for the comments.

1 Like

Hi there!

I’m a bit confused about the bigger picture regarding a task I’m working on, and I’d really appreciate some assistance.

Currently, I’m trying to extract pathways of interest from a whole brain connectome using the connectom2tck command.

To achieve this, I need to create a binary ROI mask from the AAL atlas, which is in B0 space.

My plan is to create a binary ROI mask in B0 space by following these steps:

  1. Calculating a transformation matrix (using linear or nonlinear or both) from B0 to T1 and T1 to AAL atlas.
  2. Apply the inverse of this transformation to register the AAL atlas to B0 space.
  3. Finally, extract the binary ROI files in B0 space using mrcalc.

Does this approach seem correct? I would appreciate any insights or suggestions on this process. Thank you!