File: colorhistogram.h

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (99 lines) | stat: -rw-r--r-- 2,639 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef COLORHISTOGRAM_H
#define COLORHISTOGRAM_H
#include <vcg/math/histogram.h>
namespace vcg
{
/**
 * ColorHistogram.
 *
 * This class implements a single-value histogram.
 */
template <class ScalarType>
    class ColorHistogram : public Histogram<ScalarType>
{

// public data members
private:

  std::vector <Color4f> CV; 	//! Counters for color averages.

// public methods
public:
  /**
   * Add a new value to the histogram.
   *
   * The statistics related to the histogram data (average, RMS, etc.) are
   * also updated.
   */
  void Add(ScalarType v,Color4b c,float increment=1.0);

  Color4b BinColorAvg(ScalarType v) { return BinColorAvgInd(this->BinIndex(v)); }

  Color4b BinColorAvgInd(int index) {
    return Color4b( (unsigned char)((CV[index][0] / float(this->H[index]))),
                    (unsigned char)((CV[index][1] / float(this->H[index]))),
                    (unsigned char)((CV[index][2] / float(this->H[index]))),255);
                 }

    //ScalarType RangeCount(ScalarType rangeMin, ScalarType rangeMax);
  //ScalarType BinWidth(ScalarType v);

  //! Reset histogram data.
  void Clear() {
    Histogram<ScalarType>::Clear();
    CV.clear();
  }
  /*
  Note that the histogram holds <n> valid bins plus two semi-infinite bins.

  R[0]   = -inf
  R[1]   = minv
  R[n+1] = maxv
  R[n+2] = +inf


  Eg. SetRange(0, 10, 5) asks for 5 intervals covering the 0..10 range

      H[0]  H[1]   H[2]   H[3]   H[4]   H[5]    H[6]
  -inf    0      2      4      6      8      10    +inf
  R[0]   R[1]   R[2]   R[3]   R[4]   R[5]   R[6]   R[7]

  */
  void SetRange(ScalarType _minv, ScalarType _maxv, int _n, ScalarType gamma=1.0)
  {
    Histogram<ScalarType>::SetRange(_minv,_maxv,_n,gamma);
    CV.resize(_n+2);
    fill(CV.begin(),CV.end(),Color4f(0,0,0,0));
  }
};




/*
    H[0]  H[1]   H[2]   H[3]   H[4]   H[5]    H[6]
-inf    0      2      4      6      8      10    +inf
R[0]   R[1]   R[2]   R[3]   R[4]   R[5]   R[6]   R[7]

asking for 3.14 lower bound will return an iterator pointing to R[3]==4; and will increase H[2]
asking for 4    lower bound will return an iterator pointing to R[3]==4; and will increase H[2]

*/
template <class ScalarType>
void ColorHistogram<ScalarType>::Add(ScalarType v,Color4b c,float increment/*=1.0f*/)
{
  int pos=this->BinIndex(v);
  if(v<this->minElem) this->minElem=v;
  if(v>this->maxElem) this->maxElem=v;
  if(pos>=0 && pos<=this->n)
  {
    CV[pos]+=Color4f(c[0],c[1],c[2],255.0)*increment;
    this->H[pos]+=increment;
    this->cnt+=increment;
    this->avg+=v*increment;
    this->rms += (v*v)*increment;
  }
}

} // end namespace
#endif // COLORHISTOGRAM_H