Files
Chronos/src/Titano.Core/Motion/PathSmoother.cs
T
Claude acde397c52 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
2026-10-02 09:34:56 +00:00

41 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}