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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_auto_examples_filters_plot_cycle_spinning.py>`
        to download the full example code

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

.. _sphx_glr_auto_examples_filters_plot_cycle_spinning.py:


=================================
Shift-invariant wavelet denoising
=================================

The discrete wavelet transform is not `shift-invariant`_.  Shift invariance can
be achieved through an undecimated wavelet transform (also called stationary
wavelet transform), at cost of increased redundancy (i.e. more wavelet
coefficients than input image pixels).  An alternative way to approximate
shift-invariance in the context of image denoising with the discrete wavelet
transform is to use the technique known as "cycle spinning".  This involves
averaging the results of the following 3-step procedure for multiple spatial
shifts, n:

1. (circularly) shift the signal by an amount, n
2. apply denoising
3. apply the inverse shift

For 2D image denoising, we demonstrate here that such cycle-spinning can
provide a substantial increase in quality, with much of the gain being
achieved simply by averaging shifts of only n=0 and n=1 on each axis.

.. _`shift-invariant`: https://en.wikipedia.org/wiki/Shift-invariant_system

.. GENERATED FROM PYTHON SOURCE LINES 25-91



.. image-sg:: /auto_examples/filters/images/sphx_glr_plot_cycle_spinning_001.png
   :alt: Noisy PSNR=16.46, Denoised: no cycle shifts PSNR=26.32, Denoised: 2x2 shifts PSNR=27.17, Denoised: 4x4 shifts PSNR=27.58, Denoised: 6x6 shifts PSNR=27.64
   :srcset: /auto_examples/filters/images/sphx_glr_plot_cycle_spinning_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    /build/skimage-sj0QPi/skimage-0.19.3/.pybuild/cpython3_3.11/build/skimage/_shared/utils.py:348: UserWarning:

    The optional dask dependency is not installed. The number of workers is set to 1. To silence this warning, install dask or explicitly set `num_workers=1` when calling the `cycle_spin` function

    Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
    Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
    Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
    Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).






|

.. code-block:: default

    import matplotlib.pyplot as plt

    from skimage.restoration import denoise_wavelet, cycle_spin
    from skimage import data, img_as_float
    from skimage.util import random_noise
    from skimage.metrics import peak_signal_noise_ratio


    original = img_as_float(data.chelsea()[100:250, 50:300])

    sigma = 0.155
    noisy = random_noise(original, var=sigma**2)

    fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(10, 4),
                           sharex=False, sharey=False)
    ax = ax.ravel()

    psnr_noisy = peak_signal_noise_ratio(original, noisy)
    ax[0].imshow(noisy)
    ax[0].axis('off')
    ax[0].set_title(f'Noisy\nPSNR={psnr_noisy:0.4g}')


    # Repeat denosing with different amounts of cycle spinning.  e.g.
    # max_shift = 0 -> no cycle spinning
    # max_shift = 1 -> shifts of (0, 1) along each axis
    # max_shift = 3 -> shifts of (0, 1, 2, 3) along each axis
    # etc...

    denoise_kwargs = dict(channel_axis=-1, convert2ycbcr=True, wavelet='db1',
                          rescale_sigma=True)

    all_psnr = []
    max_shifts = [0, 1, 3, 5]
    for n, s in enumerate(max_shifts):
        im_bayescs = cycle_spin(noisy, func=denoise_wavelet, max_shifts=s,
                                func_kw=denoise_kwargs, channel_axis=-1)
        ax[n+1].imshow(im_bayescs)
        ax[n+1].axis('off')
        psnr = peak_signal_noise_ratio(original, im_bayescs)
        if s == 0:
            ax[n+1].set_title(
                f'Denoised: no cycle shifts\nPSNR={psnr:0.4g}')
        else:
            ax[n+1].set_title(
                f'Denoised: {s+1}x{s+1} shifts\nPSNR={psnr:0.4g}')
        all_psnr.append(psnr)

    # plot PSNR as a function of the degree of cycle shifting
    ax[5].plot(max_shifts, all_psnr, 'k.-')
    ax[5].set_ylabel('PSNR (dB)')
    ax[5].set_xlabel('max cycle shift along each axis')
    ax[5].grid(True)
    plt.subplots_adjust(wspace=0.35, hspace=0.35)

    # Annotate with a cyan arrow on the 6x6 case vs. no cycle shift case to
    # illustrate a region with reduced block-like artifact with cycle shifting
    arrowprops = dict(arrowstyle="simple,tail_width=0.1,head_width=0.5",
                      connectionstyle="arc3",
                      color='c')
    for i in [1, 4]:
        ax[i].annotate("", xy=(101, 39), xycoords='data',
                       xytext=(70, 70), textcoords='data',
                       arrowprops=arrowprops)

    plt.show()


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

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


.. _sphx_glr_download_auto_examples_filters_plot_cycle_spinning.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

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