.. 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_denoising.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_advanced_image_processing_auto_examples_plot_denoising.py: Denoising an image with the median filter ========================================== This example shows the original image, the noisy image, the denoised one (with the median filter) and the difference between the two. .. GENERATED FROM PYTHON SOURCE LINES 8-45 .. image-sg:: /advanced/image_processing/auto_examples/images/sphx_glr_plot_denoising_001.png :alt: Original image, Noisy image, Median filter, Error :srcset: /advanced/image_processing/auto_examples/images/sphx_glr_plot_denoising_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((20, 20)) im[5:-5, 5:-5] = 1 im = sp.ndimage.distance_transform_bf(im) im_noise = im + 0.2 * rng.normal(size=im.shape) im_med = sp.ndimage.median_filter(im_noise, 3) plt.figure(figsize=(16, 5)) plt.subplot(141) plt.imshow(im, interpolation="nearest") plt.axis("off") plt.title("Original image", fontsize=20) plt.subplot(142) plt.imshow(im_noise, interpolation="nearest", vmin=0, vmax=5) plt.axis("off") plt.title("Noisy image", fontsize=20) plt.subplot(143) plt.imshow(im_med, interpolation="nearest", vmin=0, vmax=5) plt.axis("off") plt.title("Median filter", fontsize=20) plt.subplot(144) plt.imshow(np.abs(im - im_med), cmap="hot", interpolation="nearest") plt.axis("off") plt.title("Error", fontsize=20) plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, bottom=0, left=0, right=1) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.087 seconds) .. _sphx_glr_download_advanced_image_processing_auto_examples_plot_denoising.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_denoising.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_denoising.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_denoising.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_