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

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkDensity.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 _IRTKDENSITY_H
00014 
00015 #define _IRTKDENSITY_H
00016 
00022 class irtkDensity : public irtkObject
00023 {
00024 
00026   int _nbins;
00027 
00029   double _norm;
00030 
00032   double _min;
00033 
00035   double _max;
00036 
00038   double _width;
00039 
00041   double *_bins;
00042 
00043 public:
00044 
00046 //  irtkHistogram_1D(const irtkHistogram_1D &);
00047 
00049   irtkDensity(int nbins = 256);
00050 
00052   irtkDensity(double min, double max, double width);
00053 
00055   //irtkHistogram_1D(char *);
00056 
00058   ~irtkDensity(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   double Norm() const;
00089 
00091   double operator()(int) const;
00092 
00094   void Add(int, double = 1);
00095 
00097   void Delete(int, double = 1);
00098 
00100   void AddSample(double, double = 1);
00101 
00103   void DelSample(double, double = 1);
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   void WriteToImage(char *image_name);
00151 };
00152 
00153 inline int irtkDensity::GetNumberOfBins() const
00154 {
00155   return _nbins;
00156 }
00157 
00158 inline double irtkDensity::GetMin() const
00159 {
00160   return _min;
00161 }
00162 
00163 inline double irtkDensity::GetMax() const
00164 {
00165   return _max;
00166 }
00167 
00168 inline double irtkDensity::GetWidth() const
00169 {
00170   return _width;
00171 }
00172 
00173 inline double irtkDensity::Norm() const
00174 {
00175   return _norm;
00176 }
00177 
00178 inline double irtkDensity::operator()(int i) const
00179 {
00180 #ifndef NO_BOUNDS
00181   if ((i < 0) || (i >= _nbins)) {
00182     cerr << "irtkDensity::operator(): No such bin" << endl;
00183     exit(1);
00184   }
00185 #endif
00186   return _bins[i];
00187 }
00188 
00189 inline void irtkDensity::Add(int i, double value)
00190 {
00191 #ifndef NO_BOUNDS
00192   if ((i < 0) || (i >= _nbins)) {
00193     cerr << "irtkDensity::Add: No such bin" << endl;
00194     exit(1);
00195   }
00196 #endif
00197   _bins[i] += value;
00198   _norm   += value;
00199 }
00200 
00201 inline void irtkDensity::Delete(int i, double value)
00202 {
00203 #ifndef NO_BOUNDS
00204   if ((i < 0) || (i >= _nbins)) {
00205     cerr << "irtkDensity::Delete: No such bin" << endl;
00206     exit(1);
00207   }
00208 #endif
00209   _bins[i] -= value;
00210   _norm   -= value;
00211 
00212 }
00213 
00214 inline void irtkDensity::AddSample(double x, double value)
00215 {
00216   if (x < _min) return;
00217   if (x > _max) return;
00218   double bin = (x - _min) / (_max - _min) *_nbins -0.5;
00219   /*double bin = (x - _min) / (_max - _min) *(_nbins-1) ;*/
00220   int index1 = (int)floor(bin);
00221   int index2 = (int)ceil(bin);
00222   if (index2 == 0) _bins[0]+=value;
00223   if (index1 == _nbins-1) _bins[_nbins-1]+=value;
00224   if ((index1>=0)&&(index2<=_nbins-1)) {
00225     if (index1 ==index2) _bins[index1]+=value;
00226     else {
00227       _bins[index1]+=(index2-bin)*value;
00228       _bins[index2]+=(bin-index1)*value;
00229     }
00230   }
00231   _norm += value;
00232 }
00233 
00234 inline void irtkDensity::DelSample(double x, double value)
00235 {
00236   int index;
00237 
00238   if (x < _min) return;
00239   if (x > _max) return;
00240   index = ValToBin(x);
00241   _bins[index] -= value;
00242   _norm -= value;
00243 }
00244 
00245 inline int irtkDensity::ValToBin(double val)
00246 {
00247   int index;
00248 
00249 #ifndef NO_BOUNDS
00250   if ((val < _min) || (val > _max)) {
00251     cerr << "irtkDensity::ValToBin: Must be between " << _min << " and "
00252          << _max << endl;
00253     exit(1);
00254   }
00255 #endif
00256   index = round((val - _min) / (_max - _min) * _nbins -0.5);
00257   if (index < 0) index = 0;
00258   if (index > _nbins-1) index = _nbins - 1;
00259   return index;
00260 }
00261 
00262 inline double irtkDensity::BinToVal(int i)
00263 {
00264 #ifndef NO_BOUNDS
00265   if ((i < 0) || (i >= _nbins)) {
00266     cerr << "irtkDensity::BinToVal: Must be between 0 and " << _nbins << endl;
00267     exit(1);
00268   }
00269 #endif
00270   return (_min + (i + 0.5)*(_max - _min)/_nbins);
00271 }
00272 
00273 inline double irtkDensity::BinToPDF(int i)
00274 {
00275 #ifndef NO_BOUNDS
00276   if ((i < 0) || (i >= _nbins)) {
00277     cerr << "irtkDensity::BinToPDF: No such bin" << endl;
00278     exit(1);
00279   }
00280 #endif
00281   if (_norm == 0) return 0;
00282   return _bins[i] / _norm;
00283 }
00284 
00285 inline double irtkDensity::ValToPDF(double value)
00286 {
00287   int i = ValToBin(value);
00288   if (_norm == 0) return 0;
00289   return _bins[i] / _norm;
00290 }
00291 
00292 #endif
00293