.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/solutions/plot_fft_image_denoise.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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_intro_scipy_auto_examples_solutions_plot_fft_image_denoise.py: ====================== Image denoising by FFT ====================== Denoise an image (:download:`../../../../data/moonlanding.png`) by implementing a blur with an FFT. Implements, via FFT, the following convolution: .. math:: f_1(t) = \int dt'\, K(t-t') f_0(t') .. math:: \tilde{f}_1(\omega) = \tilde{K}(\omega) \tilde{f}_0(\omega) .. GENERATED FROM PYTHON SOURCE LINES 22-24 Read and plot the image ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 24-34 .. code-block:: default import numpy as np import matplotlib.pyplot as plt im = plt.imread('../../../../data/moonlanding.png').astype(float) plt.figure() plt.imshow(im, plt.cm.gray) plt.title('Original image') .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_001.png :alt: Original image :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Original image') .. GENERATED FROM PYTHON SOURCE LINES 35-37 Compute the 2d FFT of the input image ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 37-52 .. code-block:: default import scipy as sp im_fft = sp.fftpack.fft2(im) # Show the results def plot_spectrum(im_fft): from matplotlib.colors import LogNorm # A logarithmic colormap plt.imshow(np.abs(im_fft), norm=LogNorm(vmin=5)) plt.colorbar() plt.figure() plot_spectrum(im_fft) plt.title('Fourier transform') .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_002.png :alt: Fourier transform :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Fourier transform') .. GENERATED FROM PYTHON SOURCE LINES 53-55 Filter in FFT ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 55-81 .. code-block:: default # In the lines following, we'll make a copy of the original spectrum and # truncate coefficients. # Define the fraction of coefficients (in each direction) we keep keep_fraction = 0.1 # Call ff a copy of the original transform. NumPy arrays have a copy # method for this purpose. im_fft2 = im_fft.copy() # Set r and c to be the number of rows and columns of the array. r, c = im_fft2.shape # Set to zero all rows with indices between r*keep_fraction and # r*(1-keep_fraction): im_fft2[int(r*keep_fraction):int(r*(1-keep_fraction))] = 0 # Similarly with the columns: im_fft2[:, int(c*keep_fraction):int(c*(1-keep_fraction))] = 0 plt.figure() plot_spectrum(im_fft2) plt.title('Filtered Spectrum') .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_003.png :alt: Filtered Spectrum :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Filtered Spectrum') .. GENERATED FROM PYTHON SOURCE LINES 82-84 Reconstruct the final image ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 84-94 .. code-block:: default # Reconstruct the denoised image from the filtered spectrum, keep only the # real part for display. im_new = sp.fftpack.ifft2(im_fft2).real plt.figure() plt.imshow(im_new, plt.cm.gray) plt.title('Reconstructed Image') .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_004.png :alt: Reconstructed Image :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Reconstructed Image') .. GENERATED FROM PYTHON SOURCE LINES 95-100 Easier and better: :func:`scipy.ndimage.gaussian_filter` ########################################################### Implementing filtering directly with FFTs is tricky and time consuming. We can use the Gaussian filter from :mod:`scipy.ndimage` .. GENERATED FROM PYTHON SOURCE LINES 100-108 .. code-block:: default im_blur = sp.ndimage.gaussian_filter(im, 4) plt.figure() plt.imshow(im_blur, plt.cm.gray) plt.title('Blurred image') plt.show() .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_005.png :alt: Blurred image :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_fft_image_denoise_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.727 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_solutions_plot_fft_image_denoise.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_fft_image_denoise.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_fft_image_denoise.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_