.. 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_find_edges.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_find_edges.py>`
        to download the full example code.

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

.. _sphx_glr_advanced_image_processing_auto_examples_plot_find_edges.py:


Finding edges with Sobel filters
==================================

The Sobel filter is one of the simplest way of finding edges.

.. GENERATED FROM PYTHON SOURCE LINES 7-53



.. image-sg:: /advanced/image_processing/auto_examples/images/sphx_glr_plot_find_edges_001.png
   :alt: square, Sobel (x direction), Sobel filter, Sobel for noisy image
   :srcset: /advanced/image_processing/auto_examples/images/sphx_glr_plot_find_edges_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


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

    rng = np.random.default_rng(27446968)

    im = np.zeros((256, 256))
    im[64:-64, 64:-64] = 1

    im = sp.ndimage.rotate(im, 15, mode="constant")
    im = sp.ndimage.gaussian_filter(im, 8)

    sx = sp.ndimage.sobel(im, axis=0, mode="constant")
    sy = sp.ndimage.sobel(im, axis=1, mode="constant")
    sob = np.hypot(sx, sy)

    plt.figure(figsize=(16, 5))
    plt.subplot(141)
    plt.imshow(im, cmap="gray")
    plt.axis("off")
    plt.title("square", fontsize=20)
    plt.subplot(142)
    plt.imshow(sx)
    plt.axis("off")
    plt.title("Sobel (x direction)", fontsize=20)
    plt.subplot(143)
    plt.imshow(sob)
    plt.axis("off")
    plt.title("Sobel filter", fontsize=20)

    im += 0.07 * rng.random(im.shape)

    sx = sp.ndimage.sobel(im, axis=0, mode="constant")
    sy = sp.ndimage.sobel(im, axis=1, mode="constant")
    sob = np.hypot(sx, sy)

    plt.subplot(144)
    plt.imshow(sob)
    plt.axis("off")
    plt.title("Sobel for noisy image", fontsize=20)


    plt.subplots_adjust(wspace=0.02, hspace=0.02, top=1, bottom=0, left=0, right=0.9)

    plt.show()


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

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


.. _sphx_glr_download_advanced_image_processing_auto_examples_plot_find_edges.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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