.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_manipulating_images/plot_resample_to_template.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_manipulating_images_plot_resample_to_template.py: Resample an image to a template =============================== The goal of this example is to illustrate the use of the function :func:`nilearn.image.resample_to_img` to resample an image to a template. We use the MNI152 template as the reference for resampling a t-map image. Function :func:`nilearn.image.resample_img` could also be used to achieve this. .. GENERATED FROM PYTHON SOURCE LINES 12-13 First we load the required datasets using the nilearn datasets module. .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python from nilearn.datasets import ( load_mni152_template, load_sample_motor_activation_image, ) template = load_mni152_template(resolution=2) stat_img = load_sample_motor_activation_image() .. GENERATED FROM PYTHON SOURCE LINES 23-25 Now, the motor :term:`contrast` map image can be resampled to the :term:`MNI` template image. .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python from nilearn.image import resample_to_img resampled_stat_img = resample_to_img(stat_img, template) .. GENERATED FROM PYTHON SOURCE LINES 30-31 Let's check the shape and affine have been correctly updated. .. GENERATED FROM PYTHON SOURCE LINES 31-65 .. code-block:: Python # First load the original t-map in memory: from nilearn.image import load_img tmap_img = load_img(stat_img) original_shape = tmap_img.shape original_affine = tmap_img.affine resampled_shape = resampled_stat_img.shape resampled_affine = resampled_stat_img.affine template_img = load_img(template) template_shape = template_img.shape template_affine = template_img.affine print( f"""Shape comparison: - Original t-map image shape : {original_shape} - Resampled t-map image shape: {resampled_shape} - Template image shape : {template_shape} """ ) print( f"""Affine comparison: - Original t-map image affine : {original_affine} - Resampled t-map image affine: {resampled_affine} - Template image affine : {template_affine} """ ) .. rst-class:: sphx-glr-script-out .. code-block:: none Shape comparison: - Original t-map image shape : (53, 63, 46) - Resampled t-map image shape: (99, 117, 95) - Template image shape : (99, 117, 95) Affine comparison: - Original t-map image affine : [[ -3. 0. 0. 78.] [ 0. 3. 0. -112.] [ 0. 0. 3. -50.] [ 0. 0. 0. 1.]] - Resampled t-map image affine: [[ 2. 0. 0. -98.] [ 0. 2. 0. -134.] [ 0. 0. 2. -72.] [ 0. 0. 0. 1.]] - Template image affine : [[ 2. 0. 0. -98.] [ 0. 2. 0. -134.] [ 0. 0. 2. -72.] [ 0. 0. 0. 1.]] .. GENERATED FROM PYTHON SOURCE LINES 66-67 Finally, result images are displayed using nilearn plotting module. .. GENERATED FROM PYTHON SOURCE LINES 67-85 .. code-block:: Python from nilearn import plotting plotting.plot_stat_map( stat_img, bg_img=template, cut_coords=(36, -27, 66), threshold=3, title="t-map in original resolution", ) plotting.plot_stat_map( resampled_stat_img, bg_img=template, cut_coords=(36, -27, 66), threshold=3, title="Resampled t-map", ) plotting.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_resample_to_template_001.png :alt: plot resample to template :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_resample_to_template_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_resample_to_template_002.png :alt: plot resample to template :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_resample_to_template_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.839 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_06_manipulating_images_plot_resample_to_template.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/0.10.3?urlpath=lab/tree/notebooks/auto_examples/06_manipulating_images/plot_resample_to_template.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_resample_to_template.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_resample_to_template.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_