Problems combining +orig and +tlrc template ROI masks

Hello everyone! I’m having some issues combining masks. I have a set of masks of segmentations from Freesurfer, and a hand drawn PAG mask that was drawn on an MNI template. For example, the amygdala ROI from freesurfer is in NIFTI format and aligns to both +orig and +tlrc volumes of my anatomy. The PAG only aligns to +tlrc (since that where it was drawn). All of my analyses at this point are in MNI space so this is fine.

I resampled both the amygdala and PAG to the same grid, and using 3dcalc I can make a mask that has all the ROIs that came from Freesurfer (like the amygdala), but when I add in the PAG I’m running into errors from 3dcalc:

3dcalc -overwrite -a FS_amygdala_MNI_resample_NPU.nii -b PAG_mask_NPU_blur2.nii -expr ‘ispositive(a) + ispositive(b)’ -prefix PAG_centric_network_mask.nii
*+ WARNING: Template space of PAG_mask_NPU_blur2.nii does not match space of first dataset
Continuing anyway, but be sure the datasets really do match
** FATAL ERROR: dataset PAG_mask_NPU_blur2.nii differs in size [17183259 voxels] from others [1966080]

I saw in a previous post to check the header information using 3dinfo:

3dinfo -av_space -space -d3 -orient -o3 -prefix FS_amygdala_MNI_resample_NPU.nii PAG_mask_NPU_blur2.nii

+orig ORIG 0.859400 0.859400 1.800000 RAI -108.000000 -151.992004 -20.358101 FS_amygdala_MNI_resample_NPU.nii
+tlrc MNI 0.859400 0.859400 1.800000 RAI -108.000000 -151.992004 -20.358101 PAG_mask_NPU_blur2.nii

Even though the view and space of the amygdala mask is ORIG, it still aligns very well to my MNI space aligned datasets (I think I read that’s the default view for NIFTI files though). I tried 3drefit to change the view and space of the PAG before redoing 3dcalc, but this pushes the PAG way out of alignment.

I’ve tried both 3dresample and 3dfractionize to switch between grids for the freesurfer ROIs and for the PAG, and it doesn’t seem like any combination of resampling and header updating I’ve tried works, one of the masks is always out of alignment.

The strange thing to me is that in the visualization GUI I can overlay the amygdala and PAG masks in the same screen and they appear to be aligned appropriately and in the same space.

I’m stumped… any suggestions for how to get these 2 masks to cooperate with one another?


Well, whether the dsets are NIFTI or BRIK/HEAD shouldn’t/doesn’t matter. For using 3dcalc like this, the grid must be the same. (Note that your spaces are different—ORIG vs TLRC—but that won’t matter in this case, in terms of running the 3dcalc command.) Right now, you have the same voxel size and origin, but I will guess that you have different matrix sizes-- you can check with “3dinfo -n4 …” on each dset; or you can ask if grids are the same with:

3dinfo -same_grid FILE1  FILE2


3dinfo -same_all_grid FILE1  FILE2

Consider the following example:

# make an ORIG space version of MNI template
3dcopy MNI152_1mm_uni+tlrc. mni_orig.nii
3drefit -space ORIG mni_orig.nii 

# make version of the ORIG-MNI that has a different grid size
3dZeropad -I -5 -prefix mni_orig_small.nii mni_orig.nii 

# try 3dcalc on each of the ORIG-MNI versions
3dcalc -a MNI152_1mm_uni+tlrc. -b mni_orig.nii -expr 'a+b' -prefix sum1.nii
3dcalc -a MNI152_1mm_uni+tlrc. -b mni_orig_small.nii -expr 'a+b' -prefix sum2.nii

From the above, “sum1.nii” will be calculated (with the same warning you report about 3dcalc’ing dsets with different spaces). However, “sum2.nii” won’t be, because it has a different matrix size.

If that matrix size difference is the only issue, you could use 3dZeropad to adjust one; calling the one whose grid will be expanded/compressed DSET_IN, you can make it match that of DSET_REF with:

3dZeropad -master DSET_REF -prefix DSET_IN2REF DSET_IN

Again, this all assumes your dsets really can be added together in this manner-- Did you run FreeSurfer on the MNI template, or did you align the subject anatomical to MNI space? I’m not sure how they would be so well aligned otherwise…

Finally, I’ll note that we have a new convenience tool for checking if the anatomical you want to put into FreeSurfer has all the properties needed for running recon-all and have the outputs align well with the inputs (e.g., isotropic voxels and even matrix dimensions); it is called, and you can read more about why it is useful/necessary here:
(That webpage describes both checking the properties, and fixing them, if necessary.)