Files
Chronos/tests/Titano.Core.Tests/Motion/OpticalFlowTests.cs
T

156 lines
6.3 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.
using Titano.Core.Imaging;
using Titano.Core.Imaging.Geometry;
using Titano.Core.Motion;
using Titano.Testing;
namespace Titano.Core.Tests.Motion;
public sealed class OpticalFlowTests
{
private const int Base = 256;
private const int Frame = 192;
private const int Margin = 16;
private readonly TestContext _context;
public OpticalFlowTests(TestContext context) => _context = context;
[Theory]
[Case(FlowQuality.Draft)]
[Case(FlowQuality.Balanced)]
[Case(FlowQuality.Best)]
public void TranslationEndPointErrorBelowAThirdOfAPixel(FlowQuality quality)
{
const double dx = 2.3, dy = -1.7;
var texture = SyntheticImages.Natural(Base, seed: 11);
using var a = SyntheticImages.ToRgb(SyntheticImages.Crop(texture, Base, 32, 32, Frame, Frame), Frame, Frame);
using var b = SyntheticImages.ToRgb(SyntheticImages.Crop(SyntheticImages.Shift(texture, Base, dx, dy), Base, 32, 32, Frame, Frame), Frame, Frame);
using var engine = new OpticalFlowEngine(quality);
using var field = engine.Compute(a, b);
var epe = MeanEndPointError(field, (_, _) => (dx, dy));
_context.Log($"{quality}: step {field.Step}, mean EPE {epe:F4} px");
Assert.Less(epe, 0.3);
}
[Test]
public void RotationEndPointErrorBelowOnePixel()
{
var angle = 3.0 * double.Pi / 180;
var texture = SyntheticImages.Natural(Base, seed: 5);
using var baseImage = SyntheticImages.ToRgb(SyntheticImages.Crop(texture, Base, 0, 0, Base, Base), Base, Base);
using var rotated = new ImageBuffer(Base, Base);
const double c = (Base - 1) / 2.0;
// rotated(x) = base(R⁻¹(x − c) + c): content turns by +angle about the centre.
GeometryStage.Warp(baseImage, rotated, Affine2D.About(Affine2D.Rotation(-angle), c, c));
using var a = Crop(baseImage, 32);
using var b = Crop(rotated, 32);
using var engine = new OpticalFlowEngine(FlowQuality.Best);
using var field = engine.Compute(a, b);
var truth = Affine2D.About(Affine2D.Rotation(angle), c - 32, c - 32);
var epe = MeanEndPointError(field, (x, y) =>
{
var (tx, ty) = truth.Apply(x, y);
return (tx - x, ty - y);
});
_context.Log($"rotation 3°: mean EPE {epe:F4} px");
Assert.Less(epe, 1.0);
}
[Test]
public void ForwardBackwardErrorIsSmallForConsistentMotion()
{
var texture = SyntheticImages.Natural(Base, seed: 2);
using var a = SyntheticImages.ToRgb(SyntheticImages.Crop(texture, Base, 32, 32, Frame, Frame), Frame, Frame);
using var b = SyntheticImages.ToRgb(SyntheticImages.Crop(SyntheticImages.Shift(texture, Base, -1.5, 0.75), Base, 32, 32, Frame, Frame), Frame, Frame);
using var engine = new OpticalFlowEngine(FlowQuality.Balanced);
var (forward, backward) = engine.ComputeBidirectional(a, b);
using (forward)
using (backward)
{
Assert.True(forward.HasConsistency);
var stats = forward.Statistics(reliableError: 0.5);
_context.Log($"reliable {stats.ReliableFraction:P1}, mean |w| {stats.MeanMagnitude:F3}");
Assert.Greater(stats.ReliableFraction, 0.85);
Assert.Approximately(1.677, stats.MedianMagnitude, 0.1);
Assert.Less(forward.SampleError(96, 96), 0.2);
var (bu, bv) = backward.Sample(96, 96);
Assert.Within(1.5, -0.75, bu, bv, 0.2);
}
}
[Test]
public void FieldSamplingHistogramAndStatistics()
{
using var field = new MotionField(9, 5, 4);
Assert.Equal(3, field.Columns);
Assert.Equal(2, field.Rows);
for (var j = 0; j < field.Rows; j++)
for (var i = 0; i < field.Columns; i++)
{
field.U[j * field.Columns + i] = i;
field.V[j * field.Columns + i] = 2 * j;
}
var (u, v) = field.Sample(6, 2);
Assert.Approximately(1.5, u, 1e-6);
Assert.Approximately(1, v, 1e-6);
Assert.True(float.IsNaN(field.SampleError(0, 0)));
field.Fill(3, 4);
var histogram = field.MagnitudeHistogram(10, 10);
Assert.Equal(6, histogram[5]);
Assert.Equal(6, field.MagnitudeHistogram(4, 2)[3]);
var stats = field.Statistics();
Assert.Approximately(5, stats.MeanMagnitude, 1e-6);
Assert.Approximately(5, stats.P95Magnitude, 1e-6);
Assert.Approximately(3, stats.MeanU, 1e-6);
Assert.Equal(1.0, stats.ReliableFraction);
}
[Test]
public void PyramidLevelsHalveAndPreserveMean()
{
var texture = SyntheticImages.Crop(SyntheticImages.Noise(Base, 1.5, seed: 8), Base, 0, 0, 101, 75);
using var pyramid = new GrayPyramid(101, 75, GrayPyramid.LevelsFor(101, 75));
Assert.Equal(3, pyramid.Levels);
pyramid.Build(texture);
Assert.Equal(51, pyramid.LevelWidth(1));
Assert.Equal(38, pyramid.LevelHeight(1));
Assert.Equal(26, pyramid.LevelWidth(2));
var mean0 = pyramid.Level(0).ToArray().Average();
var mean2 = pyramid.Level(2).ToArray().Average();
Assert.Approximately(mean0, mean2, 0.02);
var flat = new float[64 * 64];
flat.AsSpan().Fill(0.25f);
using var constant = new GrayPyramid(64, 64, 3);
constant.Build(flat);
Assert.True(constant.Level(2).ToArray().All(x => x == 0.25f));
}
private static double MeanEndPointError(MotionField field, Func<double, double, (double U, double V)> truth)
{
double sum = 0;
var n = 0;
for (var j = 0; j < field.Rows; j++)
{
for (var i = 0; i < field.Columns; i++)
{
double x = i * field.Step, y = j * field.Step;
if (x < Margin || y < Margin || x >= Frame - Margin || y >= Frame - Margin) continue;
var (tu, tv) = truth(x, y);
var k = j * field.Columns + i;
sum += double.Hypot(field.U[k] - tu, field.V[k] - tv);
n++;
}
}
return sum / n;
}
private static ImageBuffer Crop(ImageBuffer source, int offset)
{
var crop = new ImageBuffer(Frame, Frame);
GeometryStage.Warp(source, crop, Affine2D.Translation(offset, offset));
return crop;
}
}