/vol/vipdata/irtk/image++/include/irtkHistogram_1D.h

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkHistogram_1D.h 2 2008-12-23 12:40:14Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2008-12-23 12:40:14 +0000 (Tue, 23 Dec 2008) $
00008   Version   : $Revision: 2 $
00009   Changes   : $Author: dr $
00010 
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKHISTOGRAM_1D_H
00014 
00015 #define _IRTKHISTOGRAM_1D_H
00016 
00022 class irtkHistogram_1D : public irtkObject
00023 {
00024 
00026   int _nbins;
00027 
00029   int _nsamp;
00030 
00032   double _min;
00033 
00035   double _max;
00036 
00038   double _width;
00039 
00041   int *_bins;
00042 
00043 public:
00044 
00046   irtkHistogram_1D(const irtkHistogram_1D &);
00047 
00049   irtkHistogram_1D(int nbins = 256);
00050 
00052   irtkHistogram_1D(double min, double max, double width);
00053 
00055   irtkHistogram_1D(char *);
00056 
00058   ~irtkHistogram_1D(void);
00059 
00061   void Reset();
00062 
00064   int  GetNumberOfBins() const;
00065 
00067   void PutNumberOfBins(int);
00068 
00070   double GetMin() const;
00071 
00073   void   PutMin(double);
00074 
00076   double GetMax() const;
00077 
00079   void   PutMax(double);
00080 
00082   double GetWidth() const;
00083 
00085   void   PutWidth(double);
00086 
00088   int NumberOfSamples() const;
00089 
00091   int operator()(int) const;
00092 
00094   void Add(int, int = 1);
00095 
00097   void Delete(int, int = 1);
00098 
00100   void AddSample(double);
00101 
00103   void DelSample(double);
00104 
00106   int    ValToBin(double val);
00107 
00109   double BinToVal(int    bin);
00110 
00112   double BinToPDF(int bin);
00113 
00115   double ValToPDF(double val);
00116 
00118   double BinToCDF(int bin);
00119 
00121   double ValToCDF(double val);
00122 
00124   double CDFToBin(double p);
00125 
00127   double CDFToVal(double p);
00128 
00130   double Mean();
00131 
00133   double Variance();
00134 
00136   double StandardDeviation();
00137 
00139   double Entropy();
00140 
00142   void Read (char *filename);
00143 
00145   void Write(char *filename);
00146 
00148   void Print();
00149 };
00150 
00151 inline int irtkHistogram_1D::GetNumberOfBins() const
00152 {
00153   return _nbins;
00154 }
00155 
00156 inline double irtkHistogram_1D::GetMin() const
00157 {
00158   return _min;
00159 }
00160 
00161 inline double irtkHistogram_1D::GetMax() const
00162 {
00163   return _max;
00164 }
00165 
00166 inline double irtkHistogram_1D::GetWidth() const
00167 {
00168   return _width;
00169 }
00170 
00171 inline int irtkHistogram_1D::NumberOfSamples() const
00172 {
00173   return _nsamp;
00174 }
00175 
00176 inline int irtkHistogram_1D::operator()(int i) const
00177 {
00178 #ifndef NO_BOUNDS
00179   if ((i < 0) || (i >= _nbins)) {
00180     cerr << "irtkHistogram_1D::operator(): No such bin" << endl;
00181     exit(1);
00182   }
00183 #endif
00184   return _bins[i];
00185 }
00186 
00187 inline void irtkHistogram_1D::Add(int i, int n)
00188 {
00189 #ifndef NO_BOUNDS
00190   if ((i < 0) || (i >= _nbins)) {
00191     cerr << "irtkHistogram_1D::Add: No such bin" << endl;
00192     exit(1);
00193   }
00194 #endif
00195   _bins[i] += n;
00196   _nsamp   += n;
00197 }
00198 
00199 inline void irtkHistogram_1D::Delete(int i, int n)
00200 {
00201 #ifndef NO_BOUNDS
00202   if ((i < 0) || (i >= _nbins)) {
00203     cerr << "irtkHistogram_1D::Delete: No such bin" << endl;
00204     exit(1);
00205   }
00206 #endif
00207   _bins[i] -= n;
00208   _nsamp   -= n;
00209 }
00210 
00211 inline void irtkHistogram_1D::AddSample(double x)
00212 {
00213   int index;
00214 
00215   if (x < _min) return;
00216   if (x > _max) return;
00217   index = round(_nbins * (x - _min - 0.5*_width) / (_max - _min));
00218   if (index < 0) index = 0;
00219   if (index > _nbins-1) index = _nbins - 1;
00220   _bins[index]++;
00221   _nsamp++;
00222 }
00223 
00224 inline void irtkHistogram_1D::DelSample(double x)
00225 {
00226   int index;
00227 
00228   if (x < _min) return;
00229   if (x > _max) return;
00230   index = round(_nbins * (x - _min - 0.5*_width) / (_max - _min));
00231   if (index < 0) index = 0;
00232   if (index > _nbins-1) index = _nbins - 1;
00233   _bins[index]--;
00234   _nsamp--;
00235 }
00236 
00237 inline int irtkHistogram_1D::ValToBin(double val)
00238 {
00239   int index;
00240 
00241 #ifndef NO_BOUNDS
00242   if ((val < _min) || (val > _max)) {
00243     cerr << "irtkHistogram_1D::ValToBin: Must be between " << _min << " and "
00244          << _max << endl;
00245     exit(1);
00246   }
00247 #endif
00248   index = round(_nbins * (val - _min - 0.5*_width) / (_max - _min));
00249   if (index < 0) index = 0;
00250   if (index > _nbins-1) index = _nbins - 1;
00251   return index;
00252 }
00253 
00254 inline double irtkHistogram_1D::BinToVal(int i)
00255 {
00256 #ifndef NO_BOUNDS
00257   if ((i < 0) || (i > _nbins)) {
00258     cerr << "irtkHistogram_1D::BinToVal: Must be between 0 and " << _nbins << endl;
00259     exit(1);
00260   }
00261 #endif
00262   return (i*(_max - _min)/_nbins + _min) + _width/2.0;
00263 }
00264 
00265 inline double irtkHistogram_1D::BinToPDF(int i)
00266 {
00267 #ifndef NO_BOUNDS
00268   if ((i < 0) || (i >= _nbins)) {
00269     cerr << "irtkHistogram_1D::BinToPDF: No such bin" << endl;
00270     exit(1);
00271   }
00272 #endif
00273   if (_nsamp == 0) return 0;
00274   return _bins[i] / (double)_nsamp;
00275 }
00276 
00277 #endif