How to overlay a mask image on another MRI image using nilearn.plotting

Hi, All. I am new to nilearn here. Given a mask image mask.nii.gz and t1-weighted image t1.nii.gz, How can I overlay the mark image over t1-weighting in anatomically when I use nilearn.plotting function?

Thanks.

You can plot the t1 image with plot_anat, then add the contour of the mask on top using the add_overlay method of the display object returned by plot_anat:

http://nilearn.github.io/plotting/index.html#adding-overlays-edges-contours-contour-fillings-markers-scale-bar

1 Like

Thanks, @jeromedockes.

I am following the link to display the colour of mask image as plotting.cm.purple_green
display = plotting.plot_anat(‘xyz.nii.gz’)
display.add_overlay(‘mask.nii.gz’, cmap=plotting.cm.purple_green)

But the error shows ‘Undefined variable from import: purple_green’
Is that any way I can fix this or specify other colour here, such as a tuple cmap=(255,0,255).

Thanks.

thanks! could you share the full script that produced the error? I could not reproduce it. are you using PyDev? all google results for that error message seem to point to pydev

but in the meanwhile yes, specifying for example cmap="gist_ncar" should work

@jeromedockes, yes, I am using PyDev plugin within Eclipse environment.

from nilearn import plotting, datasets, image
import nibabel as nib
import os

display = plotting.plot_anat(‘xyz.nii.gz’)
display.add_overlay(‘mask.nii.gz’, cmap=plotting.cm.purple_green)
plotting.show()

When it runs, eclipse editor shows ‘Undefined variable from import: purple_green’, then mask image is overlay-ed with white colour instead of purple_green.

Thanks.

Can you use simply the mask image in roi_img and anatomical image in bg_img using plotting function nilearn.plotting.plot_roi. You also have cmap argument to select the colormap.

from nilearn import plotting, datasets

display = plotting.plot_anat(datasets.load_mni152_template())
display.add_contours(datasets.load_mni152_brain_mask(),
                     linewidths=2.5, levels=[.5],
                     colors="cyan")
plotting.show()

should run fine.

with the colormap,

from nilearn import plotting, datasets

display = plotting.plot_anat(datasets.load_mni152_template())
display.add_contours(datasets.load_mni152_brain_mask(),
                     linewidths=2.5, levels=[.5],
                     cmap=plotting.cm.purple_green)
plotting.show()

should also work.

at least when run as a python script.

the problem you are having seems to be due to eclipse’s import mechanism

see for example here:

suggestions re. how to fix this eclipse problem

HI, @jeromedockes:

I did run the same code in terminal console and same issue exists with add_overlay function (the colour of mask is white instead of purple_green)
mask_display.add_overlay(overlayPath, cmap=plotting.cm.purple_green)

But changed it to mask_display.add_overlay(overlayPath, cmap=‘cool’) fixed the issue.

Using mask_display.add_contours(overlayPath,

                 linewidths=2.5, levels=[.5],

                 cmap=plotting.cm.purple_green)

does change the mask to the specified colour this time even eclipse editor complaints.

So I think the issue is more likely within add_overlay function.

@jeromedockes, thanks.

there are only two levels in a mask (0 or 1) so do not expect to see a specific color from a cmap without specifying its range. since you are only plotting only one contour the best solution is probably to specify a color rather than a cmap.

does this

from nilearn import plotting, datasets

display = plotting.plot_anat(datasets.load_mni152_template())
display.add_contours(datasets.load_mni152_brain_mask(),
                     linewidths=2.5, levels=[.5],
                     colors="cyan")
plotting.show()

run correctly in a terminal?
if so, I don’t think there is an issue with add_overlay