libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNHistogram.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, INSA-Lyon, CoReNum, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: SHistogram.h
19  * \author Yann LEYDIER, Jean DUONG, Asma OUJI
20  */
21 
22 #ifndef CRNHISTOGRAM_HEADER
23 #define CRNHISTOGRAM_HEADER
24 
25 #include <CRNString.h>
28 #include <cmath>
29 
30 namespace crn
31 {
32  /****************************************************************************/
44  class Histogram: public Object
45  {
46  public:
48 
50  Histogram():bins(1, 0),compression(1) { }
52  Histogram(size_t s, unsigned int v = 0);
54  Histogram(const Histogram &src, unsigned int c = 1u);
55 
56  /*****************************************************************************/
67  template<typename ITER> Histogram(ITER it_begin, ITER it_end, DesignHeuristic m = DesignHeuristic::STURGES, size_t nb_bins = (size_t)1)
68  {
69  double delta;
70  double left_bound = std::numeric_limits<double>::infinity();
71  double right_bound = -std::numeric_limits<double>::infinity();
72 
73  size_t cardinal = 0;
74 
75  // First step : check data
76 
77  for (auto it = it_begin; it != it_end; ++it)
78  {
79  double v = it->first;
80  size_t c = it->second;
81 
82  if (v < left_bound)
83  left_bound = v;
84 
85  if (v > right_bound)
86  right_bound = v;
87 
88  cardinal += c;
89  }
90 
91  double range = right_bound - left_bound;
92 
93  // Second step : build histogram
94 
96  {
97  nb_bins = (size_t) sqrt(double(cardinal));
98  delta = range / double(nb_bins);
99  }
100  else if (m == DesignHeuristic::STURGES)
101  {
102  nb_bins = 1 + (size_t) (log(double(cardinal)) / log(2.0));
103  delta = range / double(nb_bins);
104  }
105  else if (m == DesignHeuristic::SCOTT)
106  {
107  double m = 0.0;
108  double v = 0.0;
109 
110  for (auto it = it_begin; it != it_end; ++it)
111  m += it->first * ((double)it->second / (double)cardinal);
112 
113  for (auto it = it_begin; it != it_end; ++it)
114  {
115  double diff = it->first - m;
116 
117  v += (diff * diff) * ((double)it->second / (double)cardinal);
118  }
119 
120  delta = 3.5 * sqrt(v) / pow(double(cardinal), (1.0 / 3.0));
121  nb_bins = 1 + (size_t) ((right_bound - left_bound) / delta);
122  }
123  else
124  {
125  delta = range / double(nb_bins);
126  }
127 
128  ++nb_bins; // Add one safety bin !
129 
130  bins = std::vector<unsigned int>(nb_bins + 1, 0u);
131  compression = 1;
132 
133  // Third step : fill histogram
134 
135  if (nb_bins == 1) // trivial case
136  SetBin(0, (unsigned int)cardinal);
137  else
138  {
139  for (auto it = it_begin; it != it_end; ++it)
140  {
141  size_t id = (size_t)((it->first - left_bound) / delta);
142 
143  if (id < nb_bins)
144  IncBin(id, it->second);
145  }
146  }
147  }
148 
149  Histogram& operator=(const Histogram&) = delete;
150  Histogram(Histogram&&) = default;
151  Histogram& operator=(Histogram&&) = default;
152 
154  ~Histogram();
155 
157  size_t Size() const noexcept {return bins.size();}
159  unsigned int* GetBins() {return &(bins.front());}
161  const unsigned int* GetBins() const {return &(bins.front());}
162 
164  void SetBin(size_t k, unsigned int v);
165 
167  void IncBin(size_t k, unsigned int i = 1u);
168 
170  unsigned int GetBin(size_t k) const;
171 
173  unsigned int& operator[](size_t k) { return bins[k]; }
175  const unsigned int& operator[](size_t k) const { return bins[k]; }
176 
178  unsigned int CumulateBins() const;
179 
181  double Mean() const;
182 
184  double Variance(double m) const;
186  double Deviation(double m) const {return sqrt(Variance(m));}
187 
189  double Variance() const;
191  double Deviation() const {return sqrt(Variance());}
192 
194  unsigned int Max() const;
195 
197  unsigned int Min() const;
198 
200  size_t Argmax() const;
201 
203  size_t Argmin() const;
204 
206  void SetCeiling(unsigned int m);
207 
209  void ScaleMaxTo(unsigned int m);
210 
212  void AverageSmoothing(size_t d);
214  void CircularAverageSmoothing(size_t d);
215 
217  std::vector<size_t> Modes() const;
219  std::vector<size_t> StableModes() const;
220 
222  Histogram MakeIntersection(const Histogram &h) const;
223 
225  double IntersectionDivergence(const Histogram &h) const;
226 
228  double Correlation(const Histogram &h) const;
229 
231  double Chi2(const Histogram &h) const;
232 
234  double MinkowskiDistance(const Histogram &h, double r) const;
235 
237  double JeffreyDivergence(const Histogram &h) const;
238 
240  double MatchDistance(const Histogram &h) const;
241 
243  double KolmogorovSmirnovDistance(const Histogram &h) const;
244 
246  double EMD(const Histogram &h) const;
247 
249  double CEMD(const Histogram &h) const;
250 
252  void Append(const Histogram &h);
253 
255  void Resize(size_t newsize);
256 
258  String ToString() const;
259 
261  ImageBW MakeImageBW(size_t height) const;
262 
264  ImageBW MakeRadialImageBW(size_t radius) const;
265 
267  void Cumulate();
268 
270  size_t Fisher() const;
271 
273  size_t EntropyThreshold() const;
274 
276  double Entropy() const;
277 
279  size_t MedianValue() const;
280 
283 
284  std::vector<unsigned int> Std() && { return std::move(bins); }
285 
287  void Deserialize(xml::Element &el);
289  xml::Element Serialize(xml::Element &parent) const;
290 
291  private:
292  using datatype = std::vector<unsigned int>;
293  datatype bins;
294  unsigned int compression;
298  };
299  template<> struct IsSerializable<Histogram> : public std::true_type {};
300  template<> struct IsClonable<Histogram> : public std::true_type {};
301  inline double Distance(const Histogram &h1, const Histogram &h2) { return h1.MinkowskiDistance(h2, 1); }
302  template<> struct IsMetric<Histogram> : public std::true_type {};
303 
304 
306  inline size_t Size(const Histogram &h) noexcept { return h.Size(); }
307 }
308 #endif
double Deviation(double m) const
Standard deviation on class indexes, having mean value.
Definition: CRNHistogram.h:186
xml::Element Serialize(xml::Element &parent) const
Dumps the object to an XML element. Unsafe.
double JeffreyDivergence(const Histogram &h) const
Jeffrey divergence between two histograms.
#define CRN_SERIALIZATION_CONSTRUCTOR(classname)
Defines a default constructor from xml element.
Definition: CRNObject.h:165
double Entropy() const
the entropy
double IntersectionDivergence(const Histogram &h) const
Intersection divergence between two histograms.
void Deserialize(xml::Element &el)
Initializes the object from an XML element. Unsafe.
XML element.
Definition: CRNXml.h:135
void CircularAverageSmoothing(size_t d)
Smoothing circular histogram.
double KolmogorovSmirnovDistance(const Histogram &h) const
Kolmogorov-Smirnov distance between two histograms.
void IncBin(size_t k, unsigned int i=1u)
Increments a bin value.
~Histogram()
Destructor.
std::vector< unsigned int > Std()&&
Definition: CRNHistogram.h:284
unsigned int Max() const
Maximal count value.
void SetCeiling(unsigned int m)
Set maximal count value to m.
size_t Argmax() const
First class having a maximal count value.
size_t Argmin() const
First class having a minimal count value.
unsigned int GetBin(size_t k) const
Get a bin value.
ImageBW MakeRadialImageBW(size_t radius) const
returns the circular histogram image
A UTF32 character string class.
Definition: CRNString.h:61
const unsigned int & operator[](size_t k) const
Access a bin value (fast and unsafe)
Definition: CRNHistogram.h:175
double Mean() const
Mean value on class indexes.
Histogram MakeIntersection(const Histogram &h) const
Intersection between two histograms.
void Append(const Histogram &h)
Appends an histogram to the current.
double EMD(const Histogram &h) const
Earth Mover's Distance.
ImageBW MakeImageBW(size_t height) const
returns the histogram image
void SetBin(size_t k, unsigned int v)
Modify a bin value.
double Variance() const
Variance on class indexes.
size_t Size() const noexcept
Returns the number of bins.
Definition: CRNHistogram.h:157
void Cumulate()
Cumulates the values from 0 to end.
double Correlation(const Histogram &h) const
Correlation between two histograms.
double Distance(const Int &i1, const Int &i2) noexcept
Definition: CRNInt.h:78
const unsigned int * GetBins() const
Returns a reference to the internal pointer on the bins.
Definition: CRNHistogram.h:161
Histogram & operator=(const Histogram &)=delete
double Chi2(const Histogram &h) const
Correlation between two histograms.
size_t MedianValue() const
Computes the median value.
std::vector< size_t > Modes() const
Returns the modes.
std::vector< size_t > StableModes() const
Returns the stable modes.
size_t Fisher() const
Computes the Fisher threshold.
double MinkowskiDistance(const Histogram &h, double r) const
Minkowski distance between two histograms.
Mother class for integer histograms.
Definition: CRNHistogram.h:44
Histogram()
Default constructor.
Definition: CRNHistogram.h:50
void AverageSmoothing(size_t d)
Smoothing histogram.
double CEMD(const Histogram &h) const
Circular Earth Mover's Distance.
Histogram MakePopulationHistogram() const
Creates an histogram from population.
unsigned int * GetBins()
Returns the internal pointer on the bins.
Definition: CRNHistogram.h:159
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
unsigned int & operator[](size_t k)
Access a bin value (fast and unsafe)
Definition: CRNHistogram.h:173
size_t EntropyThreshold() const
Computes the entropy threshold.
String ToString() const
Dumps bins to a string.
unsigned int Min() const
Minimal count value.
double Deviation() const
Standard deviation on class indexes.
Definition: CRNHistogram.h:191
unsigned int CumulateBins() const
Cumulate all bins.
void ScaleMaxTo(unsigned int m)
Scale maximal count to m.
Histogram(ITER it_begin, ITER it_end, DesignHeuristic m=DesignHeuristic::STURGES, size_t nb_bins=(size_t) 1)
Definition: CRNHistogram.h:67
double MatchDistance(const Histogram &h) const
Match distance between two histograms.
void Resize(size_t newsize)
Resizes the histogram.