Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpHistogram.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  * Description:
31  * Gray level histogram manipulation.
32  */
33 
41 #ifndef VP_HISTOGRAM_H
42 #define VP_HISTOGRAM_H
43 
44 #include <sstream>
45 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpColor.h>
48 #include <visp3/core/vpHistogramPeak.h>
49 #include <visp3/core/vpHistogramValey.h>
50 #include <visp3/core/vpImage.h>
51 
52 #include <list>
53 
105 class VISP_EXPORT vpHistogram
106 {
107 public:
108  vpHistogram();
109  vpHistogram(const vpHistogram &h);
110  VP_EXPLICIT vpHistogram(const vpImage<unsigned char> &I);
111  VP_EXPLICIT vpHistogram(const vpImage<unsigned char> &I, const vpImage<bool> *p_mask);
112  virtual ~vpHistogram();
113 
114  vpHistogram &operator=(const vpHistogram &h);
115 
136  inline unsigned operator[](const unsigned char level) const
137  {
138  if (level < m_size) {
139  return m_histogram[level];
140  }
141 
142  std::stringstream ss;
143  ss << "Level is > to size (" << m_size << ") !";
144  throw vpException(vpException::dimensionError, ss.str().c_str());
145  };
166  inline unsigned operator()(const unsigned char level) const
167  {
168  if (level < m_size) {
169  return m_histogram[level];
170  }
171 
172  std::stringstream ss;
173  ss << "Level is > to size (" << m_size << ") !";
174  throw vpException(vpException::dimensionError, ss.str().c_str());
175  };
196  inline unsigned get(const unsigned char level) const
197  {
198  if (level < m_size) {
199  return m_histogram[level];
200  }
201 
202  std::stringstream ss;
203  ss << "Level is > to size (" << m_size << ") !";
204  throw vpException(vpException::dimensionError, ss.str().c_str());
205  };
206 
224  inline void set(const unsigned char level, unsigned int value)
225  {
226  if (level < m_size) {
227  m_histogram[level] = value;
228  }
229  else {
230  std::stringstream ss;
231  ss << "Level is > to size (" << m_size << ") !";
232  throw vpException(vpException::dimensionError, ss.str().c_str());
233  }
234  };
235 
246  inline void setMask(const vpImage<bool> *p_mask)
247  {
248  mp_mask = p_mask;
249  }
250 
251  void calculate(const vpImage<unsigned char> &I, unsigned int nbins = 256, unsigned int nbThreads = 1);
252  void equalize(const vpImage<unsigned char> &I, vpImage<unsigned char> &Iout);
253 
254  void display(const vpImage<unsigned char> &I, const vpColor &color = vpColor::white, unsigned int thickness = 2,
255  unsigned int maxValue_ = 0);
256 
257  void smooth(unsigned int fsize = 3);
258  unsigned getPeaks(std::list<vpHistogramPeak> &peaks);
259  unsigned getPeaks(unsigned char dist, vpHistogramPeak &peak1, vpHistogramPeak &peak2);
260  bool getPeaks(unsigned char dist, vpHistogramPeak &peakl, vpHistogramPeak &peakr, vpHistogramValey &valey);
261  unsigned getValey(std::list<vpHistogramValey> &valey);
262  bool getValey(const vpHistogramPeak &peak1, const vpHistogramPeak &peak2, vpHistogramValey &valey);
263  unsigned getValey(unsigned char dist, const vpHistogramPeak &peak, vpHistogramValey &valeyl,
264  vpHistogramValey &valeyr);
265  unsigned sort(std::list<vpHistogramPeak> &peaks);
266 
267  bool write(const std::string &filename);
268  bool write(const char *filename);
269 
278  inline unsigned getSize() const { return m_size; };
279 
301  inline unsigned *getValues() { return m_histogram; };
302 
308  inline unsigned int getTotal() { return m_total; };
309 
310 private:
311  void init(unsigned size = 256);
312 
313  unsigned int *m_histogram;
314  unsigned m_size;
315  const vpImage<bool> *mp_mask;
316  unsigned int m_total;
317 };
318 END_VISP_NAMESPACE
319 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor white
Definition: vpColor.h:212
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
Declaration of the peak (maximum value) in a gray level image histogram.
Declaration of the valey (minimum value) in a gray level image histogram.
Class to compute a gray level image histogram.
Definition: vpHistogram.h:106
unsigned int getTotal()
Get the total number of pixels in the input image.
Definition: vpHistogram.h:308
unsigned operator()(const unsigned char level) const
Definition: vpHistogram.h:166
unsigned getSize() const
Definition: vpHistogram.h:278
unsigned operator[](const unsigned char level) const
Definition: vpHistogram.h:136
void set(const unsigned char level, unsigned int value)
Definition: vpHistogram.h:224
unsigned * getValues()
Definition: vpHistogram.h:301
void setMask(const vpImage< bool > *p_mask)
Set a mask to ignore pixels for which the mask is false.
Definition: vpHistogram.h:246
unsigned get(const unsigned char level) const
Definition: vpHistogram.h:196