Files
Chronos/tests/Titano.Core.Tests/Motion/PhaseCorrelatorTests.cs
T
Claude bda76ccff2 feat(motion): add FFT plans and Kuglin–Hines phase correlation
Radix-2 Cooley–Tukey with SIMD butterflies and Bluestein chirp-z for
arbitrary lengths, cached plans, parallel 2D transforms, real-input
half-length transforms and Tukey/Hann windows. Phase correlation packs
both patches into one complex FFT, normalises the cross-power spectrum
with a Gaussian low-pass and refines the peak with a log-parabola fit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:19:18 +00:00

84 lines
3.5 KiB
C#

using Titano.Core.Motion;
using Titano.Testing;
namespace Titano.Core.Tests.Motion;
public sealed class PhaseCorrelatorTests
{
private const int Size = 256;
private const int Patch = 128;
private readonly TestContext _context;
public PhaseCorrelatorTests(TestContext context) => _context = context;
[Theory]
[Case("noise", 3.25, -1.6)]
[Case("noise", 0.5, 0.5)]
[Case("noise", -7.8, 2.1)]
[Case("noise", 0.0, 0.0)]
[Case("natural", 3.25, -1.6)]
[Case("natural", 0.5, 0.5)]
[Case("natural", -7.8, 2.1)]
[Case("natural", 12.3, -9.45)]
public void SubPixelErrorWithinTenthOfPixel(string texture, double dx, double dy)
{
var image = texture == "noise" ? SyntheticImages.Noise(Size, 0.8, seed: 7) : SyntheticImages.Natural(Size, seed: 7);
var shifted = SyntheticImages.Shift(image, Size, dx, dy);
var reference = SyntheticImages.Crop(image, Size, 64, 64, Patch, Patch);
var moving = SyntheticImages.Crop(shifted, Size, 64, 64, Patch, Patch);
var result = new PhaseCorrelator(Patch, Patch).Correlate(reference, moving);
var error = double.Hypot(result.Dx - dx, result.Dy - dy);
_context.Log($"{texture} ({dx}, {dy}) → ({result.Dx:F4}, {result.Dy:F4}) error {error:F4} px, confidence {result.Confidence:F3}, peak {result.Peak:F3}");
Assert.LessOrEqual(error, 0.10);
Assert.Greater(result.Confidence, 0.5);
}
[Test]
public void NonPowerOfTwoPatchesUseBluestein()
{
var image = SyntheticImages.Natural(Size, seed: 3);
var shifted = SyntheticImages.Shift(image, Size, -2.4, 1.3);
var reference = SyntheticImages.Crop(image, Size, 60, 70, 96, 80);
var moving = SyntheticImages.Crop(shifted, Size, 60, 70, 96, 80);
var result = new PhaseCorrelator(96, 80).Correlate(reference, moving);
Assert.Within(-2.4, 1.3, result.Dx, result.Dy, 0.10);
}
[Test]
public void PatchesAreReadFromLargerPlanes()
{
var image = SyntheticImages.Noise(Size, 1.0, seed: 9);
var shifted = SyntheticImages.Shift(image, Size, 1.75, -0.25);
var a = SyntheticImages.Crop(image, Size, 0, 0, Size, Size);
var b = SyntheticImages.Crop(shifted, Size, 0, 0, Size, Size);
var result = new PhaseCorrelator(64, 64).Correlate(a, b, Size, 100, 40);
Assert.Within(1.75, -0.25, result.Dx, result.Dy, 0.10);
}
[Test]
public void UnrelatedContentHasLowConfidence()
{
var a = SyntheticImages.Crop(SyntheticImages.Noise(Size, 0.8, seed: 1), Size, 0, 0, Patch, Patch);
var b = SyntheticImages.Crop(SyntheticImages.Noise(Size, 0.8, seed: 2), Size, 0, 0, Patch, Patch);
var result = new PhaseCorrelator(Patch, Patch).Correlate(a, b);
_context.Log($"confidence {result.Confidence:F3}, peak {result.Peak:F3}");
Assert.Less(result.Confidence, 0.4);
Assert.Less(result.Peak, 0.2);
}
[Test]
public void MaxShiftLimitsTheSearch()
{
var image = SyntheticImages.Noise(Size, 0.8, seed: 4);
var shifted = SyntheticImages.Shift(image, Size, 20, 0);
var a = SyntheticImages.Crop(image, Size, 64, 64, Patch, Patch);
var b = SyntheticImages.Crop(shifted, Size, 64, 64, Patch, Patch);
var free = new PhaseCorrelator(Patch, Patch).Correlate(a, b);
Assert.Within(20, 0, free.Dx, free.Dy, 0.10);
var limited = new PhaseCorrelator(Patch, Patch, new PhaseCorrelatorOptions { MaxShift = 8 }).Correlate(a, b);
Assert.LessOrEqual(double.Abs(limited.Dx), 8.5);
}
}