cd /news/computer-vision/beyond-opencv-light-weight-monocular… · home › topics › computer-vision › article
[ARTICLE · art-160720] src=dev.to ↗ pub= topic=computer-vision verified=true sentiment=↑ positive

Beyond OpenCV: Light-Weight Monocular Depth Post-Processing in Pure NumPy

A developer behind Hawk Eye, an industrial edge AI application for monocular tire tread depth assessment, built a pure-NumPy post-processing routine that removes camera perspective tilt from monocular depth maps without OpenCV, SciPy, or Scikit-Image. The method fits a least-squares plane to the depth surface using SVD via np.linalg.lstsq rather than the normal equations, avoiding the ill-conditioning that arises when forming A^T*A on a 224x224 grid, and applies percentile masking so deep grooves don't corrupt the surface fit. The detrended output is then scaled into millimeter tread-depth profiles.

by read5 min views2 publishedOct 30, 2026

When building desktop or edge AI applications, the default instinct for many engineers is to run pip install opencv-python.

However, as your architecture matures toward lightweight, self-contained edge software (such as a PyQt6 GUI app running ONNX Runtime on CPU), third-party C++ wrappers introduce real operational friction:

opencv-python-headless on Linux/Windows x64) that expand desktop installers. In our project — Hawk Eye, an industrial AI edge application for monocular tire tread depth assessment — we enforced a strict rule: Zero CV-specific external dependencies (no OpenCV, SciPy, or Scikit-Image). The pipeline relies strictly on PyQt6, ONNX Runtime, PIL, and pure NumPy.

In this article, we look at how we solve camera perspective tilt in monocular depth maps using simple vector operations in NumPy.

Monocular depth estimation models (like U-Net architectures optimized for edge CPU) map 2D pixels to depth predictions Z_raw in range [0.0, 1.0].

In field diagnostics, handheld devices or smartphone cameras are almost never held perfectly parallel to the tire surface. A 5-degree perspective tilt creates an artificial linear gradient across the depth map, causing the model to mistake perspective for actual physical tread wear.

To fix this, we detrend the baseline reference plane using Ordinary Least Squares (OLS) fitted via SVD.

We model the background surface using a linear 3D equation:

Z(x, y) = a * x + b * y + c

Where (x, y) are spatial pixel coordinates, a, b are surface slopes, and c is the global baseline offset.

Given an ROI matrix Z_raw of size H x W, we set up an overdetermined linear system A * x = b.

In textbook linear algebra, the analytical solution is written via Normal Equations:

x_opt = inv(A^T * A) * A^T * b

In production, forming A^T * A squares the dynamic range of pixel coordinates. For a 224x224 depth grid (N = 50,176 pixels, max coordinate x_max = 223), the condition number of A^T * A reaches 10^4 - 10^5. Explicit matrix inversion leads to numerical instability on low-precision CPU runtimes.

Instead, we use Singular Value Decomposition (SVD) via np.linalg.lstsq. SVD solves the system directly without forming A^T * A, avoiding matrix inversion instability.

Below is the self-contained implementation written purely in NumPy. It fits a least-squares plane, subtracts perspective tilt, and uses percentile masking to prevent deep grooves from corrupting the surface fit.

 python
import numpy as np

def detrend_and_scale_depth(
    depth_raw: np.ndarray, 
    scale_3d: float = 1.80, 
    bias_mm: float = 0.0,
    groove_mask_percentile: float = 40.0
) -> np.ndarray:
    """
    Removes camera perspective tilt using SVD Least Squares Plane Fitting
    and converts raw relative depth predictions into millimeter profile maps.

    Convention: Assumes Z_groove < Z_surface (lower relative height in depth_raw).

    Parameters
    ----------
    depth_raw : np.ndarray
        2D floating-point array (H, W) of relative predictions from ONNX U-Net.
    scale_3d : float
        Optics scale factor (mm per relative depth unit).
    bias_mm : float
        Systemic bias correction in millimeters.
    groove_mask_percentile : float
        Empirical threshold (40%) masking lower depth values (grooves) 
        during plane fitting to prevent fit sags.

    Returns
    -------
    np.ndarray
        2D array (H, W) containing detrended physical tread depth in millimeters.
    """
    h, w = depth_raw.shape

    grid_y, grid_x = np.indices((h, w), dtype=np.float32)

    x_flat = grid_x.ravel()
    y_flat = grid_y.ravel()
    z_flat = depth_raw.ravel()

    A = np.column_stack([x_flat, y_flat, np.ones_like(x_flat)])

    threshold_val = np.percentile(z_flat, groove_mask_percentile)
    mask = z_flat >= threshold_val

    plane_params, _, _, _ = np.linalg.lstsq(A[mask], z_flat[mask], rcond=None)
    a, b, c = plane_params

    z_plane = (a * grid_x + b * grid_y + c)

    z_detrended = depth_raw - z_plane

    baseline_floor = np.percentile(z_detrended, 5)
    z_detrended -= baseline_floor

    depth_mm = (z_detrended * scale_3d) + bias_mm

    return depth_mm

if __name__ == "__main__":
    y, x = np.ogrid[:100, :100]
    simulated_tilt = 0.05 * x + 0.02 * y
    simulated_groove = np.where((x > 40) & (x < 60), -2.0, 0.0)
    raw_input = simulated_tilt + simulated_groove

    depth_mm = detrend_and_scale_depth(raw_input, scale_3d=2.5)

    groove_depth = np.percentile(depth_mm, 95) - np.percentile(depth_mm, 5)

    print(f"Input Range (Tilted): [{raw_input.min():.2f}, {raw_input.max():.2f}]")
    print(f"Corrected Depth Range: [{depth_mm.min():.2f}, {depth_mm.max():.2f}] mm")
    print(f"Measured Groove Depth: {groove_depth:.2f} mm (Expected Synthetic: 5.00 mm)")
Input Range (Tilted): [0.00, 6.93]
Corrected Depth Range: [0.00, 5.00] mm
Measured Groove Depth: 5.00 mm (Expected Synthetic: 5.00 mm)

While linear plane detrending is fast, edge deployment has specific constraints:

groove_mask_percentile=40) to fit strictly to elevated tread blocks (valid for typical 15–35% groove occupancy).z = a*x^2 + b*y^2 + c*x*y + d*x + e*y + f should replace global OLS. Replacing external CV packages with vectorized NumPy operations yielded clear benefits in our PyQt6 runtime:

224x224 depth ROI (median of 100 runs after 10 warm-up iterations on Intel Core i5-1135G7 @ 2.40GHz, OMP_NUM_THREADS=1, NumPy 1.26.4). Linear algebra operations in pure NumPy are often enough for edge post-processing without bundling full C++ vision frameworks.

While synthetic tests verify the math, production calibration against real physical depth gauges (e.g., Mitutoyo 571-201-30) remains necessary to establish the optics scale factor (scale_3d) for different smartphone camera modules.

By combining SVD plane fitting with percentile masking, we achieved stable profile extraction for Hawk Eye — completely independent of external computer vision packages.

── more in #computer-vision 4 stories · sorted by recency
── more on @hawk eye 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/beyond-opencv-light-…] indexed:0 read:5min 2026-10-30 · —