.. only:: html

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

        Click :ref:`here <sphx_glr_download_auto_examples_filters_plot_dog.py>`     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_filters_plot_dog.py:


==============================================
Band-pass filtering by Difference of Gaussians
==============================================

Band-pass filters attenuate signal frequencies outside of a range (band) of
interest. In image analysis, they can be used to denoise images while
at the same time reducing low-frequency artifacts such a uneven illumination.
Band-pass filters can be used to find image features such as blobs and edges.

One method for applying band-pass filters to images is to subtract an image
blurred with a Gaussian kernel from a less-blurred image. This example shows
two applications of the Difference of Gaussians approach for band-pass
filtering.

Denoise image and reduce shadows
================================


.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np
    from skimage.data import gravel
    from skimage.filters import difference_of_gaussians, window
    from scipy.fftpack import fftn, fftshift

    image = gravel()
    wimage = image * window('hann', image.shape)  # window image to improve FFT
    filtered_image = difference_of_gaussians(image, 1, 12)
    filtered_wimage = filtered_image * window('hann', image.shape)
    im_f_mag = fftshift(np.abs(fftn(wimage)))
    fim_f_mag = fftshift(np.abs(fftn(filtered_wimage)))

    fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))
    ax[0, 0].imshow(image, cmap='gray')
    ax[0, 0].set_title('Original Image')
    ax[0, 1].imshow(np.log(im_f_mag), cmap='magma')
    ax[0, 1].set_title('Original FFT Magnitude (log)')
    ax[1, 0].imshow(filtered_image, cmap='gray')
    ax[1, 0].set_title('Filtered Image')
    ax[1, 1].imshow(np.log(fim_f_mag), cmap='magma')
    ax[1, 1].set_title('Filtered FFT Magnitude (log)')
    plt.show()




.. image:: /auto_examples/filters/images/sphx_glr_plot_dog_001.png
    :class: sphx-glr-single-img





Enhance edges in an image
=========================


.. code-block:: default


    from skimage.data import camera

    image = camera()
    wimage = image * window('hann', image.shape)  # window image to improve FFT
    filtered_image = difference_of_gaussians(image, 1.5)
    filtered_wimage = filtered_image * window('hann', image.shape)
    im_f_mag = fftshift(np.abs(fftn(wimage)))
    fim_f_mag = fftshift(np.abs(fftn(filtered_wimage)))

    fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))
    ax[0, 0].imshow(image, cmap='gray')
    ax[0, 0].set_title('Original Image')
    ax[0, 1].imshow(np.log(im_f_mag), cmap='magma')
    ax[0, 1].set_title('Original FFT Magnitude (log)')
    ax[1, 0].imshow(filtered_image, cmap='gray')
    ax[1, 0].set_title('Filtered Image')
    ax[1, 1].imshow(np.log(fim_f_mag), cmap='magma')
    ax[1, 1].set_title('Filtered FFT Magnitude (log)')
    plt.show()



.. image:: /auto_examples/filters/images/sphx_glr_plot_dog_002.png
    :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_filters_plot_dog.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/scikit-image/scikit-image/v0.17.x?filepath=notebooks/auto_examples/filters/plot_dog.ipynb
      :width: 150 px


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

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



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

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


.. only:: html

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

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