How to draw ROIs in the volume?

Hi AFNI experts,

I have one question about how to draw ROIs in the volume.
How to draw a line to connect the two ROIs according to the peak coordinates? And then how to find each ROIs along the line?
Thanks a lot in advance.


You could do something like the following short script to create a line in space. At each point, in the line you could also run whereami with an xyz coordinate on the command line to find regions at each of the points. Here, whereami -omask gives a convenient way to get all the regions in one step.


compute a line in a dataset

usage: makeline3D.csh dset x1 y1 z1 x2 y2 z2

assumes xyz values are in RAI order

set dset = $1
set x1 = $2
set y1 = $3
set z1 = $4
set x2 = $5
set y2 = $6
set z2 = $7

compute distance

set origdist = ccalc "sqrt(($x2-$x1)^2+($y2-$y1)^2+($z2-$z1)^2)"

create normalized vector

set normx = ccalc "($x2-$x1)/$origdist"
set normy = ccalc "($y2-$y1)/$origdist"
set normz = ccalc "($z2-$z1)/$origdist"

create xyz file

echo “# x y z” > xyzline.1D

do this in a minimum number of steps based on voxel size

set voxsize = @GetAfniRes -min $dset
set stepdist = ccalc "$voxsize/$origdist"
set nsteps = ccalc "1/$stepdist"

create line by fractional increments of distance

foreach disti (count 1 $nsteps)
set dist = ccalc "$disti * $origdist/$nsteps"
set newx = ccalc "$x1+$dist*$normx"
set newy = ccalc "$y1+$dist*$normy"
set newz = ccalc "$z1+$dist*$normz"
echo $newx $newy $newz >> xyzline.1D

could query atlases with whereami here for each xyz coordinate

but let’s do it all at once below


create a new dataset from the xyz values

3dUndump -master $dset -prefix xyzline3D.nii.gz -datum byte -xyz
-orient RAI -overwrite xyzline.1D
whereami -omask xyzline3D.nii.gz

Hi Daniel,

Thank you so much for your assistance!

I ran makeline3D.csh, then it did work. However, there is only 1 voxel in ROI.
Furthermore, how to draw a sphere ROI according to coordinates and radius (i.e., 40 voxels) ?

Thank you very much again!


This little script takes 7 inputs - the name of the input dataset, an initial point in xyz RAI coordinates, and an ending point also in xyz RAI coordinates. You should get a line between the points at the grid of the input dataset. There may be only one voxel in any particular image slice. If you want a sphere down at each voxel instead of single voxel, use the -srad option for a default radius. Alternatively, add a fourth and fifth column to the text file with a value and a radius, meaning put a sphere down of radius and specified value.

x y z val radius

If you reduce the step size (and take more steps), then you will get something like a cylinder with rounded ends.

Hi Daniel,

Because these masks were draw in the volume, it is not sure whether the white matter is included in these masks.
Furthermore, how to remove the white matter from each mask?

Thanks a lot in advance.


One option is to combine the spheres with white matter with 3dcalc. You can generate white matter masks with FreeSurfer or even 3dSeg. Mask out the data with something like this:

3dcalc -a masks+orig. -b wm_mask+orig -expr ‘a*not(b)’ -prefix masks_no_wm