diff --git a/src/Titano.Core/Motion/PlaneOps.cs b/src/Titano.Core/Motion/PlaneOps.cs
index b93b65ef..c4c95d52 100644
--- a/src/Titano.Core/Motion/PlaneOps.cs
+++ b/src/Titano.Core/Motion/PlaneOps.cs
@@ -137,26 +137,65 @@ internal static class PlaneOps
});
}
- /// Median over a (2r+1)² window with replicated edges.
+ ///
+ /// Median over a (2r+1)² window with replicated edges. Interior pixels run the vectorised sorting network
+ /// on Vector<float> blocks of neighbouring pixels; borders use scalar selection.
+ ///
public static void MedianFilter(float[] source, float[] destination, int width, int height, int radius)
{
ForRows(height, width, y =>
{
var size = 2 * radius + 1;
- Span window = stackalloc float[size * size];
- for (var x = 0; x < width; x++)
+ var count = size * size;
+ Span window = stackalloc float[count];
+ var vc = Vector.Count;
+ var interior = Vector.IsHardwareAccelerated && y >= radius && y < height - radius;
+ Span> lanes = interior ? stackalloc Vector[count] : default;
+ for (var x = 0; x < width;)
{
- var n = 0;
+ if (interior && x >= radius && x + vc + radius <= width)
+ {
+ var n = 0;
+ for (var dy = -radius; dy <= radius; dy++)
+ {
+ var row = (y + dy) * width + x;
+ for (var dx = -radius; dx <= radius; dx++) lanes[n++] = new Vector(source, row + dx);
+ }
+ MedianOfLanes(lanes).CopyTo(destination, y * width + x);
+ x += vc;
+ continue;
+ }
+ var k = 0;
for (var dy = -radius; dy <= radius; dy++)
{
var row = int.Clamp(y + dy, 0, height - 1) * width;
- for (var dx = -radius; dx <= radius; dx++) window[n++] = source[row + int.Clamp(x + dx, 0, width - 1)];
+ for (var dx = -radius; dx <= radius; dx++) window[k++] = source[row + int.Clamp(x + dx, 0, width - 1)];
}
- destination[y * width + x] = Select(window, n / 2);
+ destination[y * width + x] = Select(window, count / 2);
+ x++;
}
});
}
+ ///
+ /// Lane-wise exact median of the vectors (mean of the middle pair for even counts) by odd–even
+ /// transposition sort (Habermann 1972): n passes of branch-free min/max; reorders .
+ ///
+ public static Vector MedianOfLanes(Span> lanes)
+ {
+ var n = lanes.Length;
+ for (var pass = 0; pass < n; pass++)
+ {
+ for (var k = pass & 1; k + 1 < n; k += 2)
+ {
+ var lo = Vector.Min(lanes[k], lanes[k + 1]);
+ lanes[k + 1] = Vector.Max(lanes[k], lanes[k + 1]);
+ lanes[k] = lo;
+ }
+ }
+ return (n & 1) != 0 ? lanes[n / 2] : (lanes[n / 2 - 1] + lanes[n / 2]) * 0.5f;
+ }
+
/// Exact median (mean of the two middle values for even counts); reorders .
public static float Median(Span values)
{
diff --git a/src/Titano.Core/Motion/Stabilizer.cs b/src/Titano.Core/Motion/Stabilizer.cs
index 7bb44d60..2cc05e26 100644
--- a/src/Titano.Core/Motion/Stabilizer.cs
+++ b/src/Titano.Core/Motion/Stabilizer.cs
@@ -58,7 +58,7 @@ public sealed record StabilizationResult(
///
/// 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). 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 _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.Shared.Rent(width * height);
_current = ArrayPool.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
}
/// Noise-compensated mean squared gradient of the blurred patch.
- 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++;
}
diff --git a/src/Titano.Core/Motion/TemporalStacker.cs b/src/Titano.Core/Motion/TemporalStacker.cs
index 27a965a7..aec5f762 100644
--- a/src/Titano.Core/Motion/TemporalStacker.cs
+++ b/src/Titano.Core/Motion/TemporalStacker.cs
@@ -18,6 +18,7 @@ public enum StackMode
/// combines the frames currently in the window. Medians are exact: for up to
/// frames an odd–even transposition sorting network runs on Vector<float>
/// lanes (many pixels per instruction, no branches), beyond that Hoare's selection runs per pixel.
+/// Rows of every channel run in parallel.
///
public sealed class TemporalStacker : IDisposable
{
@@ -100,17 +101,7 @@ public sealed class TemporalStacker : IDisposable
{
var at = offset + i;
for (var k = 0; k < n; k++) lanes[k] = new Vector(planes[k], at);
- // n passes of odd–even transposition sort every lane.
- for (var pass = 0; pass < n; pass++)
- {
- for (var k = pass & 1; k + 1 < n; k += 2)
- {
- var lo = Vector.Min(lanes[k], lanes[k + 1]);
- lanes[k + 1] = Vector.Max(lanes[k], lanes[k + 1]);
- lanes[k] = lo;
- }
- }
- var median = (n & 1) != 0 ? lanes[n / 2] : (lanes[n / 2 - 1] + lanes[n / 2]) * 0.5f;
+ var median = PlaneOps.MedianOfLanes(lanes);
median.CopyTo(dst, at);
}
}
diff --git a/tests/Titano.Core.Tests/Motion/PlaneOpsTests.cs b/tests/Titano.Core.Tests/Motion/PlaneOpsTests.cs
new file mode 100644
index 00000000..959fab08
--- /dev/null
+++ b/tests/Titano.Core.Tests/Motion/PlaneOpsTests.cs
@@ -0,0 +1,97 @@
+using Titano.Core.Motion;
+using Titano.Testing;
+
+namespace Titano.Core.Tests.Motion;
+
+public sealed class PlaneOpsTests
+{
+ private const int Width = 37;
+ private const int Height = 23;
+
+ [Theory]
+ [Case(1)]
+ [Case(2)]
+ public void MedianFilterMatchesBruteForce(int radius)
+ {
+ var source = RandomPlane(seed: radius);
+ var output = new float[source.Length];
+ PlaneOps.MedianFilter(source, output, Width, Height, radius);
+ for (var y = 0; y < Height; y++)
+ {
+ for (var x = 0; x < Width; x++)
+ {
+ var window = new List();
+ for (var dy = -radius; dy <= radius; dy++)
+ for (var dx = -radius; dx <= radius; dx++)
+ window.Add(source[int.Clamp(y + dy, 0, Height - 1) * Width + int.Clamp(x + dx, 0, Width - 1)]);
+ window.Sort();
+ Assert.Equal(window[window.Count / 2], output[y * Width + x]);
+ }
+ }
+ }
+
+ [Theory]
+ [Case(1)]
+ [Case(3)]
+ public void BoxFilterMatchesBruteForce(int radius)
+ {
+ var source = RandomPlane(seed: 10 + radius);
+ var output = new float[source.Length];
+ PlaneOps.BoxFilter(source, output, new float[source.Length], Width, Height, radius);
+ for (var y = 0; y < Height; y++)
+ {
+ for (var x = 0; x < Width; x++)
+ {
+ double sum = 0;
+ for (var dy = -radius; dy <= radius; dy++)
+ for (var dx = -radius; dx <= radius; dx++)
+ sum += source[int.Clamp(y + dy, 0, Height - 1) * Width + int.Clamp(x + dx, 0, Width - 1)];
+ Assert.Approximately(sum / ((2 * radius + 1) * (2 * radius + 1)), output[y * Width + x], 1e-5);
+ }
+ }
+ }
+
+ [Test]
+ public void BinomialBlurKeepsConstantsAndMean()
+ {
+ var flat = new float[Width * Height];
+ flat.AsSpan().Fill(0.7f);
+ PlaneOps.Binomial5(flat, flat, new float[flat.Length], Width, Height);
+ Assert.True(flat.All(v => double.Abs(v - 0.7) < 1e-6));
+
+ var impulse = new float[Width * Height];
+ impulse[11 * Width + 17] = 1;
+ var blurred = new float[impulse.Length];
+ PlaneOps.Binomial5(impulse, blurred, new float[impulse.Length], Width, Height);
+ Assert.Approximately(1, blurred.Sum(v => (double)v), 1e-6);
+ Assert.Approximately(36.0 / 256, blurred[11 * Width + 17], 1e-7);
+ }
+
+ [Test]
+ public void SelectionAndMedian()
+ {
+ var values = new float[] { 5, 1, 4, 2, 3 };
+ Assert.Equal(3f, PlaneOps.Median(values));
+ Assert.Equal(2.5f, PlaneOps.Median(new float[] { 4, 1, 3, 2 }));
+ Assert.Equal(1f, PlaneOps.Select(new float[] { 3, 1, 2 }, 0));
+ Assert.Equal(7f, PlaneOps.Median(new float[] { 7 }));
+ }
+
+ [Test]
+ public void BilinearIsExactAtPixelCentresAndClamps()
+ {
+ var plane = RandomPlane(seed: 3);
+ Assert.Equal(plane[5 * Width + 9], PlaneOps.Bilinear(plane, Width, Height, 9, 5));
+ Assert.Equal(plane[0], PlaneOps.Bilinear(plane, Width, Height, -3, -8));
+ var mid = 0.5f * (plane[5 * Width + 9] + plane[5 * Width + 10]);
+ Assert.Approximately(mid, PlaneOps.Bilinear(plane, Width, Height, 9.5f, 5), 1e-6);
+ }
+
+ private static float[] RandomPlane(int seed)
+ {
+ var random = new Random(seed);
+ var plane = new float[Width * Height];
+ for (var i = 0; i < plane.Length; i++) plane[i] = (float)random.NextDouble();
+ return plane;
+ }
+}