fix(analysis): measure noise robustly and count only consistent flow vectors

Median-based Immerkaer noise ignores stars and edges, so star detection and
the noise column no longer read star fields as noise; motion statistics keep
only vectors that pass the forward-backward check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:52:08 +00:00
1 parent 34b7c2736d
commit 24579d7293
5 files changed
+79 -16

No files matched your search

@@ -33,7 +33,7 @@ public sealed record DeepFrameColumns
/// <summary>Correlated colour temperature in kelvin, null when not near-Planckian.</summary>
public double?[] Kelvin { get; init; } = [];
/// <summary>Laplacian variance and noise σ of sRGB-encoded luminance, two significant digits.</summary>
/// <summary>Laplacian variance and robust noise σ of sRGB-encoded luminance, two significant digits.</summary>
public double?[] Sharpness { get; init; } = [];
public double?[] Noise { get; init; } = [];
+28 -6
View File
@@ -102,6 +102,8 @@ public static class DeepAnalyzer
private sealed class SequentialState(DeepAnalysisOptions options) : IDisposable
{
private const int MinimumStabilizerSize = 16;
private const float ReliableError = 0.5f;
private const double MinimumReliable = 0.05;
private readonly OpticalFlowEngine _flow = new(FlowQuality.Draft);
private Stabilizer? _stabilizer;
private FrameStatistics? _previous;
@@ -121,6 +123,7 @@ public static class DeepAnalyzer
frame = frame with
{
Flags = frame.Flags | (duplicate ? DeepFrameFlags.Duplicate : DeepFrameFlags.None),
Noise = probe.Noise,
ShiftX = shiftX,
ShiftY = shiftY,
MotionMedian = motionMedian,
@@ -165,7 +168,6 @@ public static class DeepAnalyzer
BlueRatio = stats.MeanBlue > 0 && green > 0 ? double.Log2(stats.MeanBlue / green) : double.NaN,
Kelvin = stats.ColorTemperature?.Kelvin ?? double.NaN,
Sharpness = stats.Sharpness,
Noise = stats.Noise,
};
}
@@ -189,8 +191,12 @@ public static class DeepAnalyzer
return ((x - cx) * probe.CorrelationScale, (y - cy) * probe.CorrelationScale);
}
/// <summary>Draft flow from the previous frame with the current frame brought to the previous mean level, so
/// flicker and exposure steps do not read as motion; magnitudes in source pixels per frame.</summary>
/// <summary>
/// Draft flow from the previous frame, with the current frame brought to the previous mean level so flicker and
/// exposure steps do not read as motion. Only vectors that pass the forward–backward check (error ≤
/// <see cref="ReliableError"/> flow pixels) count, so flat noisy areas do not either; NaN when under
/// <see cref="MinimumReliable"/> of the vectors are reliable. Magnitudes in source pixels per frame.
/// </summary>
private (double Median, double P95) Motion(FrameProbe probe, FrameStatistics stats, CancellationToken cancellationToken)
{
if (probe.Index % options.FlowEvery != 0 || _previousIndex != probe.Index - 1) return (double.NaN, double.NaN);
@@ -202,9 +208,25 @@ public static class DeepAnalyzer
{
foreach (ref var v in levelled.Data) v *= gain;
}
using var field = _flow.Compute(previous, levelled, cancellationToken);
var statistics = field.Statistics();
return (statistics.MedianMagnitude * probe.FlowScale, statistics.P95Magnitude * probe.FlowScale);
var (forward, backward) = _flow.ComputeBidirectional(previous, levelled, cancellationToken);
using (forward)
using (backward)
{
var u = forward.U;
var w = forward.V;
var error = forward.Error;
var magnitudes = new float[forward.Count];
var count = 0;
for (var i = 0; i < magnitudes.Length; i++)
{
if (error[i] <= ReliableError) magnitudes[count++] = float.Hypot(u[i], w[i]);
}
if (count < MinimumReliable * magnitudes.Length) return (double.NaN, double.NaN);
var reliable = magnitudes.AsSpan(0, count);
var median = PlaneOps.Median(reliable);
var p95 = PlaneOps.Select(reliable, int.Max(0, (int)double.Ceiling(0.95 * count) - 1));
return (median * probe.FlowScale, p95 * probe.FlowScale);
}
}
}
}
@@ -50,7 +50,10 @@ public sealed record DeepFrame
/// <summary>Correlated colour temperature of the mean colour; NaN when not near-Planckian.</summary>
public double Kelvin { get; init; } = double.NaN;
/// <summary>Laplacian variance of encoded luminance (<see cref="FrameStatistics.Sharpness"/>).</summary>
public double Sharpness { get; init; } = double.NaN;
/// <summary>Robust noise σ of encoded luminance (<see cref="StarFieldDetector.RobustNoise"/>), usable frames only.</summary>
public double Noise { get; init; } = double.NaN;
/// <summary>Content motion from the previous decoded frame at the image centre (nine-patch similarity fit).</summary>
+6 -1
View File
@@ -32,6 +32,9 @@ internal sealed class FrameProbe : IDisposable
/// <summary>Source pixels per flow-image pixel.</summary>
public double FlowScale { get; private set; } = 1;
/// <summary>Robust encoded-luminance noise σ (<see cref="StarFieldDetector.RobustNoise"/>); usable frames only.</summary>
public double Noise { get; private set; } = double.NaN;
public double StarDensity { get; private set; } = double.NaN;
public double CloudCoverage { get; private set; } = double.NaN;
public double SkyLogLuminance { get; private set; } = double.NaN;
@@ -83,7 +86,9 @@ internal sealed class FrameProbe : IDisposable
Statistics = stats;
if (!IsUsable) return;
if (stats.MeanLogLuminance <= options.DarkLogLuminance) StarDensity = StarFieldDetector.Density(analysis, stats.Noise);
var encoded = StarFieldDetector.EncodedLuminance(analysis);
Noise = StarFieldDetector.RobustNoise(encoded, analysis.Width, analysis.Height);
if (stats.MeanLogLuminance <= options.DarkLogLuminance) StarDensity = StarFieldDetector.Density(encoded, analysis.Width, analysis.Height, Noise);
if (mask is not null)
{
@@ -1,4 +1,5 @@
using Titano.Core.Imaging;
using Titano.Core.Motion;
namespace Titano.Core.Analysis.Deep;
@@ -14,18 +15,19 @@ public static class StarFieldDetector
public const double NoiseFactor = 6;
public const double MinimumContrast = 0.05;
/// <summary>Point sources per megapixel of <paramref name="image"/>; <paramref name="noiseSigma"/> is the encoded-luminance
/// noise σ (e.g. <see cref="FrameStatistics.Noise"/>).</summary>
/// <summary>Point sources per megapixel of a linear-light image; <paramref name="noiseSigma"/> is the encoded-luminance noise σ.</summary>
public static double Density(ImageBuffer image, double noiseSigma)
{
ArgumentNullException.ThrowIfNull(image);
int w = image.Width, h = image.Height;
if (w < 5 || h < 5) return 0;
var e = new float[image.PlaneLength];
ColorSpace.Luminance(image, e);
for (var i = 0; i < e.Length; i++) e[i] = ColorSpace.EncodeFast(e[i]);
var threshold = double.Max(NoiseFactor * (double.IsFinite(noiseSigma) ? noiseSigma : 0), MinimumContrast);
return Density(EncodedLuminance(image), image.Width, image.Height, noiseSigma);
}
/// <summary>Point sources per megapixel of a row-major sRGB-encoded luminance plane.</summary>
public static double Density(ReadOnlySpan<float> e, int w, int h, double noiseSigma)
{
if (e.Length < w * h) throw new ArgumentException("The plane is smaller than width × height.", nameof(e));
if (w < 5 || h < 5) return 0;
var threshold = double.Max(NoiseFactor * (double.IsFinite(noiseSigma) ? noiseSigma : 0), MinimumContrast);
var count = 0;
for (var y = 2; y < h - 2; y++)
{
@@ -54,4 +56,35 @@ public static class StarFieldDetector
}
return count * 1e6 / ((double)w * h);
}
/// <summary>
/// Noise σ of an encoded-luminance plane from the median |Immerkær (1996) response| on every second pixel of every
/// second row: σ = 1.4826 · median / 6 (the kernel's L2 norm). Unlike the mean-based estimate, stars, edges and
/// texture covering under half the image do not raise it.
/// </summary>
public static double RobustNoise(ReadOnlySpan<float> e, int w, int h)
{
if (w < 3 || h < 3) return 0;
var responses = new float[((w - 1) / 2) * ((h - 1) / 2)];
var n = 0;
for (var y = 1; y < h - 1; y += 2)
{
for (var x = 1; x < w - 1; x += 2)
{
var i = y * w + x;
responses[n++] = float.Abs(e[i - w - 1] + e[i - w + 1] + e[i + w - 1] + e[i + w + 1]
- 2 * (e[i - w] + e[i + w] + e[i - 1] + e[i + 1]) + 4 * e[i]);
}
}
return n > 0 ? 1.482602218505602 * PlaneOps.Median(responses.AsSpan(0, n)) / 6 : 0;
}
/// <summary>sRGB-encoded Rec.709 luminance of an image.</summary>
public static float[] EncodedLuminance(ImageBuffer image)
{
ArgumentNullException.ThrowIfNull(image);
var e = new float[image.PlaneLength];
PlaneOps.PerceptualLuminance(image, e);
return e;
}
}