feat(motion): add nine-patch stabiliser with texture gating and smoothed path

Per frame, nine phase-correlation patches on a 3x3 grid are gated by
noise-compensated gradient energy (Immerkær noise estimate) and peak
sharpness, then fitted with a similarity by exhaustive two-point
hypotheses and weighted least squares on the inliers. Solve smooths the
camera path with Gaussian-weighted local linear regression, derives
corrections, shake RMS, recommendation (> 0.3 px) and the crop needed to
hide borders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 09:34:56 +00:00
1 parent e3d3e3c729
commit acde397c52
3 files changed
+620

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
namespace Titano.Core.Motion;
/// <summary>Smoothing of per-frame series (camera paths).</summary>
internal static class PathSmoother
{
/// <summary>
/// Gaussian-weighted local linear regression (degree-1 LOESS with a Gaussian kernel truncated at 3σ):
/// each sample is replaced by the value at its own position of a line fitted to its neighbourhood.
/// Linear trends (pans) pass unchanged and the ends are not pulled toward the interior, unlike a plain
/// Gaussian average. σ ≤ 0 returns a copy.
/// </summary>
public static double[] LocalLinear(ReadOnlySpan<double> values, double sigma)
{
var n = values.Length;
var result = values.ToArray();
if (!(sigma > 0) || n < 2) return result;
var radius = (int)double.Ceiling(3 * sigma);
Span<double> kernel = radius <= 1024 ? stackalloc double[radius + 1] : new double[radius + 1];
for (var d = 0; d <= radius; d++) kernel[d] = double.Exp(-0.5 * d * d / (sigma * sigma));
for (var k = 0; k < n; k++)
{
double s0 = 0, s1 = 0, s2 = 0, t0 = 0, t1 = 0;
var lo = int.Max(0, k - radius);
var hi = int.Min(n - 1, k + radius);
for (var j = lo; j <= hi; j++)
{
var d = j - k;
var w = kernel[int.Abs(d)];
s0 += w;
s1 += w * d;
s2 += w * d * d;
t0 += w * values[j];
t1 += w * d * values[j];
}
var det = s0 * s2 - s1 * s1;
result[k] = det > 1e-12 * s0 * s0 ? (s2 * t0 - s1 * t1) / det : t0 / s0;
}
return result;
}
}