From d476f8358be1536d4edd680c6024f784ff022f5d Mon Sep 17 00:00:00 2001 From: Stefan Klug Date: Thu, 23 Jan 2025 12:41:07 +0100 Subject: libipa: awb_bayes: Change the probabilities from log space to linear space The original code used to specify the probabilities in log space and scaled for the RaspberryPi hardware with 192 AWB measurement points. This is reasonable as the whole algorithm makes use of unitless numbers to prefer some colour temperatures based on a lux level. These numbers are then hand tuned with the specific device in mind. This has two shortcomings: 1. The linear interpolation of PWLs in log space is mathematically incorrect. The outcome might still be ok, as both spaces (log and linear) are monotonic, but it is still not "right". 2. Having unitless numbers gets more error prone when we try to harmonize the behavior over multiple platforms. Change the algorithm to interpret the numbers as being in linear space. This makes the interpolation mathematically correct at the expense of a few log operations. To account for that change, update the numbers in the tuning example file with the linear counterparts scaled to one AWB zone measurement. Signed-off-by: Stefan Klug Reviewed-by: Paul Elder Reviewed-by: Daniel Scally --- src/ipa/libipa/awb_bayes.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/ipa/libipa/awb_bayes.cpp') diff --git a/src/ipa/libipa/awb_bayes.cpp b/src/ipa/libipa/awb_bayes.cpp index c3e0b69b..e75bfcd6 100644 --- a/src/ipa/libipa/awb_bayes.cpp +++ b/src/ipa/libipa/awb_bayes.cpp @@ -234,6 +234,10 @@ int AwbBayes::readPriors(const YamlObject &tuningData) auto &pwl = priors[lux]; for (const auto &[ct, prob] : ctToProbability) { + if (prob < 1e-6) { + LOG(Awb, Error) << "Prior probability must be larger than 1e-6"; + return -EINVAL; + } pwl.append(ct, prob); } } @@ -323,7 +327,7 @@ double AwbBayes::coarseSearch(const ipa::Pwl &prior, const AwbStats &stats) cons double b = ctB_.eval(t, &spanB); RGB gains({ 1 / r, 1.0, 1 / b }); double delta2Sum = stats.computeColourError(gains); - double priorLogLikelihood = prior.eval(prior.domain().clamp(t)); + double priorLogLikelihood = log(prior.eval(prior.domain().clamp(t))); double finalLogLikelihood = delta2Sum - priorLogLikelihood; errorLimits.record(delta2Sum); @@ -406,7 +410,7 @@ void AwbBayes::fineSearch(double &t, double &r, double &b, ipa::Pwl const &prior for (int i = -nsteps; i <= nsteps; i++) { double tTest = t + i * step; double priorLogLikelihood = - prior.eval(prior.domain().clamp(tTest)); + log(prior.eval(prior.domain().clamp(tTest))); priorLogLikelihoodLimits.record(priorLogLikelihood); Pwl::Point rbStart{ { ctR_.eval(tTest, &spanR), ctB_.eval(tTest, &spanB) } }; -- cgit v1.2.1