Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
vpHistogram.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 vpHistogram_h
42 #define vpHistogram_h
43 
44 #include <sstream>
45 
46 #include <visp3/core/vpColor.h>
47 #include <visp3/core/vpHistogramPeak.h>
48 #include <visp3/core/vpHistogramValey.h>
49 #include <visp3/core/vpImage.h>
50 
51 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
52 #include <visp3/core/vpList.h>
53 #endif
54 
55 #include <list>
56 
107 class VISP_EXPORT vpHistogram
108 {
109 public:
110  vpHistogram();
111  vpHistogram(const vpHistogram &h);
112  explicit vpHistogram(const vpImage<unsigned char> &I);
113  virtual ~vpHistogram();
114 
115  vpHistogram &operator=(const vpHistogram &h);
116 
137  inline unsigned operator[](const unsigned char level) const
138  {
139  if (level < size) {
140  return histogram[level];
141  }
142 
143  std::stringstream ss;
144  ss << "Level is > to size (" << size << ") !";
145  throw vpException(vpException::dimensionError, ss.str().c_str());
146  };
167  inline unsigned operator()(const unsigned char level) const
168  {
169  if (level < size) {
170  return histogram[level];
171  }
172 
173  std::stringstream ss;
174  ss << "Level is > to size (" << size << ") !";
175  throw vpException(vpException::dimensionError, ss.str().c_str());
176  };
197  inline unsigned get(const unsigned char level) const
198  {
199  if (level < size) {
200  return histogram[level];
201  }
202 
203  std::stringstream ss;
204  ss << "Level is > to size (" << size << ") !";
205  throw vpException(vpException::dimensionError, ss.str().c_str());
206  };
207 
225  inline void set(const unsigned char level, unsigned int value)
226  {
227  if (level < size) {
228  histogram[level] = value;
229  } else {
230  std::stringstream ss;
231  ss << "Level is > to size (" << size << ") !";
232  throw vpException(vpException::dimensionError, ss.str().c_str());
233  }
234  };
235 
236  void calculate(const vpImage<unsigned char> &I, unsigned int nbins = 256, unsigned int nbThreads = 1);
237 
238  void display(const vpImage<unsigned char> &I, const vpColor &color = vpColor::white, unsigned int thickness = 2,
239  unsigned int maxValue_ = 0);
240 
241  void smooth(unsigned int fsize = 3);
242  unsigned getPeaks(std::list<vpHistogramPeak> &peaks);
243  unsigned getPeaks(unsigned char dist, vpHistogramPeak &peak1, vpHistogramPeak &peak2);
244  bool getPeaks(unsigned char dist, vpHistogramPeak &peakl, vpHistogramPeak &peakr, vpHistogramValey &valey);
245  unsigned getValey(std::list<vpHistogramValey> &valey);
246  bool getValey(const vpHistogramPeak &peak1, const vpHistogramPeak &peak2, vpHistogramValey &valey);
247  unsigned getValey(unsigned char dist, const vpHistogramPeak &peak, vpHistogramValey &valeyl,
248  vpHistogramValey &valeyr);
249  unsigned sort(std::list<vpHistogramPeak> &peaks);
250 
251  bool write(const std::string &filename);
252  bool write(const char *filename);
253 
262  inline unsigned getSize() const { return size; };
263 
285  inline unsigned *getValues() { return histogram; };
286 
287 private:
288  void init(unsigned size = 256);
289 
290  unsigned int *histogram;
291  unsigned size; // Histogram size (max allowed 256)
292 };
293 
294 #endif
295 
296 /*
297  * Local variables:
298  * c-basic-offset: 2
299  * End:
300  */
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor white
Definition: vpColor.h:206
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
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:108
unsigned operator()(const unsigned char level) const
Definition: vpHistogram.h:167
unsigned getSize() const
Definition: vpHistogram.h:262
unsigned operator[](const unsigned char level) const
Definition: vpHistogram.h:137
void set(const unsigned char level, unsigned int value)
Definition: vpHistogram.h:225
unsigned * getValues()
Definition: vpHistogram.h:285
unsigned get(const unsigned char level) const
Definition: vpHistogram.h:197