.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/filters/plot_window.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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_window.py: =================================== Using window functions with images =================================== Fast Fourier transforms (FFTs) assume that the data being transformed represent one period of a periodic signal. Thus the endpoints of the signal to be transformed can behave as discontinuities in the context of the FFT. These discontinuities distort the output of the FFT, resulting in energy from "real" frequency components leaking into wider frequencies. The effects of spectral leakage can be reduced by multiplying the signal with a window function. Windowing smoothly reduces the amplitude of the signal as it reaches the edges, removing the effect of the artificial discontinuity that results from the FFT. In this example, we see that the FFT of a typical image can show strong spectral leakage along the x and y axes (see the vertical and horizontal lines in the figure). The application of a two-dimensional Hann window greatly reduces the spectral leakage, making the "real" frequency information more visible in the plot of the frequency component of the FFT. .. GENERATED FROM PYTHON SOURCE LINES 23-50 .. image-sg:: /auto_examples/filters/images/sphx_glr_plot_window_001.png :alt: Original image, Windowed image, Original FFT (frequency), Window + FFT (frequency) :srcset: /auto_examples/filters/images/sphx_glr_plot_window_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.fft import fft2, fftshift from skimage import img_as_float from skimage.color import rgb2gray from skimage.data import astronaut from skimage.filters import window image = img_as_float(rgb2gray(astronaut())) wimage = image * window('hann', image.shape) image_f = np.abs(fftshift(fft2(image))) wimage_f = np.abs(fftshift(fft2(wimage))) fig, axes = plt.subplots(2, 2, figsize=(8, 8)) ax = axes.ravel() ax[0].set_title("Original image") ax[0].imshow(image, cmap='gray') ax[1].set_title("Windowed image") ax[1].imshow(wimage, cmap='gray') ax[2].set_title("Original FFT (frequency)") ax[2].imshow(np.log(image_f), cmap='magma') ax[3].set_title("Window + FFT (frequency)") ax[3].imshow(np.log(wimage_f), cmap='magma') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.865 seconds) .. _sphx_glr_download_auto_examples_filters_plot_window.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.23.2?filepath=notebooks/auto_examples/filters/plot_window.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_window.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_window.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_