.. 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_04_antialiasing.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_04_antialiasing.py: Antialiasing ============ We construct an A/B corner mix image where, in each 2x2 tile, the top-left pixel comes from image A and the other three come from B. Then we downsample by 0.5 using: - Standard interpolation (cubic): a non-anti-aliased decimation. - Least-squares projection (cubic-best anti-aliasing): an anti-aliased decimation. .. GENERATED FROM PYTHON SOURCE LINES 18-20 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 20-30 .. code-block:: Python import numpy as np import requests from io import BytesIO from PIL import Image from splineops.resize.resize import resize from splineops.utils import show_roi_zoom .. GENERATED FROM PYTHON SOURCE LINES 32-35 Load and prepare base ROI ------------------------- Load A and B, convert to grayscale, and define the ROI on the originals. .. GENERATED FROM PYTHON SOURCE LINES 35-68 .. code-block:: Python URL_A = "https://r0k.us/graphics/kodak/kodak/kodim14.png" URL_B = "https://r0k.us/graphics/kodak/kodak/kodim08.png" ROI_SIZE_PX = 64 # original ROI side (pixels) FACE_ROW, FACE_COL = 250, 445 # ROI center (approx) in ORIGINAL coordinates zoom = (0.5, 0.5) # 0.5× downsampling demo def to_gray01(img_rgb_uint8: np.ndarray) -> np.ndarray: g = img_rgb_uint8.astype(np.float64) / 255.0 return 0.2989 * g[..., 0] + 0.5870 * g[..., 1] + 0.1140 * g[..., 2] A = to_gray01(np.array(Image.open(BytesIO(requests.get(URL_A, timeout=10).content)))) B = to_gray01(np.array(Image.open(BytesIO(requests.get(URL_B, timeout=10).content)))) assert A.shape == B.shape, "Images A and B must have identical shape." h_img, w_img = A.shape # ORIGINAL canvas size (e.g., 512×768) # Original ROI (face) — top-left corner (for show_roi_zoom) 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)) # Keep the ROI center as *relative* position for later (downsampled views) rel_center_r = FACE_ROW / h_img rel_center_c = FACE_COL / w_img roi_kwargs_orig = dict( roi_height_frac=ROI_SIZE_PX / h_img, grayscale=True, roi_xy=(row_top, col_left), ) .. GENERATED FROM PYTHON SOURCE LINES 69-71 Image A (with ROI) ------------------ .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python _ = show_roi_zoom(A, ax_titles=("Image A (with ROI)", None), **roi_kwargs_orig) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_001.png :alt: Image A (with ROI) :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-77 Image B (with ROI) ------------------ .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python _ = show_roi_zoom(B, ax_titles=("Image B (with ROI)", None), **roi_kwargs_orig) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_002.png :alt: Image B (with ROI) :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-84 Construct A/B corner mix ------------------------ A fills the top-left of each 2×2 block; the other three pixels come from B. .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: Python mixed = B.copy() mixed[0::2, 0::2] = A[0::2, 0::2] _ = show_roi_zoom(mixed, ax_titles=("A/B corner mix (A at TL of each 2x2)", None), **roi_kwargs_orig) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_003.png :alt: A/B corner mix (A at TL of each 2x2) :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 91-93 Resized (standard) ------------------ .. GENERATED FROM PYTHON SOURCE LINES 93-138 .. code-block:: Python H, W = mixed.shape if (H % 2 == 0) or (W % 2 == 0): mixed_odd = mixed[:H - (H % 2 == 0), :W - (W % 2 == 0)] else: mixed_odd = mixed h_odd, w_odd = mixed_odd.shape assert (h_odd % 2 == 1) and (w_odd % 2 == 1), "Expect odd H×W after the crop." res_std = resize(mixed_odd, zoom_factors=zoom, method="cubic") # standard (cubic) res_ls = resize(mixed_odd, zoom_factors=zoom, method="cubic-best_antialiasing") # least-squares (best AA) def show_resized_on_original_canvas_same_relpos(resized: np.ndarray, title: str): h_res, w_res = resized.shape # EXACT half-size ROI on the resized image roi_h_res = ROI_SIZE_PX // 2 # 64 → 32 roi_w_res = ROI_SIZE_PX // 2 # SAME RELATIVE CENTER as in originals center_r_res = int(round(rel_center_r * h_res)) center_c_res = int(round(rel_center_c * w_res)) # ROI top-left in RESIZED coords, clipped row_top_res = int(np.clip(center_r_res - roi_h_res // 2, 0, h_res - roi_h_res)) col_left_res = int(np.clip(center_c_res - roi_w_res // 2, 0, w_res - roi_w_res)) # Build ORIGINAL-size white canvas and paste resized at (0,0) canvas = np.ones((h_img, w_img), dtype=resized.dtype) canvas[:h_res, :w_res] = resized # Use ORIGINAL canvas height so 32 px is respected visually (no forced shrinking) roi_kwargs_canvas = dict( roi_height_frac=(ROI_SIZE_PX // 2) / h_img, # 32 / original height grayscale=True, roi_xy=(row_top_res, col_left_res), # ROI within the pasted resized patch ) return show_roi_zoom(canvas, ax_titles=(title, None), **roi_kwargs_canvas) _ = show_resized_on_original_canvas_same_relpos( res_std, "Resized (standard cubic) — same relative ROI, 32 px" ) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_004.png :alt: Resized (standard cubic) — same relative ROI, 32 px :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-141 Resized (least-squares, best AA) -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 141-146 .. code-block:: Python _ = show_resized_on_original_canvas_same_relpos( res_ls, "Resized (least-squares, best AA) — same relative ROI, 32 px" ) .. image-sg:: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_005.png :alt: Resized (least-squares, best AA) — same relative ROI, 32 px :srcset: /auto_examples/03_resampling_using_2D_interpolation/images/sphx_glr_03_04_antialiasing_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 147-168 Discussion ---------- In this synthetic A/B mix, each 2×2 block has A at the top-left pixel and B elsewhere (i.e., 25% A, 75% B per block). • Standard interpolation (cubic) does no prefiltering before decimation. With our odd-size tweak, the 0.5× sampling grid lands exactly on the 2×2 block corners (the A pixels). So it effectively *picks* A at every step, yielding the A-by-corners image. That’s not anti-aliasing; it’s just point-sampling at a favorable phase for this pattern. • Least-squares projection (best AA) performs a proper low-pass (anti-aliasing) filtering matched to the downsampling, then decimates. On this pattern, that filter averages over each 2×2 neighborhood, so the result tends toward 25% A + 75% B — visually “more B”, more like the mix. This is exactly what anti-aliasing should do: remove the high-frequency checkerboard content so it doesn’t fold (alias) into the downsample. In short: interpolation without AA does sample-and-aliasing (here it locks onto A due to phase). Least-squares proejction does low-pass-then-sample, preserving what would survive an ideal anti-aliased decimation. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.573 seconds) .. _sphx_glr_download_auto_examples_03_resampling_using_2D_interpolation_03_04_antialiasing.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_04_antialiasing.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_04_antialiasing.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03_04_antialiasing.ipynb <03_04_antialiasing.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_04_antialiasing.py <03_04_antialiasing.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03_04_antialiasing.zip <03_04_antialiasing.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_