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

.. only:: html

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

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

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

.. _sphx_glr_advanced_image_processing_auto_examples_plot_granulo.py:


Granulometry
============

This example performs a simple granulometry analysis.

.. GENERATED FROM PYTHON SOURCE LINES 7-59



.. image-sg:: /advanced/image_processing/auto_examples/images/sphx_glr_plot_granulo_001.png
   :alt: plot granulo
   :srcset: /advanced/image_processing/auto_examples/images/sphx_glr_plot_granulo_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    import scipy as sp
    import matplotlib.pyplot as plt


    def disk_structure(n):
        struct = np.zeros((2 * n + 1, 2 * n + 1))
        x, y = np.indices((2 * n + 1, 2 * n + 1))
        mask = (x - n) ** 2 + (y - n) ** 2 <= n**2
        struct[mask] = 1
        return struct.astype(bool)


    def granulometry(data, sizes=None):
        s = max(data.shape)
        if sizes is None:
            sizes = range(1, s / 2, 2)
        granulo = [
            sp.ndimage.binary_opening(data, structure=disk_structure(n)).sum()
            for n in sizes
        ]
        return granulo


    rng = np.random.default_rng(27446968)
    n = 10
    l = 256
    im = np.zeros((l, l))
    points = l * rng.random((2, n**2))
    im[(points[0]).astype(int), (points[1]).astype(int)] = 1
    im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

    mask = im > im.mean()

    granulo = granulometry(mask, sizes=np.arange(2, 19, 4))

    plt.figure(figsize=(6, 2.2))

    plt.subplot(121)
    plt.imshow(mask, cmap="gray")
    opened = sp.ndimage.binary_opening(mask, structure=disk_structure(10))
    opened_more = sp.ndimage.binary_opening(mask, structure=disk_structure(14))
    plt.contour(opened, [0.5], colors="b", linewidths=2)
    plt.contour(opened_more, [0.5], colors="r", linewidths=2)
    plt.axis("off")
    plt.subplot(122)
    plt.plot(np.arange(2, 19, 4), granulo, "ok", ms=8)


    plt.subplots_adjust(wspace=0.02, hspace=0.15, top=0.95, bottom=0.15, left=0, right=0.95)
    plt.show()


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

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


.. _sphx_glr_download_advanced_image_processing_auto_examples_plot_granulo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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