.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/general_examples/plot_04_empr_3d.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_auto_examples_general_examples_plot_04_empr_3d.py: EMPR on an 3D Tensor ==================== Load an RGB image as a 3D tensor, run EMPR, reconstruct the tensor, and compare the reconstruction with the original image. .. GENERATED FROM PYTHON SOURCE LINES 8-82 .. image-sg:: /auto_examples/general_examples/images/sphx_glr_plot_04_empr_3d_001.png :alt: Original image, EMPR reconstruction (order=3), Mean absolute error :srcset: /auto_examples/general_examples/images/sphx_glr_plot_04_empr_3d_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Image path: /home/runner/work/HDMRLib/HDMRLib/docs/_static/examples/squirell.png Input shape: (160, 160, 3) Reconstructed shape: (160, 160, 3) Available component keys: ['g_0', 'g_1', 'g_2', 'g_3', 'g_1,2', 'g_1,3', 'g_2,3', 'g_1,2,3'] Mean absolute error: 3.2556784236118884e-18 (np.float64(-0.5), np.float64(159.5), np.float64(159.5), np.float64(-0.5)) | .. code-block:: Python from pathlib import Path import matplotlib.pyplot as plt import numpy as np import os from PIL import Image from hdmrlib import EMPR candidates = [ Path(os.environ.get("GITHUB_WORKSPACE", "")) / "docs" / "_static" / "examples" / "squirell.png", Path("docs/_static/examples/squirell.png"), Path("_static/examples/squirell.png"), ] image_path = None for candidate in candidates: if str(candidate) and candidate.exists(): image_path = candidate break if image_path is None: raise FileNotFoundError( "Could not find squirell.png. Checked:\n" + "\n".join(str(p) for p in candidates) ) # Load image img = Image.open(image_path).convert("RGB") max_size = 160 img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS) X = np.asarray(img, dtype=np.float64) / 255.0 # RGB image is a 3D tensor, so use order=3 empr = EMPR(X, order=3) X_reconstructed = np.asarray(empr.reconstruct(), dtype=np.float64) components = empr.components() # Clip for display X_display = np.clip(X, 0.0, 1.0) X_reconstructed_display = np.clip(X_reconstructed, 0.0, 1.0) # Mean absolute error across RGB channels error_map = np.mean(np.abs(X_display - X_reconstructed_display), axis=2) print("Image path:", image_path) print("Input shape:", X.shape) print("Reconstructed shape:", X_reconstructed.shape) print("Available component keys:", list(components.keys())) print("Mean absolute error:", float(np.mean(error_map))) # Create three equal-sized image axes + one separate colorbar axis. fig = plt.figure(figsize=(13, 4.2), constrained_layout=True) gs = fig.add_gridspec(1, 4, width_ratios=[1, 1, 1, 0.05]) ax0 = fig.add_subplot(gs[0, 0]) ax1 = fig.add_subplot(gs[0, 1]) ax2 = fig.add_subplot(gs[0, 2]) cax = fig.add_subplot(gs[0, 3]) # Use nearest to avoid display blur in docs/screenshots. ax0.imshow(X_display, interpolation="nearest") ax0.set_title("Original image") ax0.axis("off") ax1.imshow(X_reconstructed_display, interpolation="nearest") ax1.set_title("EMPR reconstruction (order=3)") ax1.axis("off") im = ax2.imshow(error_map, cmap="viridis", interpolation="nearest") ax2.set_title("Mean absolute error") ax2.axis("off") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.446 seconds) .. _sphx_glr_download_auto_examples_general_examples_plot_04_empr_3d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_empr_3d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_empr_3d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_empr_3d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_