Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpColorHistogram.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
35#ifndef VP_COLOR_HISTOGRAM_H
36#define VP_COLOR_HISTOGRAM_H
37
38#include <visp3/core/vpConfig.h>
39#include <visp3/core/vpRGBa.h>
40
41#include <vector>
42#include <numeric>
43
45
46template<typename T>
47class vpImage;
48class vpRGBa;
49class vpRect;
56class VISP_EXPORT vpColorHistogram
57{
58public:
59
60 class VISP_EXPORT Builder
61 {
62 public:
63 Builder(unsigned int N) : m_counts(N *N *N, 0), m_N(N), m_binSize(256 / N) { }
64 inline void add(const vpRGBa &color)
65 {
66 unsigned int index = (color.R / m_binSize) * (m_N * m_N) + (color.G / m_binSize) * m_N + (color.B / m_binSize);
67 ++m_counts[index];
68 }
69 void build(vpColorHistogram &histogram);
70 private:
71 std::vector<unsigned int> m_counts;
72 unsigned int m_N, m_binSize;
73 };
74
76 vpColorHistogram(unsigned int N);
77
84 void setBinNumber(unsigned int N);
85
86 unsigned int getBinNumber() const { return m_N; }
87
93 unsigned int getNumPixels() const { return m_numPixels; }
94
102 void build(const vpImage<vpRGBa> &image, const vpImage<bool> &mask);
103
111 void build(const std::vector<unsigned int> &counts);
112
120 void merge(const vpColorHistogram &other, float alpha);
121
128 void computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba) const;
138 void computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba, const vpRect &bb) const;
139
146 inline unsigned int colorToIndex(const vpRGBa &p) const
147 {
148 return (p.R / m_binSize) * (m_N * m_N) + (p.G / m_binSize) * m_N + (p.B / m_binSize);
149 }
150
156 inline vpRGBa indexToColor(unsigned int index) const
157 {
158 vpRGBa c;
159 c.R = (index / (m_N * m_N)) * m_binSize;
160 c.G = ((index / m_N) % (m_N)) * m_binSize;
161 c.B = (index % m_N) * m_binSize;
162 c.A = 255;
163 return c;
164 }
165
172 inline double probability(const vpRGBa &color) const
173 {
174 return m_probas[colorToIndex(color)];
175 }
176
177 double kl(const vpColorHistogram &other) const;
178
179 double jsd(const vpColorHistogram &other) const;
180
181 double hellinger(const vpColorHistogram &other) const;
182
183 static void computeSplitHistograms(const vpImage<vpRGBa> &image, const vpImage<bool> &mask, vpColorHistogram &inMask, vpColorHistogram &outsideMask);
184 static void computeSplitHistograms(const vpImage<vpRGBa> &image, const vpImage<bool> &mask, const vpRect &bbInside, vpColorHistogram &insideMask, vpColorHistogram &outsideMask);
191 std::vector<vpRGBa> mostLikelyColors(unsigned int N) const
192 {
193 std::vector<size_t> bestIndices(N);
194 std::vector<size_t> idx(m_probas.size());
195 std::iota(idx.begin(), idx.end(), 0);
196 std::partial_sort_copy(
197 idx.begin(), idx.end(),
198 bestIndices.begin(), bestIndices.end(),
199 [*this](size_t i1, size_t i2) {return m_probas[i1] > m_probas[i2]; }
200 );
201
202 std::vector<vpRGBa> colors(N);
203 for (unsigned int i = 0; i < N; ++i) {
204 colors[i] = indexToColor(static_cast<unsigned int>(bestIndices[i]));
205 }
206 return colors;
207 }
208
209private:
211 unsigned int m_N;
213 unsigned int m_binSize;
215 std::vector<float> m_probas;
217 unsigned int m_numPixels;
218};
219
220END_VISP_NAMESPACE
221
222#endif
void add(const vpRGBa &color)
Histogram representation of an RGB color distribution In this representation, probabilities are store...
unsigned int getNumPixels() const
Get the number of pixels used to compute this histogram. Can be useful when merging different histogr...
double probability(const vpRGBa &color) const
Get the probability of an RGB color according to this histogram.
void build(const vpImage< vpRGBa > &image, const vpImage< bool > &mask)
Build the histogram representation and associated color probabilities given an image and a mask....
unsigned int getBinNumber() const
vpRGBa indexToColor(unsigned int index) const
Convert an index value to a color.
std::vector< vpRGBa > mostLikelyColors(unsigned int N) const
Get the N most likely colors according to this histogram.
unsigned int colorToIndex(const vpRGBa &p) const
Convert an RGB color to an index that can be used to retrieve the probability of this color The alpha...
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned char B
Blue component.
Definition vpRGBa.h:327
unsigned char R
Red component.
Definition vpRGBa.h:325
unsigned char G
Green component.
Definition vpRGBa.h:326
unsigned char A
Additional component.
Definition vpRGBa.h:328
Defines a rectangle in the plane.
Definition vpRect.h:79