Apply mask to functional image with different dimensions

Hi!
I am doing a study of emotions evoked by music in the brain and have functional images from different individuals. I now want to apply to the functional images a mask retrieved from a previous meta analysis. However, the mask and the functional images have different dimensions and affines.

I tried to do the following but when i view the final image, I have lost all the information of the mask:

#Resample the mask to the same space as the functional images
mask_img_resampled = image.resample_img(mask_img, target_affine=fmri_img.affine, target_shape=fmri_img.shape[:3])

#Apply the mask to the fMRI image
masked_data = masking.apply_mask(fmri_img, mask_img_resampled)

#Create a new 4D image from the masked data
masked_img = image.index_img(fmri_img, range(masked_data.shape[0]))

What would be the best way to do it?

Hi @Ana_Guedes, and welcome to neurostars!

Have you tried visualizing the resampled mask, does it look correct?

Also, are your BOLD and mask images in the same space (e.g., MNI)? If not, you will have to register the images together (I can show you how to do that need be). The resample_img is more about making something match dimensions, but not for running a full registration images to each other.

For mask images, you should always use nearest-neighbor interpolation (so add an argument interpolation=nearest).

index_img is not the right command here. That function’s main use case is to extract a single 3D volume from a 4D timeseries (e.g., getting a single time point from a whole BOLD run). If you already have the masked_data as a matrix or array, you can use image.new_img_like, e.g.,

nilearn.image.new_img_like(fmri_img , masked_data , affine=fmri_img.affine , copy_header=True) 

Best,
Steven

1 Like