[{"id":33418,"web_url":"https://patchwork.libcamera.org/comment/33418/","msgid":"<20250223225628.GC16159@pendragon.ideasonboard.com>","date":"2025-02-23T22:56:28","subject":"Re: [PATCH v2 10/17] ipa: rkisp1: Add support for bayes AWB\n\talgorithm from libipa","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Stefan,\n\nOn Thu, Jan 23, 2025 at 12:41:00PM +0100, Stefan Klug wrote:\n> Now that libipa contains a bayes AWB algorithm, add it as supported\n> algorithm to the rkisp1 ipa.\n> \n> The decision between the grey world algorithm and the bayesian is done\n> based on the \"algorithm\" property of the \"Awb\" algorithm in the tuning\n> file. If the lux value in the frameContext is set by the Lux algorithm\n> it is taken into account. If the lux value is 0 the prior likelihood\n> estimation gets ignored in the AWB calculations.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\n> \n> ---\n> \n> Changes in v2:\n> - Collected tags\n> ---\n>  src/ipa/rkisp1/algorithms/awb.cpp | 52 +++++++++++++++++++++++--------\n>  1 file changed, 39 insertions(+), 13 deletions(-)\n> \n> diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\n> index b21d0d4c03bb..55e1b43c8cd2 100644\n> --- a/src/ipa/rkisp1/algorithms/awb.cpp\n> +++ b/src/ipa/rkisp1/algorithms/awb.cpp\n> @@ -16,6 +16,7 @@\n>  \n>  #include <libcamera/ipa/core_ipa_interface.h>\n>  \n> +#include \"libipa/awb_bayes.h\"\n>  #include \"libipa/awb_grey.h\"\n>  #include \"libipa/colours.h\"\n>  \n> @@ -45,13 +46,23 @@ class RkISP1AwbStats : public AwbStats\n>  {\n>  public:\n>  \tRkISP1AwbStats(const RGB<double> &rgbMeans)\n> -\t\t: rgbMeans_(rgbMeans) {}\n> +\t\t: rgbMeans_(rgbMeans)\n> +\t{\n> +\t\trg_ = rgbMeans_.r() / rgbMeans_.g();\n> +\t\tbg_ = rgbMeans_.b() / rgbMeans_.g();\n> +\t}\n>  \n> -\tdouble computeColourError([[maybe_unused]] const RGB<double> &gains) const override\n> +\tdouble computeColourError(const RGB<double> &gains) const override\n>  \t{\n> -\t\tLOG(RkISP1Awb, Error)\n> -\t\t\t<< \"RkISP1AwbStats::computeColourError is not implemented\";\n> -\t\treturn 0.0;\n> +\t\t/*\n> +\t\t* Compute the sum of the squared colour error (non-greyness) as it\n> +\t\t* appears in the log likelihood equation.\n> +\t\t*/\n> +\t\tdouble deltaR = gains.r() * rg_ - 1.0;\n> +\t\tdouble deltaB = gains.b() * bg_ - 1.0;\n> +\t\tdouble delta2 = deltaR * deltaR + deltaB * deltaB;\n> +\n> +\t\treturn delta2;\n>  \t}\n>  \n>  \tRGB<double> getRGBMeans() const override\n> @@ -61,6 +72,8 @@ public:\n>  \n>  private:\n>  \tRGB<double> rgbMeans_;\n> +\tdouble rg_;\n> +\tdouble bg_;\n>  };\n>  \n>  Awb::Awb()\n> @@ -78,13 +91,30 @@ int Awb::init(IPAContext &context, const YamlObject &tuningData)\n>  \t\t\t\t\t\t\t kMaxColourTemperature,\n>  \t\t\t\t\t\t\t kDefaultColourTemperature);\n>  \n> -\tawbAlgo_ = std::make_unique<AwbGrey>();\n> +\tif (!tuningData.contains(\"algorithm\"))\n> +\t\tLOG(RkISP1Awb, Info) << \"No awb algorithm specified.\"\n> +\t\t\t\t     << \" Default to grey world\";\n> +\n> +\tauto mode = tuningData[\"algorithm\"].get<std::string>(\"grey\");\n> +\tif (mode == \"grey\") {\n> +\t\tawbAlgo_ = std::make_unique<AwbGrey>();\n> +\t} else if (mode == \"bayes\") {\n> +\t\tawbAlgo_ = std::make_unique<AwbBayes>();\n> +\t} else {\n> +\t\tLOG(RkISP1Awb, Error) << \"Unknown awb algorithm: \" << mode;\n> +\t\treturn -EINVAL;\n> +\t}\n> +\tLOG(RkISP1Awb, Debug) << \"Using awb algorithm: \" << mode;\n> +\n>  \tint ret = awbAlgo_->init(tuningData);\n>  \tif (ret) {\n>  \t\tLOG(RkISP1Awb, Error) << \"Failed to init awb algorithm\";\n>  \t\treturn ret;\n>  \t}\n>  \n> +\tconst auto &src = awbAlgo_->controls();\n> +\tcmap.insert(src.begin(), src.end());\n> +\n>  \treturn 0;\n>  }\n>  \n> @@ -131,6 +161,8 @@ void Awb::queueRequest(IPAContext &context,\n>  \t\t\t<< (*awbEnable ? \"Enabling\" : \"Disabling\") << \" AWB\";\n>  \t}\n>  \n> +\tawbAlgo_->handleControls(controls);\n> +\n>  \tframeContext.awb.autoEnabled = awb.autoEnabled;\n>  \n>  \tif (awb.autoEnabled)\n\nThere's a comment a bit below that states\n\n\t\t/*\n\t\t * \\todo: Colour temperature reported in metadata is now\n\t\t * incorrect, as we can't deduce the temperature from the gains.\n\t\t * This will be fixed with the bayes AWB algorithm.\n\t\t */\n\nHas this been addressed, can it be removed ?\n\n> @@ -271,14 +303,8 @@ void Awb::process(IPAContext &context,\n>  \t    rgbMeans.b() < kMeanMinThreshold)\n>  \t\treturn;\n>  \n> -\t/*\n> -\t * \\Todo: Hardcode lux to a fixed value, until an estimation is\n> -\t * implemented.\n> -\t */\n> -\tint lux = 1000;\n> -\n>  \tRkISP1AwbStats awbStats{ rgbMeans };\n> -\tAwbResult awbResult = awbAlgo_->calculateAwb(awbStats, lux);\n> +\tAwbResult awbResult = awbAlgo_->calculateAwb(awbStats, frameContext.lux.lux);\n>  \n>  \tactiveState.awb.temperatureK = awbResult.colourTemperature;\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id CBC23C324E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 23 Feb 2025 22:56:48 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D7632686B8;\n\tSun, 23 Feb 2025 23:56:47 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 29DA2686A7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 23 Feb 2025 23:56:46 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E6B20496;\n\tSun, 23 Feb 2025 23:55:19 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"NRSp7gj6\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1740351320;\n\tbh=JrrIr9Q29S3rTvv22IGIl09v/lsGmXmeNaqq7HMLH+g=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=NRSp7gj6P9NRYpSlh9aXF6Zte9wonubRoYeJ+KbCdGSxVhUsvDtFga8qdBcsxwI+N\n\tze5mnXcTR2+7tPHvt2fuCrJ737xZ+bYEaCSDaAT7HhJLKc1RaI/yIu/FeTpM5n4vC/\n\tAl0AcNinyij/WC8QhbjfZrEGVqGmUMCrm2pH1B+c=","Date":"Mon, 24 Feb 2025 00:56:28 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tPaul Elder <paul.elder@ideasonboard.com>,\n\tDaniel Scally <dan.scally@ideasonboard.com>","Subject":"Re: [PATCH v2 10/17] ipa: rkisp1: Add support for bayes AWB\n\talgorithm from libipa","Message-ID":"<20250223225628.GC16159@pendragon.ideasonboard.com>","References":"<20250123114204.79321-1-stefan.klug@ideasonboard.com>\n\t<20250123114204.79321-11-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20250123114204.79321-11-stefan.klug@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]