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:
Claude committed 2026-10-02 09:53:03 +00:00
1 parent 02afd4673b
commit 5738fc1694
4 files changed
+178 -44

No files matched your search

+45 -6
View File
@@ -137,26 +137,65 @@ internal static class PlaneOps
});
}
/// <summary>Median over a (2r+1)² window with replicated edges.</summary>
/// <summary>
/// Median over a (2r+1)² window with replicated edges. Interior pixels run the vectorised sorting network
/// on Vector&lt;float&gt; blocks of neighbouring pixels; borders use scalar selection.
/// </summary>
public static void MedianFilter(float[] source, float[] destination, int width, int height, int radius)
{
ForRows(height, width, y =>
{
var size = 2 * radius + 1;
Span<float> window = stackalloc float[size * size];
for (var x = 0; x < width; x++)
var count = size * size;
Span<float> window = stackalloc float[count];
var vc = Vector<float>.Count;
var interior = Vector.IsHardwareAccelerated && y >= radius && y < height - radius;
Span<Vector<float>> lanes = interior ? stackalloc Vector<float>[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<float>(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++;
}
});
}
/// <summary>
/// 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 <paramref name="lanes"/>.
/// </summary>
public static Vector<float> MedianOfLanes(Span<Vector<float>> 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;
}
/// <summary>Exact median (mean of the two middle values for even counts); reorders <paramref name="values"/>.</summary>
public static float Median(Span<float> values)
{