diff --git a/src/Titano.Core/Analysis/RegionDeflicker.cs b/src/Titano.Core/Analysis/RegionDeflicker.cs
new file mode 100644
index 00000000..4f8e0aa7
--- /dev/null
+++ b/src/Titano.Core/Analysis/RegionDeflicker.cs
@@ -0,0 +1,43 @@
+using Titano.Core.Imaging;
+
+namespace Titano.Core.Analysis;
+
+/// Independent deflicker curves for the sky and land regions.
+public sealed record RegionDeflickerResult(DeflickerResult Sky, DeflickerResult Land);
+
+///
+/// Region deflicker (BRIEF §11.2): sky and land are measured through the soft , each gets its
+/// own target curve, and the two corrections are blended per pixel through the mask.
+/// Use it when the regions flicker independently (moving clouds, city lights) and the mask confidence is high.
+///
+public static class RegionDeflicker
+{
+ /// Trimmed-mean log2 luminance of the sky and land regions of one analysis image.
+ public static (double Sky, double Land) Measure(ImageBuffer analysisImage, RegionMask mask, double trimLow = 0.02, double trimHigh = 0.02)
+ {
+ ArgumentNullException.ThrowIfNull(analysisImage);
+ ArgumentNullException.ThrowIfNull(mask);
+ if (mask.Width != analysisImage.Width || mask.Height != analysisImage.Height)
+ throw new ArgumentException("Mask and image sizes differ.", nameof(mask));
+ return (FrameStatistics.MeasureLogLuminance(analysisImage, mask.Sky, trimLow, trimHigh),
+ FrameStatistics.MeasureLogLuminance(analysisImage, mask.Land, trimLow, trimHigh));
+ }
+
+ public static RegionDeflickerResult Compute(ReadOnlySpan skyLogLuminance, ReadOnlySpan landLogLuminance, DeflickerOptions? options = null, ReadOnlySpan intentionalOffsets = default)
+ {
+ if (skyLogLuminance.Length != landLogLuminance.Length) throw new ArgumentException("Series lengths differ.", nameof(landLogLuminance));
+ return new RegionDeflickerResult(
+ DeflickerEngine.Compute(skyLogLuminance, options, intentionalOffsets),
+ DeflickerEngine.Compute(landLogLuminance, options, intentionalOffsets));
+ }
+
+ /// Applies one frame's sky and land corrections (EV) in place; the mask is resampled to the image size
+ /// when needed (resample once up front when rendering many frames).
+ public static void Apply(ImageBuffer image, RegionMask mask, double skyEv, double landEv, ChannelGains? gains = null, double? softClipKnee = null)
+ {
+ ArgumentNullException.ThrowIfNull(image);
+ ArgumentNullException.ThrowIfNull(mask);
+ var fitted = mask.Resample(image.Width, image.Height);
+ ExposureProcessor.ApplyRegion(image, fitted.Sky, skyEv, landEv, gains, softClipKnee);
+ }
+}
diff --git a/src/Titano.Core/Analysis/RegionMask.cs b/src/Titano.Core/Analysis/RegionMask.cs
new file mode 100644
index 00000000..c7c1a22c
--- /dev/null
+++ b/src/Titano.Core/Analysis/RegionMask.cs
@@ -0,0 +1,98 @@
+namespace Titano.Core.Analysis;
+
+public enum RegionMethod
+{
+ /// Sky above a per-column horizon line.
+ Horizon,
+
+ /// Sky = the brighter class of a luminance threshold.
+ Luminance,
+}
+
+/// Soft sky/land segmentation: per-pixel sky weight in [0, 1] with a feathered transition.
+public sealed class RegionMask
+{
+ private float[]? _land;
+
+ public RegionMask(int width, int height, float[] sky, double[]? horizon, RegionMethod method, double confidence)
+ {
+ ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width);
+ ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height);
+ ArgumentNullException.ThrowIfNull(sky);
+ if (sky.Length != width * height) throw new ArgumentException("One weight per pixel required.", nameof(sky));
+ if (horizon is not null && horizon.Length != width) throw new ArgumentException("One horizon row per column required.", nameof(horizon));
+ Width = width;
+ Height = height;
+ Sky = sky;
+ Horizon = horizon;
+ Method = method;
+ Confidence = confidence;
+ }
+
+ public int Width { get; }
+ public int Height { get; }
+
+ /// Sky weight per pixel, row-major (1 = sky, 0 = land).
+ public float[] Sky { get; }
+
+ /// Land weight per pixel (1 − sky).
+ public float[] Land
+ {
+ get
+ {
+ if (_land is not null) return _land;
+ var land = new float[Sky.Length];
+ for (var i = 0; i < land.Length; i++) land[i] = 1 - Sky[i];
+ return _land = land;
+ }
+ }
+
+ /// Horizon position per column in rows from the top edge (horizon method only).
+ public double[]? Horizon { get; }
+
+ public RegionMethod Method { get; }
+
+ /// 0 … 1; region deflicker is advisable only when this is high.
+ public double Confidence { get; }
+
+ /// Bilinear resampling to another resolution (e.g. analysis size → render size), pixel-centre aligned.
+ public RegionMask Resample(int width, int height)
+ {
+ ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width);
+ ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height);
+ if (width == Width && height == Height) return this;
+ var sky = new float[width * height];
+ var sx = (double)Width / width;
+ var sy = (double)Height / height;
+ Parallel.For(0, height, y =>
+ {
+ var fy = Math.Clamp((y + 0.5) * sy - 0.5, 0, Height - 1);
+ var y0 = (int)fy;
+ var y1 = Math.Min(y0 + 1, Height - 1);
+ var ty = (float)(fy - y0);
+ for (var x = 0; x < width; x++)
+ {
+ var fx = Math.Clamp((x + 0.5) * sx - 0.5, 0, Width - 1);
+ var x0 = (int)fx;
+ var x1 = Math.Min(x0 + 1, Width - 1);
+ var tx = (float)(fx - x0);
+ var top = Sky[y0 * Width + x0] + (Sky[y0 * Width + x1] - Sky[y0 * Width + x0]) * tx;
+ var bottom = Sky[y1 * Width + x0] + (Sky[y1 * Width + x1] - Sky[y1 * Width + x0]) * tx;
+ sky[y * width + x] = top + (bottom - top) * ty;
+ }
+ });
+ double[]? horizon = null;
+ if (Horizon is not null)
+ {
+ horizon = new double[width];
+ for (var x = 0; x < width; x++)
+ {
+ var fx = Math.Clamp((x + 0.5) * sx - 0.5, 0, Width - 1);
+ var x0 = (int)fx;
+ var x1 = Math.Min(x0 + 1, Width - 1);
+ horizon[x] = (Horizon[x0] + (Horizon[x1] - Horizon[x0]) * (fx - x0)) / sy;
+ }
+ }
+ return new RegionMask(width, height, sky, horizon, Method, Confidence);
+ }
+}
diff --git a/src/Titano.Core/Analysis/RegionSegmenter.cs b/src/Titano.Core/Analysis/RegionSegmenter.cs
new file mode 100644
index 00000000..043174e1
--- /dev/null
+++ b/src/Titano.Core/Analysis/RegionSegmenter.cs
@@ -0,0 +1,370 @@
+using Titano.Core.Imaging;
+using Titano.Core.Mathematics;
+
+namespace Titano.Core.Analysis;
+
+/// Parameters of .
+public sealed record RegionSegmenterOptions
+{
+ public static RegionSegmenterOptions Default { get; } = new();
+
+ /// Frames sampled evenly from the sequence.
+ public int MaxFrames { get; init; } = 15;
+
+ /// Below this the luminance fallback is tried and the more confident mask wins.
+ public double MinimumHorizonConfidence { get; init; } = 0.4;
+
+ /// Row-step amplitude in EV that counts as a fully convincing horizon.
+ public double StrongStepEv { get; init; } = 0.5;
+
+ /// Width of the feathered transition as a fraction of the image height.
+ public double FeatherFraction { get; init; } = 0.03;
+
+ /// Path penalty per row of vertical movement between adjacent columns, relative to the horizon step.
+ public double SmoothnessPenalty { get; init; } = 0.05;
+}
+
+///
+/// Sky/land segmentation for region deflicker (BRIEF §10.2, §11.2). Horizon: the strongest step of the
+/// column-averaged log-luminance profile (median over frames, so robust to day/night polarity changes), refined per
+/// column by a dynamic-programming path that maximises the local step with a smoothness penalty, then feathered.
+/// Fallback: Otsu threshold of the temporal-median log luminance (brighter class = sky) cleaned by morphological
+/// opening and closing. Each method reports a confidence in [0, 1].
+///
+public static class RegionSegmenter
+{
+ private const float LogFloor = -14;
+
+ public static RegionMask Segment(IReadOnlyList frames, RegionSegmenterOptions? options = null)
+ {
+ ArgumentNullException.ThrowIfNull(frames);
+ options ??= RegionSegmenterOptions.Default;
+ if (frames.Count == 0) throw new ArgumentException("At least one frame is required.", nameof(frames));
+ var width = frames[0].Width;
+ var height = frames[0].Height;
+ foreach (var f in frames)
+ {
+ if (f.Width != width || f.Height != height) throw new ArgumentException("Frames must share one size.", nameof(frames));
+ }
+ if (height < 8) throw new ArgumentException("Frames must be at least 8 rows tall.", nameof(frames));
+
+ var count = Math.Min(Math.Max(1, options.MaxFrames), frames.Count);
+ var logs = new float[count][];
+ Parallel.For(0, count, i => logs[i] = LogLuminance(frames[(int)((long)i * (frames.Count - 1) / Math.Max(1, count - 1))]));
+ var feather = Math.Max(1.5, options.FeatherFraction * height);
+
+ var (horizon, horizonConfidence) = DetectHorizon(logs, width, height, options);
+ if (horizon is not null && horizonConfidence >= options.MinimumHorizonConfidence)
+ return new RegionMask(width, height, HorizonWeights(horizon, width, height, feather), horizon, RegionMethod.Horizon, horizonConfidence);
+
+ var (sky, luminanceConfidence) = LuminanceWeights(logs, width, height, feather);
+ if (horizon is not null && horizonConfidence >= luminanceConfidence)
+ return new RegionMask(width, height, HorizonWeights(horizon, width, height, feather), horizon, RegionMethod.Horizon, horizonConfidence);
+ return new RegionMask(width, height, sky, null, RegionMethod.Luminance, luminanceConfidence);
+ }
+
+ private static float[] LogLuminance(ImageBuffer image)
+ {
+ var y = new float[image.PlaneLength];
+ ColorSpace.Luminance(image, y);
+ for (var i = 0; i < y.Length; i++) y[i] = y[i] > 6.1035156e-05f ? MathF.Log2(y[i]) : LogFloor;
+ return y;
+ }
+
+ private static (double[]? Horizon, double Confidence) DetectHorizon(float[][] logs, int width, int height, RegionSegmenterOptions options)
+ {
+ var frames = logs.Length;
+ var k = Math.Max(2, height / 24);
+ var steps = new float[frames][];
+ var profiles = new double[frames][];
+ Parallel.For(0, frames, f => (steps[f], profiles[f]) = ColumnSteps(logs[f], width, height, k));
+
+ var score = new double[height];
+ var column = new double[frames];
+ for (var y = k; y <= height - k; y++)
+ {
+ for (var f = 0; f < frames; f++) column[f] = Math.Abs(profiles[f][y]);
+ score[y] = Statistics.MedianInPlace(column);
+ }
+ var y0 = k;
+ for (var y = k; y <= height - k; y++)
+ {
+ if (score[y] > score[y0]) y0 = y;
+ }
+ var peak = score[y0];
+ if (!(peak > 1e-6)) return (null, 0);
+
+ double second = 0;
+ for (var y = k; y <= height - k; y++)
+ {
+ if (Math.Abs(y - y0) > 2 * k) second = Math.Max(second, score[y]);
+ }
+ var consistent = 0;
+ var signs = new int[frames];
+ for (var f = 0; f < frames; f++)
+ {
+ var best = k;
+ for (var y = k; y <= height - k; y++)
+ {
+ if (Math.Abs(profiles[f][y]) > Math.Abs(profiles[f][best])) best = y;
+ }
+ if (Math.Abs(best - y0) <= k) consistent++;
+ signs[f] = profiles[f][y0] < 0 ? -1 : 1;
+ }
+ var confidence = Math.Min(1, peak / options.StrongStepEv) * (1 - second / peak) * consistent / frames;
+
+ // Per-column refinement: minimum-cost path through −(signed local step), median over frames.
+ var band = Math.Max(2 * k, height / 4);
+ var yLo = Math.Max(k, y0 - band);
+ var yHi = Math.Min(height - k, y0 + band);
+ var rows = yHi - yLo + 1;
+ var cost = new double[width * rows];
+ Parallel.For(0, width, x =>
+ {
+ var values = new double[frames];
+ for (var j = 0; j < rows; j++)
+ {
+ var i = (yLo + j) * width + x;
+ for (var f = 0; f < frames; f++) values[f] = signs[f] * steps[f][i];
+ cost[x * rows + j] = -Statistics.MedianInPlace(values);
+ }
+ });
+ var path = MinimumPath(cost, width, rows, Math.Max(2, height / 48), options.SmoothnessPenalty * peak);
+
+ var horizon = new double[width];
+ for (var x = 0; x < width; x++)
+ {
+ var j = path[x];
+ var offset = 0.0;
+ if (j > 0 && j < rows - 1)
+ {
+ double a = cost[x * rows + j - 1], c = cost[x * rows + j], b = cost[x * rows + j + 1];
+ var curvature = a - 2 * c + b;
+ if (curvature > 0) offset = Math.Clamp(0.5 * (a - b) / curvature, -0.5, 0.5);
+ }
+ horizon[x] = yLo + j + offset;
+ }
+ return (horizon, Math.Clamp(confidence, 0, 1));
+ }
+
+ ///
+ /// Vertical box-difference step (mean of k rows above minus k rows below each row boundary) on a horizontally
+ /// box-smoothed log image, plus its column average.
+ ///
+ private static (float[] Steps, double[] Profile) ColumnSteps(float[] log, int width, int height, int k)
+ {
+ var radius = Math.Max(1, width / 64);
+ var smooth = new float[log.Length];
+ var prefix = new double[width + 1];
+ for (var y = 0; y < height; y++)
+ {
+ var row = y * width;
+ for (var x = 0; x < width; x++) prefix[x + 1] = prefix[x] + log[row + x];
+ for (var x = 0; x < width; x++)
+ {
+ var a = Math.Max(0, x - radius);
+ var b = Math.Min(width, x + radius + 1);
+ smooth[row + x] = (float)((prefix[b] - prefix[a]) / (b - a));
+ }
+ }
+
+ var steps = new float[log.Length];
+ var profile = new double[height];
+ var columnPrefix = new double[height + 1];
+ for (var x = 0; x < width; x++)
+ {
+ for (var y = 0; y < height; y++) columnPrefix[y + 1] = columnPrefix[y] + smooth[y * width + x];
+ for (var y = k; y <= height - k; y++)
+ {
+ var above = columnPrefix[y] - columnPrefix[y - k];
+ var below = columnPrefix[y + k] - columnPrefix[y];
+ var step = (above - below) / k;
+ steps[y * width + x] = (float)step;
+ profile[y] += step;
+ }
+ }
+ for (var y = 0; y < height; y++) profile[y] /= width;
+ return (steps, profile);
+ }
+
+ /// Dynamic programming over columns: each step may move at most rows at a
+ /// cost of per row. Returns the row index (within the band) per column.
+ private static int[] MinimumPath(double[] cost, int width, int rows, int slope, double penalty)
+ {
+ var energy = new double[width * rows];
+ var from = new int[width * rows];
+ Array.Copy(cost, energy, rows);
+ for (var x = 1; x < width; x++)
+ {
+ for (var j = 0; j < rows; j++)
+ {
+ var best = double.PositiveInfinity;
+ var arg = j;
+ for (var d = -slope; d <= slope; d++)
+ {
+ var p = j + d;
+ if (p < 0 || p >= rows) continue;
+ var e = energy[(x - 1) * rows + p] + penalty * Math.Abs(d);
+ if (e < best)
+ {
+ best = e;
+ arg = p;
+ }
+ }
+ energy[x * rows + j] = cost[x * rows + j] + best;
+ from[x * rows + j] = arg;
+ }
+ }
+ var path = new int[width];
+ var end = 0;
+ for (var j = 1; j < rows; j++)
+ {
+ if (energy[(width - 1) * rows + j] < energy[(width - 1) * rows + end]) end = j;
+ }
+ path[width - 1] = end;
+ for (var x = width - 1; x > 0; x--) path[x - 1] = from[x * rows + path[x]];
+ return path;
+ }
+
+ private static float[] HorizonWeights(double[] horizon, int width, int height, double feather)
+ {
+ var sky = new float[width * height];
+ for (var y = 0; y < height; y++)
+ {
+ for (var x = 0; x < width; x++)
+ {
+ var t = Math.Clamp(0.5 + (horizon[x] - (y + 0.5)) / feather, 0, 1);
+ sky[y * width + x] = (float)(t * t * (3 - 2 * t));
+ }
+ }
+ return sky;
+ }
+
+ private static (float[] Sky, double Confidence) LuminanceWeights(float[][] logs, int width, int height, double feather)
+ {
+ var n = width * height;
+ var median = new double[n];
+ var values = new double[logs.Length];
+ for (var i = 0; i < n; i++)
+ {
+ for (var f = 0; f < logs.Length; f++) values[f] = logs[f][i];
+ median[i] = Statistics.MedianInPlace(values);
+ }
+
+ double min = double.PositiveInfinity, max = double.NegativeInfinity;
+ foreach (var v in median)
+ {
+ min = Math.Min(min, v);
+ max = Math.Max(max, v);
+ }
+ if (!(max - min > 1e-6)) return (new float[n], 0);
+
+ var (threshold, separability) = Otsu(median, min, max);
+ var binary = new byte[n];
+ for (var i = 0; i < n; i++) binary[i] = median[i] > threshold ? (byte)1 : (byte)0;
+ var radius = Math.Max(1, Math.Min(width, height) / 64);
+ binary = MinMax(MinMax(binary, width, height, radius, dilate: false), width, height, radius, dilate: true);
+ binary = MinMax(MinMax(binary, width, height, radius, dilate: true), width, height, radius, dilate: false);
+
+ var sky = new float[n];
+ for (var i = 0; i < n; i++) sky[i] = binary[i];
+ BoxBlur(sky, width, height, Math.Max(1, (int)Math.Round(feather / 2)));
+ // Otsu's η is 2/π for a single Gaussian mode: rescale so a unimodal image scores 0 and two spikes score 1.
+ var confidence = Math.Clamp((separability - 2 / Math.PI) / (1 - 2 / Math.PI), 0, 1);
+ return (sky, confidence);
+ }
+
+ /// Otsu (1979) threshold over a 256-bin histogram and its separability η = σ²between / σ²total.
+ private static (double Threshold, double Separability) Otsu(double[] values, double min, double max)
+ {
+ const int bins = 256;
+ var histogram = new double[bins];
+ Statistics.AccumulateHistogram(values, min, max + (max - min) * 1e-9, histogram);
+ var width = (max - min) / bins;
+ double total = values.Length, sumAll = 0, sumSquares = 0;
+ for (var i = 0; i < bins; i++)
+ {
+ var centre = min + (i + 0.5) * width;
+ sumAll += histogram[i] * centre;
+ sumSquares += histogram[i] * centre * centre;
+ }
+ var mean = sumAll / total;
+ var variance = sumSquares / total - mean * mean;
+ double w0 = 0, sum0 = 0, bestBetween = -1;
+ var bestBin = 0;
+ for (var i = 0; i < bins - 1; i++)
+ {
+ w0 += histogram[i];
+ sum0 += histogram[i] * (min + (i + 0.5) * width);
+ var w1 = total - w0;
+ if (w0 <= 0 || w1 <= 0) continue;
+ var m0 = sum0 / w0;
+ var m1 = (sumAll - sum0) / w1;
+ var between = w0 * w1 * (m0 - m1) * (m0 - m1) / (total * total);
+ if (between > bestBetween)
+ {
+ bestBetween = between;
+ bestBin = i;
+ }
+ }
+ return (min + (bestBin + 1) * width, variance > 0 ? Math.Max(0, bestBetween) / variance : 0);
+ }
+
+ /// Binary erosion (min) or dilation (max) with a (2r+1)² square, as two separable passes.
+ private static byte[] MinMax(byte[] source, int width, int height, int radius, bool dilate)
+ {
+ var horizontal = new byte[source.Length];
+ for (var y = 0; y < height; y++)
+ {
+ for (var x = 0; x < width; x++)
+ {
+ var v = dilate ? (byte)0 : (byte)1;
+ for (var d = -radius; d <= radius; d++)
+ {
+ var s = source[y * width + Math.Clamp(x + d, 0, width - 1)];
+ v = dilate ? Math.Max(v, s) : Math.Min(v, s);
+ }
+ horizontal[y * width + x] = v;
+ }
+ }
+ var result = new byte[source.Length];
+ for (var y = 0; y < height; y++)
+ {
+ for (var x = 0; x < width; x++)
+ {
+ var v = dilate ? (byte)0 : (byte)1;
+ for (var d = -radius; d <= radius; d++)
+ {
+ var s = horizontal[Math.Clamp(y + d, 0, height - 1) * width + x];
+ v = dilate ? Math.Max(v, s) : Math.Min(v, s);
+ }
+ result[y * width + x] = v;
+ }
+ }
+ return result;
+ }
+
+ /// Separable box blur with edge-shrunk windows, in place.
+ private static void BoxBlur(float[] data, int width, int height, int radius)
+ {
+ var line = new double[Math.Max(width, height) + 1];
+ for (var y = 0; y < height; y++)
+ {
+ for (var x = 0; x < width; x++) line[x + 1] = line[x] + data[y * width + x];
+ for (var x = 0; x < width; x++)
+ {
+ int a = Math.Max(0, x - radius), b = Math.Min(width, x + radius + 1);
+ data[y * width + x] = (float)((line[b] - line[a]) / (b - a));
+ }
+ }
+ for (var x = 0; x < width; x++)
+ {
+ for (var y = 0; y < height; y++) line[y + 1] = line[y] + data[y * width + x];
+ for (var y = 0; y < height; y++)
+ {
+ int a = Math.Max(0, y - radius), b = Math.Min(height, y + radius + 1);
+ data[y * width + x] = (float)((line[b] - line[a]) / (b - a));
+ }
+ }
+ }
+}
diff --git a/tests/Titano.Core.Tests/Analysis/RegionTests.cs b/tests/Titano.Core.Tests/Analysis/RegionTests.cs
new file mode 100644
index 00000000..ad06ce20
--- /dev/null
+++ b/tests/Titano.Core.Tests/Analysis/RegionTests.cs
@@ -0,0 +1,201 @@
+using System.Globalization;
+using Titano.Core.Analysis;
+using Titano.Core.Diagnostics.Synthetic;
+using Titano.Core.Imaging;
+using Titano.Testing;
+
+namespace Titano.Core.Tests.Analysis;
+
+public sealed class RegionTests(TestContext context)
+{
+ private static SyntheticSequence Landscape(int frames, double tilt, double wave, int seed = 5) => SyntheticSequence.Create(new SyntheticSequenceOptions
+ {
+ Width = 128,
+ Height = 80,
+ FrameCount = frames,
+ Seed = seed,
+ StartEv = -1,
+ EndEv = 0,
+ HorizonFraction = 0.45,
+ HorizonTilt = tilt,
+ HorizonWave = wave,
+ SkyFlickerEv = 0.15,
+ LandFlickerEv = 0.15,
+ });
+
+ private static List RenderAll(SyntheticSequence sequence) => Enumerable.Range(0, sequence.Count).Select(sequence.Render).ToList();
+
+ [Test]
+ public void HorizonIsFoundPerColumnWithHighConfidence()
+ {
+ var sequence = Landscape(30, tilt: 0.08, wave: 3);
+ var frames = RenderAll(sequence);
+ try
+ {
+ var mask = RegionSegmenter.Segment(frames);
+ Assert.Equal(RegionMethod.Horizon, mask.Method);
+ Assert.Greater(mask.Confidence, 0.6);
+ var horizon = Assert.NotNull(mask.Horizon);
+ var worst = Enumerable.Range(0, mask.Width).Max(x => Math.Abs(horizon[x] - sequence.HorizonRow(x + 0.5)));
+ var agreement = Agreement(mask, sequence);
+ context.Log(string.Create(CultureInfo.InvariantCulture, $"confidence {mask.Confidence:F3}, worst column error {worst:F3} px, pixel agreement {agreement:P2}"));
+ Assert.LessOrEqual(worst, 1.0);
+ Assert.GreaterOrEqual(agreement, 0.98);
+ }
+ finally
+ {
+ frames.ForEach(f => f.Dispose());
+ }
+ }
+
+ [Test]
+ public void HorizonSurvivesPolarityChangesAcrossFrames()
+ {
+ var day = Landscape(10, tilt: 0, wave: 0);
+ var night = SyntheticSequence.Create(day.Options with { SkyLevel = 0.01, LandLevel = 0.2, Seed = 6 });
+ var frames = RenderAll(day).Concat(RenderAll(night)).ToList();
+ try
+ {
+ var mask = RegionSegmenter.Segment(frames);
+ Assert.Equal(RegionMethod.Horizon, mask.Method);
+ var horizon = Assert.NotNull(mask.Horizon);
+ Assert.True(horizon.All(h => Math.Abs(h - 0.45 * 80) <= 1), "horizon row");
+ }
+ finally
+ {
+ frames.ForEach(f => f.Dispose());
+ }
+ }
+
+ [Test]
+ public void CliffFallsBackToLuminance()
+ {
+ // Sky above row 20 on the left half and above row 60 on the right: no single row step explains it.
+ var frames = new List();
+ for (var f = 0; f < 6; f++)
+ {
+ var image = new ImageBuffer(96, 80, 3);
+ for (var y = 0; y < 80; y++)
+ {
+ for (var x = 0; x < 96; x++)
+ {
+ var sky = y < (x < 48 ? 20 : 60);
+ var v = sky ? 0.4f * (1 + 0.02f * f) : 0.03f * (1 + ((x * 7 + y * 13) % 5) * 0.1f);
+ for (var c = 0; c < 3; c++) image[c, x, y] = v;
+ }
+ }
+ frames.Add(image);
+ }
+ try
+ {
+ var mask = RegionSegmenter.Segment(frames);
+ Assert.Equal(RegionMethod.Luminance, mask.Method);
+ Assert.Greater(mask.Confidence, 0.5);
+ var correct = 0;
+ for (var y = 0; y < 80; y++)
+ {
+ for (var x = 0; x < 96; x++)
+ {
+ var sky = y < (x < 48 ? 20 : 60);
+ if (mask.Sky[y * 96 + x] >= 0.5f == sky) correct++;
+ }
+ }
+ Assert.GreaterOrEqual(correct / (96.0 * 80), 0.97);
+ }
+ finally
+ {
+ frames.ForEach(f => f.Dispose());
+ }
+ }
+
+ [Test]
+ public void FeaturelessFramesHaveNoConfidence()
+ {
+ using var flat = new ImageBuffer(64, 48, 3);
+ flat.Data.Fill(0.2f);
+ var mask = RegionSegmenter.Segment([flat]);
+ Assert.Less(mask.Confidence, 0.05);
+ }
+
+ /// BRIEF §20.4: with independent sky and land flicker, the land residual after region deflicker is at
+ /// most 25 % of the residual left by one global curve.
+ [Test]
+ public void RegionDeflickerMeetsTheRegressionGate()
+ {
+ var sequence = Landscape(150, tilt: 0.05, wave: 2);
+ var frames = RenderAll(sequence);
+ RegionMask mask;
+ double[] sky, land, global;
+ try
+ {
+ mask = RegionSegmenter.Segment(frames);
+ sky = new double[frames.Count];
+ land = new double[frames.Count];
+ global = new double[frames.Count];
+ for (var i = 0; i < frames.Count; i++)
+ {
+ (sky[i], land[i]) = RegionDeflicker.Measure(frames[i], mask);
+ global[i] = FrameStatistics.MeasureLogLuminance(frames[i]);
+ }
+ }
+ finally
+ {
+ frames.ForEach(f => f.Dispose());
+ }
+
+ var options = new DeflickerOptions { Window = 31 };
+ var globalResult = DeflickerEngine.Compute(global, options);
+ var regionResult = RegionDeflicker.Compute(sky, land, options);
+ var trueLand = AnalysisFixtures.TrueLandMask(sequence);
+ var landAfterGlobal = AnalysisFixtures.MeasureSeries(sequence, (i, image) =>
+ {
+ ExposureProcessor.Apply(image, globalResult.CorrectionEv[i]);
+ return image;
+ }, trueLand);
+ var landAfterRegion = AnalysisFixtures.MeasureSeries(sequence, (i, image) =>
+ {
+ RegionDeflicker.Apply(image, mask, regionResult.Sky.CorrectionEv[i], regionResult.Land.CorrectionEv[i]);
+ return image;
+ }, trueLand);
+
+ var globalResidual = AnalysisFixtures.FrameToFrameResidualRms(landAfterGlobal, sequence.SceneEv);
+ var regionResidual = AnalysisFixtures.FrameToFrameResidualRms(landAfterRegion, sequence.SceneEv);
+ var ratio = regionResidual / globalResidual;
+ context.Log(string.Create(CultureInfo.InvariantCulture,
+ $"mask {mask.Method} confidence {mask.Confidence:F3}; land residual global {globalResidual:F4} EV, region {regionResidual:F4} EV, ratio {ratio:P1}"));
+ Assert.LessOrEqual(ratio, 0.25);
+ }
+
+ [Test]
+ public void ResampledMaskKeepsTheHorizon()
+ {
+ var sequence = Landscape(6, tilt: 0, wave: 0);
+ var frames = RenderAll(sequence);
+ try
+ {
+ var mask = RegionSegmenter.Segment(frames);
+ var big = mask.Resample(256, 160);
+ Assert.Equal(256 * 160, big.Sky.Length);
+ Assert.Approximately(2 * mask.Horizon![10], big.Horizon![20], 0.6);
+ Assert.True(big.Sky[5 * 256 + 100] > 0.99f && big.Sky[150 * 256 + 100] < 0.01f);
+ Assert.Same(mask, mask.Resample(mask.Width, mask.Height));
+ }
+ finally
+ {
+ frames.ForEach(f => f.Dispose());
+ }
+ }
+
+ private static double Agreement(RegionMask mask, SyntheticSequence sequence)
+ {
+ var correct = 0;
+ for (var y = 0; y < mask.Height; y++)
+ {
+ for (var x = 0; x < mask.Width; x++)
+ {
+ if (mask.Sky[y * mask.Width + x] >= 0.5f == sequence.IsSky(x, y)) correct++;
+ }
+ }
+ return correct / (double)(mask.Width * mask.Height);
+ }
+}