
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/numpy_operations/plot_view_as_blocks.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_numpy_operations_plot_view_as_blocks.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_numpy_operations_plot_view_as_blocks.py:


============================
Block views on images/arrays
============================

This example illustrates the use of ``view_as_blocks`` from
:py:func:`skimage.util`.  Block views can be incredibly useful when one
wants to perform local operations on non-overlapping image patches.

We use ``astronaut`` from ``skimage.data`` and virtually 'slice' it into square
blocks.  Then, on each block, we either pool the mean, the max or the
median value of that block. The results are displayed altogether, along
with a spline interpolation of order 3 rescaling of the original `astronaut`
image.

.. GENERATED FROM PYTHON SOURCE LINES 17-66



.. image-sg:: /auto_examples/numpy_operations/images/sphx_glr_plot_view_as_blocks_001.png
   :alt: Original rescaled with  spline interpolation (order=3), Block view with  local mean pooling, Block view with  local max pooling, Block view with  local median pooling
   :srcset: /auto_examples/numpy_operations/images/sphx_glr_plot_view_as_blocks_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    from scipy import ndimage as ndi
    from matplotlib import pyplot as plt
    import matplotlib.cm as cm

    import skimage as ski


    # get astronaut from skimage.data in grayscale
    l = ski.color.rgb2gray(ski.data.astronaut())

    # size of blocks
    block_shape = (4, 4)

    # see astronaut as a matrix of blocks (of shape block_shape)
    view = ski.util.view_as_blocks(l, block_shape)

    # collapse the last two dimensions in one
    flatten_view = view.reshape(view.shape[0], view.shape[1], -1)

    # resampling the image by taking either the `mean`,
    # the `max` or the `median` value of each blocks.
    mean_view = np.mean(flatten_view, axis=2)
    max_view = np.max(flatten_view, axis=2)
    median_view = np.median(flatten_view, axis=2)

    # display resampled images
    fig, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True, sharey=True)
    ax = axes.ravel()

    l_resized = ndi.zoom(l, 2, order=3)
    ax[0].set_title("Original rescaled with\n spline interpolation (order=3)")
    ax[0].imshow(l_resized, extent=(-0.5, 128.5, 128.5, -0.5), cmap=cm.Greys_r)

    ax[1].set_title("Block view with\n local mean pooling")
    ax[1].imshow(mean_view, cmap=cm.Greys_r)

    ax[2].set_title("Block view with\n local max pooling")
    ax[2].imshow(max_view, cmap=cm.Greys_r)

    ax[3].set_title("Block view with\n local median pooling")
    ax[3].imshow(median_view, cmap=cm.Greys_r)

    for a in ax:
        a.set_axis_off()

    fig.tight_layout()
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.056 seconds)


.. _sphx_glr_download_auto_examples_numpy_operations_plot_view_as_blocks.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_view_as_blocks.ipynb <plot_view_as_blocks.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_view_as_blocks.py <plot_view_as_blocks.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_view_as_blocks.zip <plot_view_as_blocks.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
