Files
Chronos/tests/Chronos.Core.Tests/Motion/OpticalFlowTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

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 Chronos.Core.Imaging;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Motion;
using Chronos.Testing;
namespace Chronos.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;
}
}