Files
Chronos/src/Titano.Core/Analysis/RegionDeflicker.cs
T
Claude 731ab2c48b feat(analysis): add sky/land segmentation and region deflicker
Horizon from the strongest column-averaged row step (median over
frames), refined per column by a dynamic-programming path and feathered;
Otsu luminance fallback with morphology; confidences for both. Region
deflicker fits sky and land separately and blends through the mask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:51:43 +00:00

44 lines
2.5 KiB
C#

using Titano.Core.Imaging;
namespace Titano.Core.Analysis;
/// <summary>Independent deflicker curves for the sky and land regions.</summary>
public sealed record RegionDeflickerResult(DeflickerResult Sky, DeflickerResult Land);
/// <summary>
/// Region deflicker (BRIEF §11.2): sky and land are measured through the soft <see cref="RegionMask"/>, each gets its
/// own <see cref="DeflickerEngine"/> 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.
/// </summary>
public static class RegionDeflicker
{
/// <summary>Trimmed-mean log2 luminance of the sky and land regions of one analysis image.</summary>
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<double> skyLogLuminance, ReadOnlySpan<double> landLogLuminance, DeflickerOptions? options = null, ReadOnlySpan<double> 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));
}
/// <summary>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).</summary>
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);
}
}