How to get the most posterior coronal slice location of a volume mask?


How to get the location of the most posterior coronal slice location of a volume mask (1 in mask, 0 outside) ?


When you say “location”, you mean (x, y, z) coordinate?

3dAutobox (which you may or may not want to use with the “-noclust” option) will tell you this information.

If you use the -extent option, you will see text output in terms of xyz coords for each of the FOV bounding box walls. The formatting of that isn’t very clean for scripting, unfortunately (at the moment, there is only the “clean” list of 6 numbers dumped to a file for IJK values), so you might want to do the following:

  1. make a new dset that has been autoboxed, and
  2. use 3dinfo to report the specific P extent of the new, autoboxed dset:

3dAutobox -prefix AAA.nii -input DSET
set ext = `3dinfo -Pextent AAA.nii`
echo "++ The furthest extent in the posterior direction is:  $ext"


Hi Paul,

Thanks for your answer!

Yes, 3dAutobox is what I need!


That’s a nice idea, I still think 3dAutobox (again, probably with -noclust) would be the fastest. To get around having to create a new dset, you could do:


# get the full, formatted string
set aaa = `3dAutobox -noclust -extent -input PIL.nii`
# get just the "P=..." piece of the output (always the 7th, even if dataset is resampled), and the get just the number part (3rd character and after)
set just_coord = `echo ${aaa[7]} | awk '{print substr($0,3)}'`

echo "++ The posterior coord is:  ${just_coord}"


Hi Paul,

Yes, you are right. When I post my answer I did not see your answer. 3dAutobox is much more faster than 3dmaskdump.

Thank you again,

Hi 2086,

It might be more straightforward to do this with 3dClusterize, say, which outputs the cluster extents. You could just take the most posterior extent form the list of clusters (min size = 1 voxel, if possible).

  • rick