perf(motion): vectorise the flow median filter and parallelise stabiliser patches
The flow median filter runs the odd-even transposition network on Vector<float> blocks in the interior (shared with the temporal median), which removes the dominant cost of Best-quality flow. The stabiliser analyses its nine patches in parallel with one correlator each and gathers results in patch order, so output stays deterministic. Adds brute-force reference tests for the median, box and binomial kernels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+178
-44
No files matched your search
@@ -58,7 +58,7 @@ public sealed record StabilizationResult(
|
||||
|
||||
/// <summary>
|
||||
/// Video stabilisation. Per frame: Kuglin–Hines phase correlation of nine patches on a 3×3 grid against the
|
||||
/// previous frame, texture gating (noise-compensated gradient energy, Immerkær 1996 noise estimate; a
|
||||
/// previous frame (patches analysed in parallel), texture gating (noise-compensated gradient energy, Immerkær 1996 noise estimate; a
|
||||
/// textureless frame yields no vectors and therefore no motion), then a robust similarity fit (exhaustive
|
||||
/// two-point hypotheses as deterministic RANSAC, plausibility limits, weighted least squares on the
|
||||
/// inliers). <see cref="Solve"/> accumulates the camera path, smooths its translation, rotation and log-scale
|
||||
@@ -71,11 +71,12 @@ public sealed class Stabilizer : IDisposable
|
||||
// Expected smoothed-gradient energy of unit-variance white noise: 2 · Σ([−1 −2 0 2 1]/8)² · Σ([1 2 1]/4)².
|
||||
private const double NoiseGradientGain = 2 * (10.0 / 64) * (6.0 / 16);
|
||||
|
||||
private readonly PhaseCorrelator _correlator;
|
||||
private readonly PhaseCorrelator[] _correlators;
|
||||
private readonly PhaseCorrelation?[] _results = new PhaseCorrelation?[9];
|
||||
private readonly (int X, int Y)[] _patches;
|
||||
private readonly double[] _previousEnergy;
|
||||
private readonly double[] _currentEnergy;
|
||||
private readonly float[] _smooth;
|
||||
private readonly float[][] _smooth;
|
||||
private readonly Match[] _matches = new Match[9];
|
||||
private readonly List<FrameMotion> _motions = [];
|
||||
private float[]? _previous;
|
||||
@@ -91,7 +92,14 @@ public sealed class Stabilizer : IDisposable
|
||||
PatchSize = Options.PatchSize > 0
|
||||
? int.Min(Options.PatchSize, int.Min(width, height))
|
||||
: int.Clamp(1 << BitOperations.Log2((uint)(int.Min(width, height) / 3)), 8, 256);
|
||||
_correlator = new PhaseCorrelator(PatchSize, PatchSize);
|
||||
// One correlator and blur buffer per patch so the nine patches are analysed in parallel.
|
||||
_correlators = new PhaseCorrelator[9];
|
||||
_smooth = new float[9][];
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
_correlators[i] = new PhaseCorrelator(PatchSize, PatchSize);
|
||||
_smooth[i] = new float[PatchSize * PatchSize];
|
||||
}
|
||||
_patches = new (int, int)[9];
|
||||
for (var j = 0; j < 3; j++)
|
||||
{
|
||||
@@ -104,7 +112,6 @@ public sealed class Stabilizer : IDisposable
|
||||
}
|
||||
_previousEnergy = new double[9];
|
||||
_currentEnergy = new double[9];
|
||||
_smooth = new float[PatchSize * PatchSize];
|
||||
_previous = ArrayPool<float>.Shared.Rent(width * height);
|
||||
_current = ArrayPool<float>.Shared.Rent(width * height);
|
||||
}
|
||||
@@ -191,27 +198,27 @@ public sealed class Stabilizer : IDisposable
|
||||
private FrameMotion Analyse()
|
||||
{
|
||||
var current = CurrentPlane;
|
||||
for (var i = 0; i < 9; i++) _currentEnergy[i] = TextureEnergy(current, _patches[i].X, _patches[i].Y);
|
||||
var first = _motions.Count == 0;
|
||||
var previous = _previous!;
|
||||
Parallel.For(0, 9, i =>
|
||||
{
|
||||
var (x0, y0) = _patches[i];
|
||||
_currentEnergy[i] = TextureEnergy(current, x0, y0, _smooth[i]);
|
||||
_results[i] = first || double.Min(_previousEnergy[i], _currentEnergy[i]) < Options.TextureThreshold
|
||||
? null
|
||||
: _correlators[i].Correlate(previous, current, Width, x0, y0);
|
||||
});
|
||||
|
||||
FrameMotion motion;
|
||||
if (_motions.Count == 0)
|
||||
// Gathered in patch order, so the fit does not depend on thread scheduling.
|
||||
var count = 0;
|
||||
var half = (PatchSize - 1) / 2.0;
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
motion = new FrameMotion(Affine2D.Identity, 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
var count = 0;
|
||||
var half = (PatchSize - 1) / 2.0;
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
if (double.Min(_previousEnergy[i], _currentEnergy[i]) < Options.TextureThreshold) continue;
|
||||
var (x0, y0) = _patches[i];
|
||||
var r = _correlator.Correlate(_previous, current, Width, x0, y0);
|
||||
if (r.Confidence < Options.MinimumConfidence) continue;
|
||||
_matches[count++] = new Match(x0 + half, y0 + half, x0 + half + r.Dx, y0 + half + r.Dy, r.Confidence);
|
||||
}
|
||||
motion = FitSimilarity(_matches.AsSpan(0, count));
|
||||
if (_results[i] is not { } r || r.Confidence < Options.MinimumConfidence) continue;
|
||||
var (x0, y0) = _patches[i];
|
||||
_matches[count++] = new Match(x0 + half, y0 + half, x0 + half + r.Dx, y0 + half + r.Dy, r.Confidence);
|
||||
}
|
||||
var motion = first ? new FrameMotion(Affine2D.Identity, 0, 0) : FitSimilarity(_matches.AsSpan(0, count));
|
||||
|
||||
_motions.Add(motion);
|
||||
(_previous, _current) = (_current, _previous);
|
||||
@@ -220,7 +227,7 @@ public sealed class Stabilizer : IDisposable
|
||||
}
|
||||
|
||||
/// <summary>Noise-compensated mean squared gradient of the blurred patch.</summary>
|
||||
private double TextureEnergy(float[] plane, int x0, int y0)
|
||||
private double TextureEnergy(float[] plane, int x0, int y0, float[] smooth)
|
||||
{
|
||||
var p = PatchSize;
|
||||
var w = Width;
|
||||
@@ -234,7 +241,7 @@ public sealed class Stabilizer : IDisposable
|
||||
float a = plane[c - w - 1], b = plane[c - w], d = plane[c - w + 1];
|
||||
float e = plane[c - 1], f = plane[c], g = plane[c + 1];
|
||||
float h = plane[c + w - 1], i = plane[c + w], j = plane[c + w + 1];
|
||||
_smooth[y * p + x] = (a + d + h + j + 2 * (b + e + g + i) + 4 * f) * (1f / 16);
|
||||
smooth[y * p + x] = (a + d + h + j + 2 * (b + e + g + i) + 4 * f) * (1f / 16);
|
||||
laplacian += float.Abs(a + d + h + j - 2 * (b + e + g + i) + 4 * f);
|
||||
}
|
||||
}
|
||||
@@ -247,8 +254,8 @@ public sealed class Stabilizer : IDisposable
|
||||
for (var x = 2; x < p - 2; x++)
|
||||
{
|
||||
var k = y * p + x;
|
||||
var gx = 0.5 * (_smooth[k + 1] - _smooth[k - 1]);
|
||||
var gy = 0.5 * (_smooth[k + p] - _smooth[k - p]);
|
||||
var gx = 0.5 * (smooth[k + 1] - smooth[k - 1]);
|
||||
var gy = 0.5 * (smooth[k + p] - smooth[k - p]);
|
||||
energy += gx * gx + gy * gy;
|
||||
samples++;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user