Files
Chronos/tests/Titano.Core.Tests/Motion/SyntheticImages.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

123 lines
4.9 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.Motion;
namespace Titano.Core.Tests.Motion;
/// <summary>Deterministic synthetic textures (periodic, band-limited) and exact sub-pixel shifts by Fourier phase ramp.</summary>
internal static class SyntheticImages
{
/// <summary>Gaussian-filtered white noise: spectral envelope exp(−2π²σ²|f|²), mean 0.5, standard deviation 0.15.</summary>
public static double[] Noise(int size, double sigma, int seed) =>
Synthesize(size, seed, f => double.Exp(-2 * double.Pi * double.Pi * sigma * sigma * f * f));
/// <summary>Natural-image-like texture: amplitude ∝ 1/f^1.4 with a soft high-frequency roll-off.</summary>
public static double[] Natural(int size, int seed) =>
Synthesize(size, seed, f => f == 0 ? 0 : double.Pow(f * size, -1.4) * double.Exp(-f * f / (2 * 0.2 * 0.2)));
/// <summary>Periodic shift with sub-pixel accuracy: result(x, y) = image(x − dx, y − dy).</summary>
public static double[] Shift(double[] image, int size, double dx, double dy)
{
var re = (double[])image.Clone();
var im = new double[re.Length];
Fourier.Forward2D(re, im, size, size);
for (var v = 0; v < size; v++)
{
for (var u = 0; u < size; u++)
{
var (s, c) = double.SinCos(-2 * double.Pi * (Signed(u, size) * dx + Signed(v, size) * dy) / size);
var i = v * size + u;
(re[i], im[i]) = (re[i] * c - im[i] * s, re[i] * s + im[i] * c);
}
}
Fourier.Inverse2D(re, im, size, size);
return re;
}
/// <summary>Copies a rectangle of a square periodic image into a float plane.</summary>
public static float[] Crop(double[] image, int size, int x0, int y0, int width, int height)
{
var plane = new float[width * height];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++) plane[y * width + x] = (float)image[((y0 + y) % size) * size + (x0 + x) % size];
}
return plane;
}
/// <summary>Single-channel buffer from a plane.</summary>
public static ImageBuffer ToBuffer(float[] plane, int width, int height)
{
var buffer = new ImageBuffer(width, height, 1, clear: false);
plane.AsSpan().CopyTo(buffer.Plane(0));
return buffer;
}
/// <summary>RGB buffer whose channels are scaled copies of a plane (distinct but correlated colours).</summary>
public static ImageBuffer ToRgb(float[] plane, int width, int height)
{
var buffer = new ImageBuffer(width, height, 3, clear: false);
for (var c = 0; c < 3; c++)
{
var dst = buffer.Plane(c);
var gain = 0.6f + 0.2f * c;
for (var i = 0; i < plane.Length; i++) dst[i] = plane[i] * gain;
}
return buffer;
}
/// <summary>Peak signal-to-noise ratio in dB for unit peak over the window [x0, x1)×[y0, y1).</summary>
public static double Psnr(ReadOnlySpan<float> a, ReadOnlySpan<float> b, int width, int x0, int y0, int x1, int y1)
{
double sum = 0;
var n = 0;
for (var y = y0; y < y1; y++)
{
for (var x = x0; x < x1; x++)
{
var d = a[y * width + x] - (double)b[y * width + x];
sum += d * d;
n++;
}
}
var mse = sum / n;
return mse == 0 ? double.PositiveInfinity : 10 * double.Log10(1 / mse);
}
private static double[] Synthesize(int size, int seed, Func<double, double> envelope)
{
var random = new Random(seed);
var re = new double[size * size];
var im = new double[size * size];
for (var v = 0; v < size; v++)
{
for (var u = 0; u < size; u++)
{
// Nyquist bins stay empty so the phase-ramp shift is exact for a real image.
if (u == size / 2 || v == size / 2) continue;
double fx = Signed(u, size) / (double)size, fy = Signed(v, size) / (double)size;
var a = envelope(double.Sqrt(fx * fx + fy * fy));
re[v * size + u] = a * Gaussian(random);
im[v * size + u] = a * Gaussian(random);
}
}
re[0] = im[0] = 0;
Fourier.Inverse2D(re, im, size, size);
double mean = 0, sq = 0;
foreach (var x in re) mean += x;
mean /= re.Length;
foreach (var x in re) sq += (x - mean) * (x - mean);
var scale = 0.15 / double.Sqrt(sq / re.Length);
for (var i = 0; i < re.Length; i++) re[i] = 0.5 + (re[i] - mean) * scale;
return re;
}
private static double Gaussian(Random random)
{
var u1 = 1.0 - random.NextDouble();
var u2 = random.NextDouble();
return double.Sqrt(-2 * double.Log(u1)) * double.Cos(2 * double.Pi * u2);
}
private static int Signed(int index, int length) => index <= length / 2 ? index : index - length;
}