{"id":11800,"url":"https://patchwork.libcamera.org/api/patches/11800/?format=json","web_url":"https://patchwork.libcamera.org/patch/11800/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210330211210.194806-3-jeanmichel.hautbois@ideasonboard.com>","date":"2021-03-30T21:12:08","name":"[libcamera-devel,v4,2/4] WIP: ipa: ipu3: Add an histogram class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"df582155e2eda82113655347edee2787b46c1d7c","submitter":{"id":75,"url":"https://patchwork.libcamera.org/api/people/75/?format=json","name":"Jean-Michel Hautbois","email":"jeanmichel.hautbois@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11800/mbox/","series":[{"id":1874,"url":"https://patchwork.libcamera.org/api/series/1874/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1874","date":"2021-03-30T21:12:06","name":"Implement IPA algorithms and demo with IPU3","version":4,"mbox":"https://patchwork.libcamera.org/series/1874/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11800/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11800/checks/","tags":{},"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 B0FDFC0DA3\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Mar 2021 21:12:21 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 565006878B;\n\tTue, 30 Mar 2021 23:12:21 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AABFD602D2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Mar 2021 23:12:18 +0200 (CEST)","from localhost.localdomain (unknown\n\t[IPv6:2a01:e0a:169:7140:40dc:d947:395c:88d2])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0A2B4102;\n\tTue, 30 Mar 2021 23:12:18 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"MEY3Zri9\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1617138738;\n\tbh=I5Ut3x5rARrfmv6+wtRztv9qaZu1DOsFyvwcBBXMQR8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=MEY3Zri9528QJICClhef+fmS2rt8SMCcify0PKWWyssiXTz1xqT/kmjQc9rPXb3ch\n\t62PAP0DL/RDu8eGiihVrKYsPpELPVzjFL6OTuCWn16q+BLmkC6Lu+JdCPg4kVsyEm5\n\tveQ0YBepfpAdsAEGq4rlZum6Pq+PL0kkxMsZza9k=","From":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 30 Mar 2021 23:12:08 +0200","Message-Id":"<20210330211210.194806-3-jeanmichel.hautbois@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20210330211210.194806-1-jeanmichel.hautbois@ideasonboard.com>","References":"<20210330211210.194806-1-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v4 2/4] WIP: ipa: ipu3: Add an histogram\n\tclass","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>","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"This class will be used at least by AGC algorithm when quantiles are\nneeded for example.\n\nSigned-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n---\n src/ipa/libipa/histogram.cpp                 | 154 +++++++++++++++++++\n src/ipa/libipa/histogram.h                   |  40 +++++\n src/ipa/libipa/meson.build                   |   2 +\n src/ipa/raspberrypi/controller/histogram.hpp |   9 +-\n 4 files changed, 197 insertions(+), 8 deletions(-)\n create mode 100644 src/ipa/libipa/histogram.cpp\n create mode 100644 src/ipa/libipa/histogram.h","diff":"diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp\nnew file mode 100644\nindex 00000000..46fbb940\n--- /dev/null\n+++ b/src/ipa/libipa/histogram.cpp\n@@ -0,0 +1,154 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi (Trading) Limited\n+ *\n+ * histogram.cpp - histogram calculations\n+ */\n+#include \"histogram.h\"\n+\n+#include <cmath>\n+\n+#include \"libcamera/internal/log.h\"\n+\n+/**\n+ * \\file histogram.h\n+ * \\brief Class to represent Histograms and manipulate them\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+/**\n+ * \\class Histogram\n+ * \\brief The base class for creating histograms\n+ *\n+ * The Histogram class defines a standard interface for IPA algorithms. By\n+ * abstracting histograms, it makes it possible to implement generic code\n+ * to manage histograms regardless of their specific type.\n+ *\n+ * This class stores a cumulative frequency histogram, which is a mapping that\n+ * counts the cumulative number of observations in all of the bins up to the\n+ * specified bin. It can be used to find quantiles and averages between quantiles.\n+ */\n+\n+/**\n+ * \\brief Create a cumulative histogram with a bin number of intervals\n+ * \\param[in] data a reference to the histogram\n+ */\n+Histogram::Histogram(Span<uint32_t> data)\n+{\n+\tcumulative_.reserve(data.size());\n+\tcumulative_.push_back(0);\n+\tfor (const uint32_t &value : data)\n+\t\tcumulative_.push_back(cumulative_.back() + value);\n+}\n+/**\n+ * \\fn Histogram::bins()\n+ * \\brief getter for number of bins\n+ * \\return Number of bins\n+ */\n+/**\n+ * \\fn Histogram::total()\n+ * \\brief getter for number of values\n+ * \\return Number of values\n+ */\n+\n+/**\n+ * \\brief Cumulative frequency up to a (fractional) point in a bin.\n+ * \\param[in] bin the bin upon which to cumulate\n+ *\n+ * With F(p) the cumulative frequency of the histogram, the value is 0 at\n+ * the bottom of the histogram, and the maximum is the number of bins.\n+ * The pixels are spread evenly throughout the “bin” in which they lie, so that\n+ * F(p) is a continuous (monotonically increasing) function.\n+ *\n+ * \\return The cumulated frequency from 0 up to the specified bin\n+ */\n+uint64_t Histogram::cumulativeFreq(double bin) const\n+{\n+\tif (bin <= 0)\n+\t\treturn 0;\n+\telse if (bin >= bins())\n+\t\treturn total();\n+\tint b = static_cast<int32_t>(bin);\n+\treturn cumulative_[b] +\n+\t       (bin - b) * (cumulative_[b + 1] - cumulative_[b]);\n+}\n+\n+/**\n+ * \\brief Return the (fractional) bin of the point through the histogram\n+ * \\param[in] q the desired point (0 <= q <= 1)\n+ * \\param[in] first low limit (default is 0)\n+ * \\param[in] last high limit (default is UINT_MAX)\n+ *\n+ * A quantile gives us the point p = Q(q) in the range such that a proportion\n+ * q of the pixels lie below p. A familiar quantile is Q(0.5) which is the median\n+ * of a distribution.\n+ *\n+ * \\return The fractional bin of the point\n+ */\n+double Histogram::quantile(double q, uint32_t first, uint32_t last) const\n+{\n+\tif (last == UINT_MAX)\n+\t\tlast = cumulative_.size() - 2;\n+\tASSERT(first <= last);\n+\n+\tuint64_t item = q * total();\n+\t/* Binary search to find the right bin */\n+\twhile (first < last) {\n+\t\tint middle = (first + last) / 2;\n+\t\t/* Is it between first and middle ? */\n+\t\tif (cumulative_[middle + 1] > item)\n+\t\t\tlast = middle;\n+\t\telse\n+\t\t\tfirst = middle + 1;\n+\t}\n+\tASSERT(item >= cumulative_[first] && item <= cumulative_[last + 1]);\n+\n+\tdouble frac;\n+\tif (cumulative_[first + 1] == cumulative_[first])\n+\t\tfrac = 0;\n+\telse\n+\t\tfrac = (item - cumulative_[first]) / (cumulative_[first + 1] - cumulative_[first]);\n+\treturn first + frac;\n+}\n+\n+/**\n+ * \\brief Calculate the mean between two quantiles\n+ * \\param[in] lowQuantile low Quantile\n+ * \\param[in] highQuantile high Quantile\n+ *\n+ * Quantiles are not ideal for metering as they suffer several limitations.\n+ * Instead, a concept is introduced here: inter-quantile mean.\n+ * It returns the mean of all pixels between lowQuantile and highQuantile.\n+ *\n+ * \\return The mean histogram bin value between the two quantiles\n+ */\n+double Histogram::interQuantileMean(double lowQuantile, double highQuantile) const\n+{\n+\tASSERT(highQuantile > lowQuantile);\n+\t/* Proportion of pixels which lies below lowQuantile */\n+\tdouble lowPoint = quantile(lowQuantile);\n+\t/* Proportion of pixels which lies below highQuantile */\n+\tdouble highPoint = quantile(highQuantile, static_cast<uint32_t>(lowPoint));\n+\tdouble sumBinFreq = 0, cumulFreq = 0;\n+\n+\tfor (double p_next = floor(lowPoint) + 1.0;\n+\t     p_next <= ceil(highPoint);\n+\t     lowPoint = p_next, p_next += 1.0) {\n+\t\tint bin = floor(lowPoint);\n+\t\tdouble freq = (cumulative_[bin + 1] - cumulative_[bin]) * (std::min(p_next, highPoint) - lowPoint);\n+\n+\t\t/* Accumulate weigthed bin */\n+\t\tsumBinFreq += bin * freq;\n+\t\t/* Accumulate weights */\n+\t\tcumulFreq += freq;\n+\t}\n+\t/* add 0.5 to give an average for bin mid-points */\n+\treturn sumBinFreq / cumulFreq + 0.5;\n+}\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/histogram.h b/src/ipa/libipa/histogram.h\nnew file mode 100644\nindex 00000000..dc7451aa\n--- /dev/null\n+++ b/src/ipa/libipa/histogram.h\n@@ -0,0 +1,40 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi (Trading) Limited\n+ *\n+ * histogram.h - histogram calculation interface\n+ */\n+#ifndef __LIBCAMERA_IPA_LIBIPA_HISTOGRAM_H__\n+#define __LIBCAMERA_IPA_LIBIPA_HISTOGRAM_H__\n+\n+#include <assert.h>\n+#include <limits.h>\n+#include <stdint.h>\n+\n+#include <vector>\n+\n+#include <libcamera/span.h>\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class Histogram\n+{\n+public:\n+\tHistogram(Span<uint32_t> data);\n+\tsize_t bins() const { return cumulative_.size() - 1; }\n+\tuint64_t total() const { return cumulative_[cumulative_.size() - 1]; }\n+\tuint64_t cumulativeFreq(double bin) const;\n+\tdouble quantile(double q, uint32_t first = 0, uint32_t last = UINT_MAX) const;\n+\tdouble interQuantileMean(double lowQuantile, double hiQuantile) const;\n+\n+private:\n+\tstd::vector<uint64_t> cumulative_;\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_IPA_LIBIPA_HISTOGRAM_H__ */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 1819711d..038fc490 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -2,10 +2,12 @@\n \n libipa_headers = files([\n     'algorithm.h',\n+    'histogram.h'\n ])\n \n libipa_sources = files([\n     'algorithm.cpp',\n+    'histogram.cpp'\n ])\n \n libipa_includes = include_directories('..')\ndiff --git a/src/ipa/raspberrypi/controller/histogram.hpp b/src/ipa/raspberrypi/controller/histogram.hpp\nindex 90f5ac78..fc236416 100644\n--- a/src/ipa/raspberrypi/controller/histogram.hpp\n+++ b/src/ipa/raspberrypi/controller/histogram.hpp\n@@ -10,9 +10,6 @@\n #include <vector>\n #include <cassert>\n \n-// A simple histogram class, for use in particular to find \"quantiles\" and\n-// averages between \"quantiles\".\n-\n namespace RPiController {\n \n class Histogram\n@@ -29,12 +26,8 @@ public:\n \t}\n \tuint32_t Bins() const { return cumulative_.size() - 1; }\n \tuint64_t Total() const { return cumulative_[cumulative_.size() - 1]; }\n-\t// Cumulative frequency up to a (fractional) point in a bin.\n \tuint64_t CumulativeFreq(double bin) const;\n-\t// Return the (fractional) bin of the point q (0 <= q <= 1) through the\n-\t// histogram. Optionally provide limits to help.\n-\tdouble Quantile(double q, int first = -1, int last = -1) const;\n-\t// Return the average histogram bin value between the two quantiles.\n+\tHistogram::quantile(double q, uint32_t first = 0, uint32_t last = UINT_MAX) const;\n \tdouble InterQuantileMean(double q_lo, double q_hi) const;\n \n private:\n","prefixes":["libcamera-devel","v4","2/4"]}