.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_resampling_using_2D_interpolation/03_06_compare_different_methods.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_03_resampling_using_2D_interpolation_03_06_compare_different_methods.py: Compare Different Methods ========================= A summary of the cost/benefit tradeoff of the three interpolation methods is provided in this example. .. GENERATED FROM PYTHON SOURCE LINES 14-16 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 16-28 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt import requests from io import BytesIO from PIL import Image from splineops.utils import ( resize_and_compute_metrics, # resampling + metrics show_roi_zoom, ) .. GENERATED FROM PYTHON SOURCE LINES 29-34 Load and Normalize an Image --------------------------- Here, we load an example image from an online repository. We convert it to grayscale in [0, 1]. .. GENERATED FROM PYTHON SOURCE LINES 34-69 .. code-block:: Python url = 'https://r0k.us/graphics/kodak/kodak/kodim14.png' response = requests.get(url) img = Image.open(BytesIO(response.content)) data = np.array(img, dtype=np.float64) # Convert to [0..1] input_image_normalized = data / 255.0 # Convert to grayscale via simple weighting input_image_normalized = ( input_image_normalized[:, :, 0] * 0.2989 + # Red channel input_image_normalized[:, :, 1] * 0.5870 + # Green channel input_image_normalized[:, :, 2] * 0.1140 # Blue channel ) zoom_factors_2d = (0.25, 0.25) border_fraction = 0.3 # --- ROI: match the LS/Oblique examples --- ROI_SIZE_PX = 64 FACE_ROW, FACE_COL = 400, 600 # ROI center in ORIGINAL coordinates h_img, w_img = input_image_normalized.shape row_top = int(np.clip(FACE_ROW - ROI_SIZE_PX // 2, 0, h_img - ROI_SIZE_PX)) col_left = int(np.clip(FACE_COL - ROI_SIZE_PX // 2, 0, w_img - ROI_SIZE_PX)) roi_rect = (row_top, col_left, ROI_SIZE_PX, ROI_SIZE_PX) # (r, c, h, w) # Reusable kwargs for consistent ROI zooms below roi_kwargs = dict( roi_height_frac=ROI_SIZE_PX / h_img, grayscale=True, roi_xy=(row_top, col_left), ) .. GENERATED FROM PYTHON SOURCE LINES 70-74 Standard Interpolation ---------------------- We use our standard interpolation method. .. GENERATED FROM PYTHON SOURCE LINES 74-89 .. code-block:: Python ( resized_2d_interp, recovered_2d_interp, snr_2d_interp, mse_2d_interp, time_2d_interp ) = resize_and_compute_metrics( input_image_normalized, method="cubic", zoom_factors=zoom_factors_2d, border_fraction=border_fraction, roi=roi_rect, # <-- compute metrics on the shared ROI ) .. GENERATED FROM PYTHON SOURCE LINES 90-94 Least-Squares Projection ------------------------ We use the least-squares projection method. .. GENERATED FROM PYTHON SOURCE LINES 94-109 .. code-block:: Python ( resized_2d_ls, recovered_2d_ls, snr_2d_ls, mse_2d_ls, time_2d_ls ) = resize_and_compute_metrics( input_image_normalized, method="cubic-best_antialiasing", zoom_factors=zoom_factors_2d, border_fraction=border_fraction, roi=roi_rect, # <-- compute metrics on the shared ROI ) .. GENERATED FROM PYTHON SOURCE LINES 110-114 Oblique Projection ------------------ We use the oblique-projection method. .. GENERATED FROM PYTHON SOURCE LINES 114-129 .. code-block:: Python ( resized_2d_ob, recovered_2d_ob, snr_2d_ob, mse_2d_ob, time_2d_ob ) = resize_and_compute_metrics( input_image_normalized, method="cubic-fast_antialiasing", zoom_factors=zoom_factors_2d, border_fraction=border_fraction, roi=roi_rect, # <-- compute metrics on the shared ROI ) .. GENERATED FROM PYTHON SOURCE LINES 130-134 Comparison ---------- We compare the performance of the different methods being analyzed. .. GENERATED FROM PYTHON SOURCE LINES 136-140 Comparison Table ~~~~~~~~~~~~~~~~ We print the SNR, MSE, and timing data for each method. .. GENERATED FROM PYTHON SOURCE LINES 140-157 .. code-block:: Python methods = [ ("Standard Interpolation", snr_2d_interp, mse_2d_interp, time_2d_interp), ("Least-Squares Projection", snr_2d_ls, mse_2d_ls, time_2d_ls), ("Oblique Projection", snr_2d_ob, mse_2d_ob, time_2d_ob), ] # Print the table header using the same widths as we'll use for data header_line = f"{'Method':<25} {'SNR (dB)':>10} {'MSE':>16} {'Time (s)':>12}" print(header_line) print("-" * len(header_line)) # or manually set a dash length, e.g. 67 # Now print each row with the matching format for method_name, snr_val, mse_val, time_val in methods: row_line = f"{method_name:<25} {snr_val:>10.2f} {mse_val:>16.2e} {time_val:>12.4f}" print(row_line) .. rst-class:: sphx-glr-script-out .. code-block:: none Method SNR (dB) MSE Time (s) ------------------------------------------------------------------ Standard Interpolation 12.15 2.20e-02 0.0187 Least-Squares Projection 14.28 1.35e-02 1.4392 Oblique Projection 14.24 1.36e-02 1.1208 .. GENERATED FROM PYTHON SOURCE LINES 158-160 All Methods ~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 160-177 .. code-block:: Python recovered_stack = [ ("Standard (Cubic)", recovered_2d_interp, snr_2d_interp, mse_2d_interp), ("Least-Squares (Best)", recovered_2d_ls, snr_2d_ls, mse_2d_ls), ("Oblique (Fast AA)", recovered_2d_ob, snr_2d_ob, mse_2d_ob), ] fig, axes = plt.subplots(3, 1, figsize=(8, 12)) for ax, (label, img, snr_val, mse_val) in zip(axes, recovered_stack): ax.imshow(img, cmap="gray", aspect="equal") ax.set_title(f"{label}\nSNR: {snr_val:.2f} dB · MSE: {mse_val:.2e}") ax.axis("off") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_001.png :alt: Standard (Cubic) SNR: 12.15 dB · MSE: 2.20e-02, Least-Squares (Best) SNR: 14.28 dB · MSE: 1.35e-02, Oblique (Fast AA) SNR: 14.24 dB · MSE: 1.36e-02 :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 178-180 Standard Interpolation ~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 180-187 .. code-block:: Python _ = show_roi_zoom( recovered_2d_interp, # image to inspect ax_titles=("Standard (Cubic)", None), # customise left title; right auto **roi_kwargs ) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_002.png :alt: Standard (Cubic) :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 188-190 Least-Squares Projection ~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 190-197 .. code-block:: Python _ = show_roi_zoom( recovered_2d_ls, # image to inspect ax_titles=("Least-Squares (Best)", None), # customise left title; right auto **roi_kwargs ) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_003.png :alt: Least-Squares (Best) :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 198-200 Oblique Projection ~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 200-205 .. code-block:: Python _ = show_roi_zoom( recovered_2d_ob, # image to inspect ax_titles=("Oblique (Fast AA)", None), # customise left title; right auto **roi_kwargs ) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_004.png :alt: Oblique (Fast AA) :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_06_compare_different_methods_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.684 seconds) .. _sphx_glr_download_auto_examples_03_resampling_using_2D_interpolation_03_06_compare_different_methods.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/03_resampling_using_2D_interpolation/03_06_compare_different_methods.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/03_resampling_using_2D_interpolation/03_06_compare_different_methods.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03_06_compare_different_methods.ipynb <03_06_compare_different_methods.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_06_compare_different_methods.py <03_06_compare_different_methods.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03_06_compare_different_methods.zip <03_06_compare_different_methods.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_