Sub-Pixel Normalized Cross-Correlation for Watermark Detection
Before a mathematical deblending algorithm can invert a watermark, it must pinpoint its exact spatial coordinates down to fractions of a pixel. Discover how Normalized Cross-Correlation (NCC) and quadratic Taylor expansion enable sub-pixel localization across complex photographic backgrounds.
1. The Problem of Watermark Localization
When an AI generation platform like Google AI Studio, Imagen 3, or Google Veo renders a video or image, the watermark's pixel position varies depending on aspect ratio (16:9, 1:1, 9:16, 4:3), resolution scaling, and padding. If a template is offset by merely 0.5 pixels during reverse deblending, the subtraction will miss the alpha contour, creating a distracting bright-and-dark relief shadow (an "emboss artifact").
Consequently, reliable watermark restoration requires sub-pixel accuracy ($< 0.1\text{ px}$) regardless of background luminance, color gradients, or noise.
2. Normalized Cross-Correlation (NCC) Formulation
Template matching evaluates how closely a known template patch $T(u, v)$ matches a region of an input image $I(x + u, y + v)$ over a template domain $W$ of dimensions $K \times M$.
Standard cross-correlation suffers from sensitivity to overall brightness: bright white background regions naturally yield high correlation scores even when no watermark is present. To achieve invariance against local illumination variations, Normalized Cross-Correlation (NCC) subtracts local mean values and divides by standard deviations:
γ(x, y) = Σ [ (I(x+u, y+v) - Ī) × (T(u, v) - T̄) ] / √[ Σ (I(x+u, y+v) - Ī)² × Σ (T(u, v) - T̄)² ]
Where:
- $\bar{T}$: The mean intensity of the watermark template.
- $\bar{I}(x, y)$: The local mean intensity of the image patch centered at $(x, y)$.
- $\gamma(x, y) \in [-1.0, 1.0]$: The correlation coefficient. A value of $+1.0$ indicates a perfect structural match, $0.0$ signifies uncorrelated data, and $-1.0$ indicates perfect inverse correlation.
3. Accelerating Search with Multi-Scale Pyramids
Calculating full NCC across a 4K image ($3840 \times 2160$) with a $120 \times 120$ template requires billions of multiply-accumulate (MAC) operations. To ensure instantaneous performance in the browser (sub-50ms), AURA ERASE employs a multi-resolution Gaussian Scale-Space Pyramid:
- Coarse Detection (Level 2): Downsample both image and template by a factor of 4. Evaluate NCC only within the four candidate corners (bottom-right, bottom-left, top-right, top-left).
- Refinement (Level 1): Using the candidate coordinates from Level 2, bound the search region to a tight $\pm 16$ pixel neighborhood at half resolution.
- Full Resolution Peak Search (Level 0): Execute pixel-level NCC within a $\pm 4$ pixel radius around the best candidate, identifying the discrete integer peak $(x_0, y_0)$.
4. Achieving Sub-Pixel Precision via Quadratic Taylor Expansion
The discrete peak $(x_0, y_0)$ obtained on the pixel grid is limited to integer coordinates. Because image rendering involves continuous physical light sampled through optical sensors or virtual pixel lattices, the true continuous peak $(\hat{x}, \hat{y})$ almost always lies between discrete pixel centers.
To compute the continuous sub-pixel offset $(\Delta x, \Delta y)$, we approximate the local correlation surface $\gamma(x, y)$ around $(x_0, y_0)$ using a 2D second-order Taylor series expansion:
γ(x_0 + Δx, y_0 + Δy) ≈ γ(x_0, y_0) + g^T Δp + ½ Δp^T H Δp
Where $\Delta p = [\Delta x, \Delta y]^T$, $g$ is the spatial gradient vector, and $H$ is the $2 \times 2$ Hessian matrix of second-order spatial partial derivatives:
g = [ ∂γ/∂x, ∂γ/∂y ]^TH = [ [ ∂²γ/∂x², ∂²γ/∂x∂y ], [ ∂²γ/∂x∂y, ∂²γ/∂y² ] ]
Setting the derivative with respect to $\Delta p$ to zero yields the analytical sub-pixel peak position:
Δp = -H^(-1) × g
By solving this linear system using discrete central finite differences, the engine obtains sub-pixel displacements with an accuracy down to $1/50\text{th}$ of a pixel ($0.02\text{ px}$).
5. Dynamic Confidence Thresholding
Not all images contain watermarks, and user uploads might already be clean. A naive peak detector might pick the highest random correlation peak in an image corner and inappropriately trigger a deblend. To prevent false positives, AURA ERASE implements a multi-criteria rejection pipeline:
- Peak Value Threshold: $\gamma_{\text{max}} \ge 0.72$. If the peak correlation is below this threshold, detection rejects the candidate.
- Peak-to-Sidelobe Ratio (PSR): $PSR = (\gamma_{\text{max}} - \mu_{\text{sidelobe}}) / \sigma_{\text{sidelobe}} \ge 8.5$. This ensures the peak is a sharp, isolated impulse rather than high cross-talk across a textured background.
- Edge Gradient Consistency: The orientation of high-gradient vectors along the 4-point star arms must align with the template's normal vector field within $\pm 12^\circ$.
Test Sub-Pixel Detection Live
Upload any Gemini or AI Studio media to watch the sub-pixel detector pinpoint watermark coordinates automatically.
Try Auto-Detection in Studio โ