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

+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);
}
}
}
}