feat(analysis): add white-balance smoother

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 09:51:43 +00:00
1 parent f6adbf7a31
commit a50ad7deb0
2 files changed
+126

No files matched your search

@@ -0,0 +1,55 @@
using Titano.Core.Imaging;
using Titano.Core.Mathematics;
namespace Titano.Core.Analysis;
/// <summary>Per-frame white-balance smoothing result; ratios are log2(R/G) and log2(B/G).</summary>
public sealed record WhiteBalanceResult(
ChannelGains[] Gains,
double[] RedRatio,
double[] BlueRatio,
double[] SmoothedRedRatio,
double[] SmoothedBlueRatio);
/// <summary>
/// White-balance smoothing (BRIEF §11.2): the channel ratios log2(R/G) and log2(B/G) of each frame's mean colour are
/// smoothed with robust LOWESS, and each frame gets the channel gains that move its ratios onto the smoothed ones.
/// Gains are normalised so the Rec.709 luminance of the frame's mean colour is unchanged (WB never alters exposure).
/// Frames with a non-positive channel mean get identity gains.
/// </summary>
public static class WhiteBalanceSmoother
{
public static WhiteBalanceResult Smooth(ReadOnlySpan<double> meanRed, ReadOnlySpan<double> meanGreen, ReadOnlySpan<double> meanBlue, int window = 31, int robustIterations = 2)
{
var n = meanRed.Length;
if (meanGreen.Length != n || meanBlue.Length != n) throw new ArgumentException("Channel series lengths differ.", nameof(meanGreen));
var red = new double[n];
var blue = new double[n];
for (var i = 0; i < n; i++)
{
var valid = meanRed[i] > 0 && meanGreen[i] > 0 && meanBlue[i] > 0 && double.IsFinite(meanRed[i] + meanGreen[i] + meanBlue[i]);
red[i] = valid ? Math.Log2(meanRed[i] / meanGreen[i]) : double.NaN;
blue[i] = valid ? Math.Log2(meanBlue[i] / meanGreen[i]) : double.NaN;
}
var smoothRed = LocalRegression.Smooth(red, window, 1, robustIterations).Fitted;
var smoothBlue = LocalRegression.Smooth(blue, window, 1, robustIterations).Fitted;
var gains = new ChannelGains[n];
for (var i = 0; i < n; i++)
{
if (double.IsNaN(red[i]))
{
gains[i] = ChannelGains.Identity;
continue;
}
var gr = Math.Pow(2, smoothRed[i] - red[i]);
var gb = Math.Pow(2, smoothBlue[i] - blue[i]);
double r = meanRed[i], g = meanGreen[i], b = meanBlue[i];
var k = Luma(r, g, b) / Luma(gr * r, g, gb * b);
gains[i] = new ChannelGains(k * gr, k, k * gb);
}
return new WhiteBalanceResult(gains, red, blue, smoothRed, smoothBlue);
}
private static double Luma(double r, double g, double b) => ColorSpace.LumaR * r + ColorSpace.LumaG * g + ColorSpace.LumaB * b;
}