diff --git a/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs b/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs
index 78c9810f..34268978 100644
--- a/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs
+++ b/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs
@@ -33,7 +33,7 @@ public sealed record DeepFrameColumns
/// Correlated colour temperature in kelvin, null when not near-Planckian.
public double?[] Kelvin { get; init; } = [];
- /// Laplacian variance and noise σ of sRGB-encoded luminance, two significant digits.
+ /// Laplacian variance and robust noise σ of sRGB-encoded luminance, two significant digits.
public double?[] Sharpness { get; init; } = [];
public double?[] Noise { get; init; } = [];
diff --git a/src/Titano.Core/Analysis/Deep/DeepAnalyzer.cs b/src/Titano.Core/Analysis/Deep/DeepAnalyzer.cs
index e0e55593..052fce12 100644
--- a/src/Titano.Core/Analysis/Deep/DeepAnalyzer.cs
+++ b/src/Titano.Core/Analysis/Deep/DeepAnalyzer.cs
@@ -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);
}
- /// 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.
+ ///
+ /// 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 ≤
+ /// flow pixels) count, so flat noisy areas do not either; NaN when under
+ /// of the vectors are reliable. Magnitudes in source pixels per frame.
+ ///
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);
+ }
}
}
}
diff --git a/src/Titano.Core/Analysis/Deep/DeepFrame.cs b/src/Titano.Core/Analysis/Deep/DeepFrame.cs
index f2884ef4..3e6e131c 100644
--- a/src/Titano.Core/Analysis/Deep/DeepFrame.cs
+++ b/src/Titano.Core/Analysis/Deep/DeepFrame.cs
@@ -50,7 +50,10 @@ public sealed record DeepFrame
/// Correlated colour temperature of the mean colour; NaN when not near-Planckian.
public double Kelvin { get; init; } = double.NaN;
+ /// Laplacian variance of encoded luminance ().
public double Sharpness { get; init; } = double.NaN;
+
+ /// Robust noise σ of encoded luminance (), usable frames only.
public double Noise { get; init; } = double.NaN;
/// Content motion from the previous decoded frame at the image centre (nine-patch similarity fit).
diff --git a/src/Titano.Core/Analysis/Deep/FrameProbe.cs b/src/Titano.Core/Analysis/Deep/FrameProbe.cs
index b74fb9f9..04ae9643 100644
--- a/src/Titano.Core/Analysis/Deep/FrameProbe.cs
+++ b/src/Titano.Core/Analysis/Deep/FrameProbe.cs
@@ -32,6 +32,9 @@ internal sealed class FrameProbe : IDisposable
/// Source pixels per flow-image pixel.
public double FlowScale { get; private set; } = 1;
+ /// Robust encoded-luminance noise σ (); usable frames only.
+ 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)
{
diff --git a/src/Titano.Core/Analysis/Deep/StarFieldDetector.cs b/src/Titano.Core/Analysis/Deep/StarFieldDetector.cs
index 00cbe6f8..15bdf8ec 100644
--- a/src/Titano.Core/Analysis/Deep/StarFieldDetector.cs
+++ b/src/Titano.Core/Analysis/Deep/StarFieldDetector.cs
@@ -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;
- /// Point sources per megapixel of ; is the encoded-luminance
- /// noise σ (e.g. ).
+ /// Point sources per megapixel of a linear-light image; is the encoded-luminance noise σ.
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);
+ }
+ /// Point sources per megapixel of a row-major sRGB-encoded luminance plane.
+ public static double Density(ReadOnlySpan 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);
}
+
+ ///
+ /// 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.
+ ///
+ public static double RobustNoise(ReadOnlySpan 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;
+ }
+
+ /// sRGB-encoded Rec.709 luminance of an image.
+ public static float[] EncodedLuminance(ImageBuffer image)
+ {
+ ArgumentNullException.ThrowIfNull(image);
+ var e = new float[image.PlaneLength];
+ PlaneOps.PerceptualLuminance(image, e);
+ return e;
+ }
}