.. only:: html
    .. note::
        :class: sphx-glr-download-link-note
        Click :ref:`here `     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_extract_rois_statistical_maps.py:
Region Extraction using a t-statistical map (3D)
================================================
This example shows how to extract regions or separate the regions
from a statistical map.
We use localizer t-statistic maps from
:func:`nilearn.datasets.fetch_neurovault_auditory_computation_task`
as an input image.
The idea is to threshold an image to get foreground objects using a
function :func:`nilearn.image.threshold_img` and extract objects using a function
:func:`nilearn.regions.connected_regions`.
Fetching t-statistic image of localizer constrasts by loading from datasets
utilities
.. code-block:: default
    from nilearn import datasets
    localizer = datasets.fetch_neurovault_auditory_computation_task()
    tmap_filename = localizer.images[0]
Threshold the t-statistic image by importing threshold function
.. code-block:: default
    from nilearn.image import threshold_img
    # Two types of strategies can be used from this threshold function
    # Type 1: strategy used will be based on scoreatpercentile
    threshold_percentile_img = threshold_img(tmap_filename, threshold='97%', copy=False)
    # Type 2: threshold strategy used will be based on image intensity
    # Here, threshold value should be within the limits i.e. less than max value.
    threshold_value_img = threshold_img(tmap_filename, threshold=3.0, copy=False)
Visualization
Showing thresholding results by importing plotting modules and its utilities
.. code-block:: default
    from nilearn import plotting
    # Showing percentile threshold image
    plotting.plot_stat_map(threshold_percentile_img, display_mode='z', cut_coords=5,
                           title='Threshold image with string percentile', colorbar=False)
    # Showing intensity threshold image
    plotting.plot_stat_map(threshold_value_img, display_mode='z', cut_coords=5,
                           title='Threshold image with intensity value', colorbar=False)
.. rst-class:: sphx-glr-horizontal
    *
      .. image:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_001.png
          :alt: plot extract rois statistical maps
          :class: sphx-glr-multi-img
    *
      .. image:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_002.png
          :alt: plot extract rois statistical maps
          :class: sphx-glr-multi-img
.. rst-class:: sphx-glr-script-out
 Out:
 .. code-block:: none
    
Extracting the regions by importing connected regions function
.. code-block:: default
    from nilearn.regions import connected_regions
    regions_percentile_img, index = connected_regions(threshold_percentile_img,
                                                      min_region_size=1500)
    regions_value_img, index = connected_regions(threshold_value_img,
                                                 min_region_size=1500)
Visualizing region extraction results
.. code-block:: default
    title = ("ROIs using percentile thresholding. "
             "\n Each ROI in same color is an extracted region")
    plotting.plot_prob_atlas(regions_percentile_img, bg_img=tmap_filename,
                             view_type='contours', display_mode='z',
                             cut_coords=5, title=title)
    title = ("ROIs using image intensity thresholding. "
             "\n Each ROI in same color is an extracted region")
    plotting.plot_prob_atlas(regions_value_img, bg_img=tmap_filename,
                             view_type='contours', display_mode='z',
                             cut_coords=5, title=title)
    plotting.show()
.. rst-class:: sphx-glr-horizontal
    *
      .. image:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_003.png
          :alt: plot extract rois statistical maps
          :class: sphx-glr-multi-img
    *
      .. image:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_004.png
          :alt: plot extract rois statistical maps
          :class: sphx-glr-multi-img
.. rst-class:: sphx-glr-script-out
 Out:
 .. code-block:: none
    /home/varoquau/dev/nilearn/nilearn/plotting/displays.py:97: UserWarning: No contour levels were found within the data range.
      im = getattr(ax, type)(data_2d.copy(),
.. rst-class:: sphx-glr-timing
   **Total running time of the script:** ( 0 minutes  10.009 seconds)
.. _sphx_glr_download_auto_examples_06_manipulating_images_plot_extract_rois_statistical_maps.py:
.. only :: html
 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example
  .. container:: binder-badge
    .. image:: https://mybinder.org/badge_logo.svg
      :target: https://mybinder.org/v2/gh/nilearn/nilearn.github.io/master?filepath=examples/auto_examples/06_manipulating_images/plot_extract_rois_statistical_maps.ipynb
      :width: 150 px
  .. container:: sphx-glr-download sphx-glr-download-python
     :download:`Download Python source code: plot_extract_rois_statistical_maps.py `
  .. container:: sphx-glr-download sphx-glr-download-jupyter
     :download:`Download Jupyter notebook: plot_extract_rois_statistical_maps.ipynb `
.. only:: html
 .. rst-class:: sphx-glr-signature
    `Gallery generated by Sphinx-Gallery `_