How to find the edge of an MRI image?


I have a binary brain mask, and I want an image of just the outside edge of this brain mask. Is there a tool that will help me do this?


In AFNI, you could use:

# make mask, dilated/shrunken by one voxel
3dmask_tool                         \
     -dilate_inputs -1              \
     -inputs DSET_MASK              \
     -prefix mask_shrunk.nii.gz

# subtract shrunken mask from original
3dcalc                              \
     -a DSET_MASK                   \
     -b mask_shrunk.nii.gz          \
     -expr 'a-b'                    \
     -prefix mask_outer_edge.nii.gz



This is a beautiful solution. I’ll probably implement in FSL because I’m more familiar, but it’s a great strategy.

Here is Pauls method for FSL. You can use fslmaths or my open source clone niimath (which is dramatically faster for the dilation).

fslmaths DSET_MASK -dilM mask_shrunk
fslmaths mask_shrunk -sub DSET_MASK mask_outer_edge
1 Like

As an aside, if you want to grow or shrink by more than a voxel or have an edge that is more than one voxel thick, iterative dilation is a poor choice. It is very slow and the shapes grow as diamonds on the grid, not spherically.

Surprisingly, it turns out that the Euclidean Distance Transform is separable, allowing you to compute it in 3D in very little time. You can find implementations of this lovely algorithm in R and Python. While it is not in fslmaths, it is in my niimath clone. This allows you to shrink or grow by any amount an have an edge that is and any thickness you desire:

niimath DSET_MASK -binv -edt mask_edt
niimath mask_edt -thr 2 -uthr 3 -bin mask_outer_edge
1 Like