Brain mask and original 3D T1-w MRI image mismatch

Hi everyone,
I work on Python 3.9.

I have to perform a brain extraction from 3D T1-w MRI images in order to obtain a whole brain mask. To do so, I used the nilearn function: compute_brain_mask.

The problem is that there is a mismatch between the computed mask and the original image when I plot them together by using the plot_roi function.

The image is a Nifti1Image object of size (152, 152, 120)
The mask and the image have the same affine transformation.

How can I fix the problem?

Thanks per advance.

Hi,
Can you show the cod that estimates th mask form the anat and plots it ?

(Note that mask computation in Nilearn assumes an EPI-type input. I’m not sure it works well with T1, but this is another problem than the one you’re describing).

Best,
Bertrand

Hi Bertrand, thanks for the help!

My code is the following:

Plot of the computed mask:

Could you suggest me a Python library to compute a brain mask of T1-w MRI?

Oh, I see. The function you used only applies to MNI-sampled data.
If you want to apply it to arbitrary images, you can try with compute_epi_mask(which is optimized for EPI images, but may work reasonably well for T1 images).
HTH,
Bertrand