.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_denoise/06_02_2d_image_smoothing.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 JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_denoise_06_02_2d_image_smoothing.py: 2D Image Smoothing ================== We use the smooth module to smooth a 2D image. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from splineops.denoise.smoothing_spline import smoothing_spline_nd .. GENERATED FROM PYTHON SOURCE LINES 21-23 2D Image Smoothing ------------------ .. GENERATED FROM PYTHON SOURCE LINES 23-108 .. code-block:: Python import requests from io import BytesIO from PIL import Image def create_image(): """ Loads a real grayscale image. """ url = 'https://r0k.us/graphics/kodak/kodak/kodim06.png' response = requests.get(url) img = Image.open(BytesIO(response.content)) data = np.array(img, dtype=np.float64) data /= 255.0 # Normalize to [0, 1] return data def add_noise(img, snr_db): """ Adds Gaussian noise to the image based on the desired SNR in dB. """ signal_power = np.mean(img ** 2) sigma = np.sqrt(signal_power / (10 ** (snr_db / 10))) noise = np.random.randn(*img.shape) * sigma noisy_img = img + noise return noisy_img def compute_snr(clean_signal, noisy_signal): """ Compute the Signal-to-Noise Ratio (SNR). Parameters: clean_signal (np.ndarray): Original clean signal. noisy_signal (np.ndarray): Noisy signal. Returns: float: SNR value in decibels (dB). """ signal_power = np.mean(clean_signal ** 2) noise_power = np.mean((noisy_signal - clean_signal) ** 2) snr = 10 * np.log10(signal_power / noise_power) return snr def demo_image(): # Parameters lambda_ = 0.1 # Regularization parameter gamma = 2.0 # Order of the spline operator snr_db = 10.0 # Desired SNR in dB # Load image img = create_image() noisy_img = add_noise(img, snr_db) smoothed_img = smoothing_spline_nd(noisy_img, lambda_, gamma) # Compute SNRs snr_noisy = compute_snr(img, noisy_img) snr_smooth = compute_snr(img, smoothed_img) snr_improvement = snr_smooth - snr_noisy print("Image:") print(f"SNR of noisy image: {snr_noisy:.2f} dB") print(f"SNR after smoothing: {snr_smooth:.2f} dB") print(f"SNR improvement: {snr_improvement:.2f} dB\n") # Visualization for image plt.figure(figsize=(12, 4)) plt.subplot(1, 3, 1) plt.imshow(img, cmap='gray') plt.title('Original Image') plt.axis('off') plt.subplot(1, 3, 2) plt.imshow(noisy_img, cmap='gray') plt.title(f'Noisy Image (SNR={snr_noisy:.2f} dB)') plt.axis('off') plt.subplot(1, 3, 3) plt.imshow(smoothed_img, cmap='gray') plt.title(f'Smoothed Image (SNR={snr_smooth:.2f} dB)') plt.axis('off') plt.tight_layout() plt.show() # Run the image demo demo_image() .. image-sg:: /auto_examples/06_denoise/images/sphx_glr_06_02_2d_image_smoothing_001.png :alt: Original Image, Noisy Image (SNR=10.00 dB), Smoothed Image (SNR=17.69 dB) :srcset: /auto_examples/06_denoise/images/sphx_glr_06_02_2d_image_smoothing_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Image: SNR of noisy image: 10.00 dB SNR after smoothing: 17.69 dB SNR improvement: 7.69 dB Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Got range [-0.6702387206030012..1.76025280954446]. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Got range [-0.09837255519596473..1.1837520472210685]. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.474 seconds) .. _sphx_glr_download_auto_examples_06_denoise_06_02_2d_image_smoothing.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/splineops/splineops.github.io/main?urlpath=lab/tree/notebooks_binder/auto_examples/06_denoise/06_02_2d_image_smoothing.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/06_denoise/06_02_2d_image_smoothing.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_02_2d_image_smoothing.ipynb <06_02_2d_image_smoothing.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_02_2d_image_smoothing.py <06_02_2d_image_smoothing.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06_02_2d_image_smoothing.zip <06_02_2d_image_smoothing.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_