Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HistogramImage.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __HistogramImage_HH__
27 #define __HistogramImage_HH__
28 
29 #include <Base/Common/BIASpragmaStart.hh>
30 
31 #include <vector>
32 
33 #include <Base/Image/Image.hh>
34 #include <Base/Math/Math.hh>
35 
36 // start from big bits, because small bits are used in Image
37 #define D_HIST_VEC 0x8000000
38 
39 namespace BIAS{
40 
41 
43 
44  /** @class HistogramImage
45  @ingroup g_image
46  @brief Class for easy histogram image generation.
47 
48  Automatically generate and draw some histograms in one image.
49 
50  The histogram image has size bincount*zoomfactor+border by
51  bincount*zoomfactor+border and can display some histograms simultanously.
52  The zoomfactor represents the size of one point in the histogram image in
53  pixel units. It can be set via the constructor HistogramImage().
54  The Border is fixed to 2 pixel.
55 
56  For usage, see ExampleHistogram.cpp or call successivly
57  - InitHist()
58 
59  - AddHist()
60 
61  - Draw()
62 
63  Two styles are possible: point (HIST_POINTS) and bar (HIST_BARS) style.
64  Use SetDrawMode() for choosing a style.
65 
66  Colors can be set for each histogram via SetColor().
67 
68  see also ExampleHistogram.cpp
69 
70  Since HistogramImage is derived from Image<unsigned char>, any drawing
71  function working on Image<unsigned char> can be used on HistogramImage
72  as well.
73 
74  @author woelk */
75 
76  class BIASImage_EXPORT HistogramImage : public Image<unsigned char>
77  {
78  public:
79  ///
80  HistogramImage(unsigned short int zoomfactor = 2);
81  ~HistogramImage();
82 
83  /// resizes Hist_
84  void SetBinCount(unsigned int bincount);
85 
86  inline unsigned int GetBinCount()
87  { return BinCount_; };
88 
89  inline unsigned int GetHistCount()
90  { return HistCount_; };
91 
92  /// sets the zoomfactor
93  /** @todo factor also scales the y axis, this is most probably not desired*/
94  void SetFactor(unsigned short Factor);
95 
96  /// dummy function to avoid swapping color model in this class
97  void SetColorModel();
98 
99  /// Select drawing style. Default is HIST_POINTS
100  void SetDrawMode(HistMode m) {Mode_ = m;};
101 
102  /** Reserves internal data structures for histcount histograms. */
103  void SetHistCount(unsigned int histcount);
104 
105  /** reserves the internal data structures for histcount histograms with
106  bincount bins in each in one image
107  @author woelk */
108  int InitHist(unsigned int bincount = 256, unsigned int histcount = 1);
109 
110  /// releases internal data structures (Hist_)
111  int ReleaseHist();
112 
113  // calculates the Shannon Entropy and treats entries in HistogramImage as
114  // probabilities
115  float CalcShannonEntropy(unsigned int hist=0);
116 
117  // calculates the Renyi Entropy and treats entries in HistogramImage as
118  // probabilities
119  float CalcRenyiEntropy(unsigned int hist=0, double alpha=1.0);
120 
121 
122  /** calculates the histogram of image and adds them to the internal data
123  structures
124  @author woelk */
125  template <class StorageType>
126  int AddHist(const Image<StorageType>& Image, unsigned int hist = 0);
127 
128  /** Calcluates the histogram from data and adds them to the internal data
129  structures. Minimum and maximum data values are determined
130  automatically and BinCount_ bins are calculated evenspaced between min
131  and max.
132  @author woelk */
133  template <class DataType>
134  int AddHist(const std::vector<DataType>& data, unsigned int hist = 0);
135 
136 
137 
138  /* BinCount_ bins are deteremined between min and max
139  @author woelk 07 2004 */
140  template <class DataType>
141  int AddHist(const std::vector<DataType>& data, DataType min, DataType max,
142  unsigned int hist = 0);
143 
144  /** for data[i] increase according bin about weight[i]
145  @author woelk */
146  template <class DataType>
147  int AddWeightedHist(const std::vector<DataType>& data,
148  const std::vector<double>& weight, unsigned int hist = 0);
149 
150  // sets color in which to draw hist num hist
151  int SetColor(unsigned char R, unsigned char G, unsigned char B,
152  unsigned int hist = 0);
153 
154  /// returns the range of the histogram, i.e. the data value associated
155  /// with bin[0] and bin[bincount-1]
156  inline void GetDataRange(double& min, double& max, unsigned int hist = 0)
157  { min=MinBinVal_[hist] ; max=MaxBinVal_[hist]; }
158 
159  /// returns the biggest bin in histogram hist
160  inline void GetMaxHistEntry(double& maxentry, unsigned int hist = 0)
161  { maxentry=MaxHistEntry_[hist]; }
162 
163  /// if true, the upper border of histogram is MaxHistEntry, otherwise the
164  /// upper border of histogram k is \sum_i Hist[k][i] = NumData_[k]
165  inline void SetScaleYToMaxEntry(bool s)
166  { ScaleYToMaxEntry_ = s; }
167 
168  /** actually draws histogram(s) from the internal data structures */
169  int Draw();
170 
171  /** actually draws histogram(s) from the internal data structures
172  uses a logarithmic y axis */
173  int DrawLog();
174 
175  /// zeros existing histogram
176  int ZeroHist(unsigned int hist = 0);
177 
178  /// Get the counted number of bin from hist, -1 for invalid bin/hist
179  double GetBin(unsigned int bin, unsigned int hist=0);
180 
181  /// Set the counted number of bin from hist to manipulate the histogram
182  int SetBin(unsigned int bin, double value, unsigned int hist=0);
183 
184  void Dump(std::ostream& os=std::cout);
185 
186  /// writes ascii data to stream, can be used later e.g. with gnuplot
187  void WriteASCII(std::ostream& os=std::cout);
188 
189  /// opens file and calls WriteASCII() with it
190  int WriteASCII(const std::string& filename);
191 
192  /** transforms data coordinates to image coordinates
193  does automatic clipping */
194  void CooTransf(double datacoo[2], double imcoo[2], unsigned hist=0);
195  /** transforms data coordinates to image coordinates
196  does automatic clipping and rounding */
197  void CooTransf(double datacoo[2], unsigned imcoo[2], unsigned hist=0);
198 
199  /** returns true if MaxHistEntry==0 for all histograms
200  @author woelk 10/2004 */
201  bool IsEmpty();
202 
203  protected:
204  unsigned short int HistCount_;
205  unsigned int BinCount_; // number of bins in histogram
206  double **Hist_; // array containing the histogram
207  unsigned char **Color_; // array containing rgb values for every histogram
208  unsigned int HistSize_; // height of histogram
209  double *MaxHistEntry_; // contains the biggest entry in every histograms
210  double *NumData_; // contains the number of data in each histogram
211  unsigned short int Factor_; // scaling factor for drawing histogram
212  double *Average_; // Average value for each hist
214  unsigned int _uiBorder; // border of the histogram
215  unsigned char _ucBorderColor;
216  double *MinBinVal_, *MaxBinVal_, *BinSize_;
217  // should the upper border be MaxHistEntry, otherwise the
218  // upper border of histogram k is \sum_i Hist[k][i]
220 
221 
222  // only calls DeleteHist_ and DrawHist_
223  int RedrawHist_(unsigned int hist);
224 
225  // removes all image point with color of hist
226  int DeleteHist_(unsigned int hist);
227 
228  // draws histogram
229  int DrawHist_(unsigned int hist);
230 
231  // draws histogram
232  int DrawHistLog_(unsigned int hist);
233 
234  int CalcAverage_(unsigned int hist);
235 
236  }; // class
237 
238  class BIASImage_EXPORT Histogram2D : public Image<unsigned char>
239  {
240  public:
241  Histogram2D(unsigned short zoomfactor = 2);
242  ~Histogram2D();
243 
244  /// dummy function to avoid swapping color model in this class
245  void SetColorModel();
246 
247  /// sets the zoomfactor
248  void SetFactor(unsigned short Factor);
249 
250  void InitHist(unsigned xbincount=256, unsigned ybincount=256);
251 
252  template <class DataType>
253  void AddHist(const std::vector<DataType>& xdata,const std::vector<DataType>& ydata);
254 
255  void Draw();
256 
257  void DrawLog();
258 
259  void ZeroHist();
260 
261  void Dump(std::ostream& os=std::cout);
262 
263  /// writes ascii data to stream, can be used later e.g. with gnuplot
264  void WriteASCII(std::ostream& os=std::cout);
265 
266  /** transforms data coordinates to image coordinates
267  automatic clipping of imcoo is performed */
268  void CooTransf(double datacoo[2], double imcoo[2]);
269  /** transforms data coordinates to image coordinates
270  automatic clipping of imcoo is performed */
271  void CooTransf(double datacoo[2], unsigned imcoo[2]);
272 
273  protected:
274  unsigned XBinCount_, YBinCount_;
275  double **Hist_; // array containing the histogram
276  double MaxHistEntry_; // contains the biggest entry in each histogram
277  double XMinBinVal_, XMaxBinVal_, XBinSize_, YMinBinVal_, YMaxBinVal_,
278  YBinSize_;
279  unsigned int _uiBorder; // border of the histogram
280  unsigned char _ucBorderColor;
281  unsigned short int Factor_; // scaling factor for drawing histogram
282 
283  void _ReleaseHist();
284 
285  }; // class
286 
287 } // namespace BIAS
288 
289 #include <Base/Common/BIASpragmaEnd.hh>
290 
291 #endif // __HistogramImage_HH__
unsigned int GetHistCount()
unsigned char _ucBorderColor
void SetScaleYToMaxEntry(bool s)
if true, the upper border of histogram is MaxHistEntry, otherwise the upper border of histogram k is ...
unsigned int _uiBorder
unsigned short int Factor_
unsigned short int Factor_
The image template class for specific storage types.
Definition: Image.hh:78
Class for easy histogram image generation.
unsigned char ** Color_
void GetMaxHistEntry(double &maxentry, unsigned int hist=0)
returns the biggest bin in histogram hist
void GetDataRange(double &min, double &max, unsigned int hist=0)
returns the range of the histogram, i.e.
void SetDrawMode(HistMode m)
Select drawing style. Default is HIST_POINTS.
unsigned char _ucBorderColor
unsigned short int HistCount_
unsigned int GetBinCount()