[{"id":33200,"web_url":"https://patchwork.libcamera.org/comment/33200/","msgid":"<Z5dUPS0mGuAtE2cc@pyrite.rasen.tech>","date":"2025-01-27T09:39:09","subject":"Re: [PATCH v2 05/17] libipa: Add grey world AWB algorithm","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"On Thu, Jan 23, 2025 at 12:40:55PM +0100, Stefan Klug wrote:\n> Add the grey world algorithm that is currently used in rkisp1 to libipa.\n> No changes in functionality were made.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\n\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\n> \n> ---\n> \n> Changes in v2:\n> - Fixed some typos\n> - Fixed result of gainsFromColourTemperature()\n> ---\n>  src/ipa/libipa/awb_grey.cpp | 114 ++++++++++++++++++++++++++++++++++++\n>  src/ipa/libipa/awb_grey.h   |  35 +++++++++++\n>  src/ipa/libipa/meson.build  |   2 +\n>  3 files changed, 151 insertions(+)\n>  create mode 100644 src/ipa/libipa/awb_grey.cpp\n>  create mode 100644 src/ipa/libipa/awb_grey.h\n> \n> diff --git a/src/ipa/libipa/awb_grey.cpp b/src/ipa/libipa/awb_grey.cpp\n> new file mode 100644\n> index 000000000000..49448976ed26\n> --- /dev/null\n> +++ b/src/ipa/libipa/awb_grey.cpp\n> @@ -0,0 +1,114 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024 Ideas on Board Oy\n> + *\n> + * Base class for grey world AWB algorithm\n> + */\n> +\n> +#include \"awb_grey.h\"\n> +\n> +#include <cmath>\n> +\n> +#include <libcamera/base/log.h>\n> +#include <libcamera/control_ids.h>\n> +\n> +#include \"colours.h\"\n> +\n> +using namespace libcamera::controls;\n> +\n> +/**\n> + * \\file awb_grey.h\n> + * \\brief Implementation of a grey world AWB algorithm\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DECLARE_CATEGORY(Awb)\n> +namespace ipa {\n> +\n> +/**\n> + * \\class AwbGrey\n> + * \\brief A Grey world auto white balance algorithm\n> + */\n> +\n> +/**\n> + * \\brief Initialize the algorithm with the given tuning data\n> + * \\param[in] tuningData The tuning data for the algorithm\n> + *\n> + * Load the colour temperature curve from the tuning data. If there is no tuning\n> + * data available, continue with a warning. Manual colour temperature will not\n> + * work in that case.\n> + *\n> + * \\return 0 on success, a negative error code otherwise\n> + */\n> +int AwbGrey::init(const YamlObject &tuningData)\n> +{\n> +\tInterpolator<Vector<double, 2>> gains;\n> +\tint ret = gains.readYaml(tuningData[\"colourGains\"], \"ct\", \"gains\");\n> +\tif (ret < 0)\n> +\t\tLOG(Awb, Warning)\n> +\t\t\t<< \"Failed to parse 'colourGains' \"\n> +\t\t\t<< \"parameter from tuning file; \"\n> +\t\t\t<< \"manual colour temperature will not work properly\";\n> +\telse\n> +\t\tcolourGainCurve_ = gains;\n> +\n> +\treturn 0;\n> +}\n> +\n> +/**\n> + * \\brief Calculate awb data from the given statistics\n> + * \\param[in] stats The statistics to use for the calculation\n> + * \\param[in] lux The lux value of the scene\n> + *\n> + * Estimates the colour temperature based on the colours::estimateCCT function.\n> + * The gains are calculated purely based on the RGB means provided by the \\a\n> + * stats. The colour temperature is not taken into account when calculating the\n> + * gains.\n> + *\n> + * The \\a lux parameter is not used in this algorithm.\n> + *\n> + * \\return The awb result\n> + */\n> +AwbResult AwbGrey::calculateAwb(const AwbStats &stats, [[maybe_unused]] int lux)\n> +{\n> +\tAwbResult result;\n> +\tauto means = stats.getRGBMeans();\n> +\tresult.colourTemperature = estimateCCT(means);\n> +\n> +\t/*\n> +\t * Estimate the red and blue gains to apply in a grey world. The green\n> +\t * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the\n> +\t * divisor to a minimum value of 1.0.\n> +\t */\n> +\tresult.gains.r() = means.g() / std::max(means.r(), 1.0);\n> +\tresult.gains.g() = 1.0;\n> +\tresult.gains.b() = means.g() / std::max(means.b(), 1.0);\n> +\treturn result;\n> +}\n> +\n> +/**\n> + * \\brief Compute white balance gains from a colour temperature\n> + * \\param[in] colourTemperature The colour temperature in Kelvin\n> + *\n> + * Compute the white balance gains from a \\a colourTemperature. This function\n> + * does not take any statistics into account. It simply interpolates the colour\n> + * gains configured in the colour temperature curve.\n> + *\n> + * \\return The colour gains if a colour temperature curve is available,\n> + * [1, 1, 1] otherwise.\n> + */\n> +RGB<double> AwbGrey::gainsFromColourTemperature(double colourTemperature)\n> +{\n> +\tif (!colourGainCurve_) {\n> +\t\tLOG(Awb, Error) << \"No gains defined\";\n> +\t\treturn RGB<double>({ 1.0, 1.0, 1.0 });\n> +\t}\n> +\n> +\tauto gains = colourGainCurve_->getInterpolated(colourTemperature);\n> +\treturn { { gains[0], 1.0, gains[1] } };\n> +}\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/awb_grey.h b/src/ipa/libipa/awb_grey.h\n> new file mode 100644\n> index 000000000000..7cb5cbf86873\n> --- /dev/null\n> +++ b/src/ipa/libipa/awb_grey.h\n> @@ -0,0 +1,35 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024 Ideas on Board Oy\n> + *\n> + * AWB grey world algorithm\n> + */\n> +\n> +#pragma once\n> +\n> +#include \"libcamera/internal/yaml_parser.h\"\n> +\n> +#include \"awb.h\"\n> +#include \"interpolator.h\"\n> +#include \"vector.h\"\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +class AwbGrey : public AwbAlgorithm\n> +{\n> +public:\n> +\tAwbGrey() = default;\n> +\n> +\tint init(const YamlObject &tuningData) override;\n> +\tAwbResult calculateAwb(const AwbStats &stats, int lux) override;\n> +\tRGB<double> gainsFromColourTemperature(double colourTemperature) override;\n> +\n> +private:\n> +\tstd::optional<Interpolator<Vector<double, 2>>> colourGainCurve_;\n> +};\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index 03e879c5834f..c550a6eb45b6 100644\n> --- a/src/ipa/libipa/meson.build\n> +++ b/src/ipa/libipa/meson.build\n> @@ -3,6 +3,7 @@\n>  libipa_headers = files([\n>      'agc_mean_luminance.h',\n>      'algorithm.h',\n> +    'awb_grey.h',\n>      'awb.h',\n>      'camera_sensor_helper.h',\n>      'colours.h',\n> @@ -21,6 +22,7 @@ libipa_headers = files([\n>  libipa_sources = files([\n>      'agc_mean_luminance.cpp',\n>      'algorithm.cpp',\n> +    'awb_grey.cpp',\n>      'awb.cpp',\n>      'camera_sensor_helper.cpp',\n>      'colours.cpp',\n> -- \n> 2.43.0\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 8E652BEFBE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 27 Jan 2025 09:39:25 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4CA406855D;\n\tMon, 27 Jan 2025 10:39:24 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7E2396851B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Jan 2025 10:39:22 +0100 (CET)","from pyrite.rasen.tech (unknown [206.0.71.13])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 03A8278D;\n\tMon, 27 Jan 2025 10:38:11 +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=\"bdCCoHqc\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1737970693;\n\tbh=iZ/h/fCBc65gJ7aiTw7lmMNuBPlT/QN26al6/pAFJ+E=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=bdCCoHqcfNNvVljkc+RCD2IMHckdQF5Wno6v/b3th96y6qODLGiPP8hNQL30aN1oA\n\tA4qcb+7k/cBBDIq3qPF4W56oXs2vCvEYY0+AjyqjyTj39px4FTV9NMeIhv5GlWbbJ2\n\tlLn6cVCwKj79nRZIBLrge/jEjXn7eVMBz83nC3w0=","Date":"Mon, 27 Jan 2025 04:39:09 -0500","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tDaniel Scally <dan.scally@ideasonboard.com>","Subject":"Re: [PATCH v2 05/17] libipa: Add grey world AWB algorithm","Message-ID":"<Z5dUPS0mGuAtE2cc@pyrite.rasen.tech>","References":"<20250123114204.79321-1-stefan.klug@ideasonboard.com>\n\t<20250123114204.79321-6-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20250123114204.79321-6-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>"}}]