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. 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 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 1. Generate 2D pixel coordinate grids 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 Design matrix A: Shape N, 3 where N = H W A = np.column stack x flat, y flat, np.ones like x flat 2. Trimmed OLS: Mask out deep grooves to fit plane ONLY on top tread blocks threshold val = np.percentile z flat, groove mask percentile mask = z flat = threshold val 3. Solve for plane parameters a, b, c via SVD lstsq plane params, , , = np.linalg.lstsq A mask , z flat mask , rcond=None a, b, c = plane params 4. Reconstruct background trend plane across full ROI z plane = a grid x + b grid y + c 5. Detrend surface Subtract baseline slope z detrended = depth raw - z plane 6. Robust baseline shift P05 percentile instead of min to reject noise baseline floor = np.percentile z detrended, 5 z detrended -= baseline floor 7. Convert relative depth delta to physical millimeters depth mm = z detrended scale 3d + bias mm return depth mm if name == " main ": Simulate 100x100 tile with camera tilt + 2.0 unit groove 20% of area 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 Process depth map depth mm = detrend and scale depth raw input, scale 3d=2.5 Evaluation using P95 - P05 percentiles Assumes groove area = 5-10% of ROI 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.