Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
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  explicit vpHistogram(const vpImage<unsigned char> &I, const vpImage<bool> *p_mask);
114  virtual ~vpHistogram();
115 
116  vpHistogram &operator=(const vpHistogram &h);
117 
138  inline unsigned operator[](const unsigned char level) const
139  {
140  if (level < m_size) {
141  return m_histogram[level];
142  }
143 
144  std::stringstream ss;
145  ss << "Level is > to size (" << m_size << ") !";
146  throw vpException(vpException::dimensionError, ss.str().c_str());
147  };
168  inline unsigned operator()(const unsigned char level) const
169  {
170  if (level < m_size) {
171  return m_histogram[level];
172  }
173 
174  std::stringstream ss;
175  ss << "Level is > to size (" << m_size << ") !";
176  throw vpException(vpException::dimensionError, ss.str().c_str());
177  };
198  inline unsigned get(const unsigned char level) const
199  {
200  if (level < m_size) {
201  return m_histogram[level];
202  }
203 
204  std::stringstream ss;
205  ss << "Level is > to size (" << m_size << ") !";
206  throw vpException(vpException::dimensionError, ss.str().c_str());
207  };
208 
226  inline void set(const unsigned char level, unsigned int value)
227  {
228  if (level < m_size) {
229  m_histogram[level] = value;
230  }
231  else {
232  std::stringstream ss;
233  ss << "Level is > to size (" << m_size << ") !";
234  throw vpException(vpException::dimensionError, ss.str().c_str());
235  }
236  };
237 
248  inline void setMask(const vpImage<bool> *p_mask)
249  {
250  mp_mask = p_mask;
251  }
252 
253  void calculate(const vpImage<unsigned char> &I, unsigned int nbins = 256, unsigned int nbThreads = 1);
254  void equalize(const vpImage<unsigned char> &I, vpImage<unsigned char> &Iout);
255 
256  void display(const vpImage<unsigned char> &I, const vpColor &color = vpColor::white, unsigned int thickness = 2,
257  unsigned int maxValue_ = 0);
258 
259  void smooth(unsigned int fsize = 3);
260  unsigned getPeaks(std::list<vpHistogramPeak> &peaks);
261  unsigned getPeaks(unsigned char dist, vpHistogramPeak &peak1, vpHistogramPeak &peak2);
262  bool getPeaks(unsigned char dist, vpHistogramPeak &peakl, vpHistogramPeak &peakr, vpHistogramValey &valey);
263  unsigned getValey(std::list<vpHistogramValey> &valey);
264  bool getValey(const vpHistogramPeak &peak1, const vpHistogramPeak &peak2, vpHistogramValey &valey);
265  unsigned getValey(unsigned char dist, const vpHistogramPeak &peak, vpHistogramValey &valeyl,
266  vpHistogramValey &valeyr);
267  unsigned sort(std::list<vpHistogramPeak> &peaks);
268 
269  bool write(const std::string &filename);
270  bool write(const char *filename);
271 
280  inline unsigned getSize() const { return m_size; };
281 
303  inline unsigned *getValues() { return m_histogram; };
304 
310  inline unsigned int getTotal() { return m_total; };
311 
312 private:
313  void init(unsigned size = 256);
314 
315  unsigned int *m_histogram;
316  unsigned m_size;
317  const vpImage<bool> *mp_mask;
318  unsigned int m_total;
319 };
320 
321 #endif
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 int getTotal()
Get the total number of pixels in the input image.
Definition: vpHistogram.h:310
unsigned operator()(const unsigned char level) const
Definition: vpHistogram.h:168
unsigned getSize() const
Definition: vpHistogram.h:280
unsigned operator[](const unsigned char level) const
Definition: vpHistogram.h:138
void set(const unsigned char level, unsigned int value)
Definition: vpHistogram.h:226
unsigned * getValues()
Definition: vpHistogram.h:303
void setMask(const vpImage< bool > *p_mask)
Set a mask to ignore pixels for which the mask is false.
Definition: vpHistogram.h:248
unsigned get(const unsigned char level) const
Definition: vpHistogram.h:198