Visual Servoing Platform  version 3.6.1 under development (2025-03-16)
vpColorHistogram.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
43 BEGIN_VISP_NAMESPACE
44 
45 template<typename T>
46 class vpImage;
47 class vpRGBa;
48 class vpRect;
49 
50 
51 
52 class VISP_EXPORT vpColorHistogram
53 {
54 public:
55 
56  class Builder
57  {
58  public:
59  Builder(unsigned int N) : m_counts(N *N *N, 0), m_N(N), m_binSize(256 / N) { }
60  inline void add(const vpRGBa &color)
61  {
62  unsigned int index = (color.R / m_binSize) * (m_N * m_N) + (color.G / m_binSize) * m_N + (color.B / m_binSize);
63  ++m_counts[index];
64  }
65  void build(vpColorHistogram &histogram);
66  private:
67  std::vector<unsigned int> m_counts;
68  unsigned int m_N, m_binSize;
69  };
70 
72  vpColorHistogram(unsigned int N);
73 
79  void setBinNumber(unsigned int N);
80 
81  unsigned int getBinNumber() const { return m_N; }
82 
83  unsigned int getNumPixels() const { return m_numPixels; }
84 
85  void build(const vpImage<vpRGBa> &image, const vpImage<bool> &mask);
86 
87  void build(const std::vector<unsigned int> &counts);
88 
89  void merge(const vpColorHistogram &other, float alpha);
90 
91  void computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba) const;
92  void computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba, const vpRect &bb) const;
93 
94 
95  inline unsigned int colorToIndex(const vpRGBa &p) const
96  {
97  return (p.R / m_binSize) * (m_N * m_N) + (p.G / m_binSize) * m_N + (p.B / m_binSize);
98  }
99 
100  inline double probability(const vpRGBa &color) const
101  {
102  return m_probas[colorToIndex(color)];
103  }
104 
105  double kl(const vpColorHistogram &other) const;
106 
107  double jsd(const vpColorHistogram &other) const;
108 
109  double hellinger(const vpColorHistogram &other) const;
110 
111  static void computeSplitHistograms(const vpImage<vpRGBa> &image, const vpImage<bool> &mask, vpColorHistogram &inMask, vpColorHistogram &outsideMask);
112  static void computeSplitHistograms(const vpImage<vpRGBa> &image, const vpImage<bool> &mask, const vpRect &bbInside, vpColorHistogram &insideMask, vpColorHistogram &outsideMask);
113 
114 private:
115  unsigned int m_N;
116  unsigned int m_binSize;
117  std::vector<float> m_probas;
118  unsigned int m_numPixels;
119 };
120 
121 END_VISP_NAMESPACE
122 
123 #endif
void add(const vpRGBa &color)
unsigned int getNumPixels() const
double probability(const vpRGBa &color) const
unsigned int getBinNumber() const
unsigned int colorToIndex(const vpRGBa &p) const
Definition of the vpImage class member functions.
Definition: vpImage.h:131
Definition: vpRGBa.h:70
unsigned char B
Blue component.
Definition: vpRGBa.h:187
unsigned char R
Red component.
Definition: vpRGBa.h:185
unsigned char G
Green component.
Definition: vpRGBa.h:186
Defines a rectangle in the plane.
Definition: vpRect.h:79