Image Transform MNI -> Talaiarach with resample_img or nipype

Hello,

I’m hoping to confirm whether my following code is indeed performing the function I’m intending: transforming an image from MNI space to talairach space as specified by Lancaster et al. (https://brainmap.org/icbm2tal/).

def icbm_spm2tal(input_mni_image, name):

    talairach_reference_space_image = image.load_img('data/colin_27_mask_2mm.nii.gz')
    
    # spm to talairach
    icbm_spm2tal_affine = np.array([[0.9254, 0.0024, -0.0118, -1.0207],
                            [-0.0048, 0.9316, -0.0871, -1.7667],
                            [0.0152, 0.0883,  0.8924, 4.0926],
                            [0.0000, 0.0000, 0.0000, 1.0000]]);
    
    after_rot = image.resample_img(input_mni_image, 
                                   target_affine=icbm_spm2tal_affine.dot(talairach_reference_space_image.affine),
                                   target_shape=talairach_reference_space_image.shape[0:3],
                                   interpolation='nearest')
    after_rot.to_filename( name + '_tal.nii.gz')
    return after_rot

Alternatively, I’ve had trouble with the nipyp ApplyXFM() function

import scipy
icbm_fsl = np.array([[0.9254, 0.0024, -0.0118, -1.0207],
                        [-0.0048, 0.9316, -0.0871, -1.7667],
                        [0.0152, 0.0883,  0.8924, 4.0926],
                        [0.0000, 0.0000, 0.0000, 1.0000]])

scipy.io.savemat('icbm_fsl.mat', {'mydata': icbm_fsl})

cd = '/Users/thomasvanasse/Desktop/'

import nipype.interfaces.fsl as fsl
from nipype.testing import example_data
applyxfm = fsl.preprocess.ApplyXFM()
applyxfm.inputs.in_file = cd + 'mean_rCST.nii.gz'
applyxfm.inputs.in_matrix_file = cd + 'icbm_fsl.mat'
applyxfm.inputs.out_file = cd + 'out_file.nii.gz'
applyxfm.inputs.reference = cd + 'colin_27_mask_2mm.nii.gz'
applyxfm.inputs.apply_xfm = True
result = applyxfm.run() 

However, with nipype I get the following error:

RuntimeError: Command:
flirt -in /Users/thomasvanasse/Desktop/mean_rCST.nii.gz -ref /Users/thomasvanasse/Desktop/colin_27_mask_2mm.nii.gz -out /Users/thomasvanasse/Desktop/out_file.nii.gz -omat mean_rCST_flirt.mat -applyxfm -init /Users/thomasvanasse/Desktop/icbm_fsl.mat
Standard output:

Standard error:
libc++abi.dylib: terminating with uncaught exception of type NEWMAT::IncompatibleDimensionsException
Return code: -6

Any help would be appreciated,

Tom

If I understand correctly, I think I solved my problem. Instead of using resample_img, I apply my transform to my source affine, and then saved the image. This leaves the transformed image in the same dimensions as the pretransformed image (previously referenced in this thread: Affine transformation and resampling data ) with the proper affine.

import nibabel as nib
def icbm_spm2tal(input_mni_image,name):

    icbm_spm2tal_affine = np.array([[0.9254, 0.0024, -0.0118, -1.0207],
                                    [-0.0048, 0.9316, -0.0871, -1.7667],
                                    [0.0152, 0.0883,  0.8924, 4.0926],
                                    [0.0000, 0.0000, 0.0000, 1.0000]]);
        
    # apply affine transform to source affine
    moving_vox2mm = input_mni_image.affine
    new_vox2mm = icbm_spm2tal_affine.dot(moving_vox2mm)  # apply affine transform here
    nib.save(input_mni_image.__class__(input_mni_image.get_data(), 
                                       new_vox2mm, 
                                       input_mni_image.header), 
             'data/shokri_kojori_et_al_2019/' + name + '_tal.nii.gz')
    return_image = load_img(name + '_tal.nii.gz')
return return_image

Below is the pretransformed mni image with the talairach reference brain (green), post-transformed image overlayed on the talairach reference brain (red).
Screen Shot 2020-08-07 at 11.31.37 AM