imgproc-filter · safe CPU

Filtering and paired Sobel gradients

Generic correlation and separable filters remain available for arbitrary components and channels. Interleaved u8 additionally gets accelerated Gaussian kernels, direct 3×3 Sobel, exact paired gradients, fused absolute/binary responses, and fused L1 magnitude.

Contracts

spatial_gradient_u8 matches OpenCV spatialGradient: grayscale u8 input, signed i16 X/Y outputs, 3×3 Sobel coefficients, and Replicate or Reflect101 borders. Its *_into form writes caller-owned packed slices.

sobel_l1_magnitude_u8 computes abs(Gx) + abs(Gy) directly as non-negative i16 values in [0, 2040]. It does not materialize public X/Y intermediates. Python exposes the same operations as spatial_gradient_image and sobel_l1_magnitude_image(..., out=).

sobel_3x3_u8 writes a first-order X or Y response directly to f32 from parallel three-row i16 rings. sobel_abs_3x3_u8 saturates the absolute response to u8, while sobel_threshold_3x3_u8 also produces a binary mask in the same pass. Python exposes caller-owned out= forms.

gaussian_blur_u8 accepts 3×3, 5×5, or 7×7 kernels and all five CPU border modes. The accelerated 3×3/5×5 path uses symmetric Q8 coefficients, branch-free unrolled interiors, bounded row parallelism, and a caller-owned GaussianBlurU8Workspace. The 7×7 contract currently uses the high-precision generic fallback.

Large packed images use bounded Rayon row stages and runtime-dispatched safe SIMD context. Strided inputs retain exact row semantics. CPU calls never upload to a device.

Measured boundary

1.86×

1080p allocated fused L1 lead over OpenCV 4.13 on the recorded Windows host.

2.19×

4K allocated fused L1 lead with exact int16 output.

2.42×

8K allocated fused L1 lead; 300 randomized cases are bit-exact.

OpenCV uses spatialGradient, two absdiff stages, and add. SpatialRust fuses that public workload into one traversal and one result allocation. Reuse is effectively tied at 1080p while OpenCV leads at 4K and 8K, and OpenCV remains faster for standalone paired gradients.

The direct CV_32F-compatible Sobel path combines three-row rings with zero-copy packed NumPy input. It is 1.88× faster than OpenCV at 1080p and 2.03× at 4K with exact values; VGA remains a narrow OpenCV win. The fused abs(Sobel X) > 96 mask wins 3.81×–6.64× allocated and 2.95×–8.68× with caller-owned output. Reproduce with the threshold harness.

Reproduce with the focused harness and inspect the dated receipt.

Gaussian progress

20.7×

1080p allocated 5×5 improvement over the previous generic SpatialRust engine.

26.7×

4K allocated improvement; workspace reuse improves the old path by about 65.9×.

0

Canonical 5×5 max error against OpenCV; 300 mixed-size randomized cases stay within 2/255.

Standalone OpenCV Gaussian remains faster: 3.10× at 1080p, 2.93× at 4K, and 3.58× at 8K for allocated Python calls. The next optimization boundary is high-precision 7×7 and useful fused consumers, not a false standalone-win claim. Reproduce with the Gaussian harness.