using Titano.Core.Imaging; using Titano.Core.Motion; namespace Titano.Core.Tests.Motion; /// Deterministic synthetic textures (periodic, band-limited) and exact sub-pixel shifts by Fourier phase ramp. internal static class SyntheticImages { /// Gaussian-filtered white noise: spectral envelope exp(−2π²σ²|f|²), mean 0.5, standard deviation 0.15. 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)); /// Natural-image-like texture: amplitude ∝ 1/f^1.4 with a soft high-frequency roll-off. 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))); /// Periodic shift with sub-pixel accuracy: result(x, y) = image(x − dx, y − dy). 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; } /// Copies a rectangle of a square periodic image into a float plane. 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; } /// Single-channel buffer from a plane. 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; } /// RGB buffer whose channels are scaled copies of a plane (distinct but correlated colours). 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; } /// Peak signal-to-noise ratio in dB for unit peak over the window [x0, x1)×[y0, y1). public static double Psnr(ReadOnlySpan a, ReadOnlySpan 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 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; }