Easy way to extract each roi as a single mask from a multi-roi mask


There is a single brick mask file, which consists of several rois with different values, and names of these rois are stored at labtable of this mask file.

Is there an easy way to extract each roi(value) as a single mask from this multi roi(value) mask. Though 3dcalc can do this work for each roi, is there an easy way? And it will be much better if each single roi mask could be named from the labtable automatically.


Hi 2086,

You can do something like this:

3dROIstats -mask MyMask+tlrc.'<1>' MyDataset+tlrc.

Changing the 1 to other numbers can increment through. 3dROIstats will also output a column for each value in the dataset by default, which is my usual method. I then rename the columns (usually in R).


Hi Peter,

My aim is to split this multi-roi mask into single-roi masks and name these single-roi masks from the labeltable of the multi-roi mask, but not to extract some statistic value under each roi from some datasets. For example, one mask contain three rois A and B and C, I am going to split it into three masks, contain of A or B or C.

The reason to do this is to import this multi-roi mask to FSL FDT, which will take this multi-roi mask as ONE roi.

Thank you anyway,

Hi, 2086–

I don’t quite see what you are asking, but yes, you can select a region with the labeltable name.

For example, this should give you the “Brain-Stem”-labelled ROI from the dset as a binary mask in its own file:

3dcalc -a "aparc.a2009s+aseg_REN_gm.nii.gz<Brain-Stem>" -expr 'step(a)' -prefix output.nii.gz


Hi Pual,

Thank you. I didn’t know 3dcalc can be done with roi label directly until I saw your answer.

And this is an adaptation from your answer to meet my need:

# get a list of all roi names( get labeltable|rm summary lines|rm "|print 2ed column names)
set rois = `3dinfo -labeltable mri_aparc+aseg_REN_gm.nii.gz | tail -n +4 | sed 's/\"//g' | awk '{print $2}'`

# extract each roi into single-roi mask
foreach roi ($rois)
    3dcalc -a "mri_aparc+aseg_REN_gm.nii.gz<$roi>" -expr 'step(a)' -prefix $roi.nii.gz


Hi !
I just wonder if there is any easy way to “select” a once painted mask and move it around.
I’m making ROIs of peripheral nerves, having multiple sequences of the same region. The thing is that the sequences don’t overlap perfectly and i have to make a new ROI every time (it’s about a 1-4 voxel shift in mostly one direction). It would be a lot easier if i could take the mask of one sequence and adjust it to the others instead of having to redo them every time.
Cheers, CEO of Homus

Hi rosaann-

There are a number of possible ways: You can try the Nudge plugin within AFNI’s viewer. Alternatively, it’s possible to use 3drotate.