3dAllineate and affine transformation

Dear afni experts:
I obtained the affine transformation between an epi and an anat T1 volumes using align_epi_anat.py. I then attempted to use 3dAllineate function to get an new epi volume that is aligned to the anat file. However, there is a relatively large displacement between the two files. I have some questions as follows.

  1. I am not sure how to set the output grid (i.e., -master option for 3dAlineate) of the aligned epi file. If I choose the anat grid, it upsamples the epi volume because the anat vol has higher resolution. If I choose the epi source grid, it clips some epi data because of the large displacement. How can I get an aligned version of epi data without resample the data (i.e., same resolution)? I search a little bit and find that I can first run @Align_center. However, my epi volume has a narrow and a tilted FOV. So simply aligning grid centers or center of mass did not work very well…

  2. My second thought is to create a new volume to serve as the master grid. I nudge the original epi file to roughly match the t1 file (i.e., remove the large displacement) and save this nudged epi. I then use 3dAllineate to transform the original epi to anat but set the nudge epi file as the grid such that I can preserve the grid resolution of the original epi. However, the clipping problem is even worse… Not sure why…

  3. My third question is that, in my opinion, we do not even resample data at all when applying affine transform. For any epi file, you can simply use the affine transform and change the geometry info in the header file of the epi volume. In this way, no resampling will be executed. So I am confused about why we have to specify a master grid in 3dAllineate… Can I ask how to directly change the geometric string in an epi head file??

Sorry about the long question lists.


Hi, Ruyuan-

Can you use “-master ANAT -mast_dxyz 3.0” (or whatever you want your final spacing to be)?

Note that typically in processing with afni_proc.py, individual transformations are calculated, and then concatenated before applying them to the EPI data. Is afni_proc.py not able to help with your processing needs?


Thanks so much for the reply.
Yes, afni_proc did correct process and transformed the data. However, my original epi dataset is 2x2x2.2 mm and 100x100x37 dimension. afni_proc (i.e., 3dAllineate) resamples data to 2x2x2 mm and 128x128x128 dimension, which dramatically increase file size and change the resolution and matrix of the data. I just want to align epi to T1 but without changing the resolution and matrix size—namely, no resampling data. Is that possible? It seems possible only when epi and anat are very close. Unfortunately my epi data and anat T1 deviate significantly, so I can not use -master epi.


Is there any command that can read and write geometry string info in the head file??


To get the geometry matrix and dimensions:

3dAttribute IJK_TO_DICOM_REAL dset_name
3dAttribute DATASET_DIMENSIONS dset_name

To copy one matrix from a dset to another (but I don’t think this is what you want to do for your data case here??):

# tcsh syntax
set obliq_matr = `3dAttribute IJK_TO_DICOM_REAL  my_dset`
3dcopy other_dset other_dset_copy.nii
3drefit -atrfloat IJK_TO_DICOM_REAL "${obliq_matr}" other_dset_copy.nii

Above, I copie dthe “other_dset” because refitting the header might do bad, undoable things to it.


The minor upsamplign to voxel size seems both appropriate and convenient.

To deal with the anatomical grid/FOV: What if you used 3dAutobox (maybe with -npad to repad it a little) or 3dZeropad to shrink the FOV of your anatomical volume?

I am assuming you are not using the “tlrc” block to go to standard space? If so, you could use either of the above to shrink a copy of your template volume, and use that as a final target?


Thanks Taylor. This is super useful!

Actually, I am confused because, to apply an affine transformation, you can simply extract the geometric matrix, multiple by the affine transformation, and then save the new geometric matrix into the header file. In this case, you avoid any resampling and maximally preserve the same resolution and the matrix size of the original file. When doesn’t afni implement such regime?? In current 3dAllineate, you have to specify a master grid. Maybe I am wrong here… but it seems to make sense to me.

Thanks anyway.


Hi, Paul
I am trying to follow-up the discussion here. Is there any way that you can apply an affine transform but without resampling the data? In my opinion, you can just do like

new_img_affine = affine_xfm * old_img_affine,

where old_img_affine is the geometry info you can read from the file header. Then you can just update old_img_affine to new_img_affine such in the file header. In this case you do not need to resample the data at all. Maybe I miss something here… but it seems that this is the simplest way to do it.


So, a volume has one value per voxel. Those discrete points in space, located within the volume’s FOV, make up a data set grid. When you rotate the data set, what is its “new” grid going to be? It is the same grid as before, but with data values “rotated” through it? If so, you will have to interpolate the new data values in some way. That interpolation is resampling (which can be done any number of ways: NN, cubic, wsinc5, etc., depending on the data set).

Again, typically afni_proc.py deals with this for you; transforms are concatenated before applying, so that multiple resamplings/interpolations don’t take place.


Thanks Taylor!
Sorry, I did not express it clearly. Sure, if you want to write out the values into a file you should specify the rotated grid and interpolate values. My question is that, can you just change the information in the header so the data will be displayed as transformed in the afni gui.

Imaging that you have an epi, a t1 and the affine transform between them. I just want to display the epi file overlayed on the t1 file in the afni viewer. In this case, you do not need to resample epi data. You just need to change the header info in the epi file and the afni viewer will correctly interpret the header and transform the image such that the transformed epi and T1 are overlapped. In this case, the epi data are not touched at all and keep the same value and the same size.

In nifti files, the mapping from the voxel space to the physical (i.e., RAI) space is controlled by sform and qform. Can 3dAllineate directly change sform/qform such that only the mapping is changed but the data are untouched?