Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef __cmtkHistogram_h_included_
00034 #define __cmtkHistogram_h_included_
00035
00036 #include <cmtkconfig.h>
00037
00038 #include <Base/cmtkHistogramBase.h>
00039
00040 #include <System/cmtkSmartPtr.h>
00041 #include <System/cmtkMemory.h>
00042
00043 #include <vector>
00044 #include <algorithm>
00045 #include <cassert>
00046
00047 namespace
00048 cmtk
00049 {
00050
00053
00063 template<class T>
00064 class Histogram :
00066 public HistogramBase
00067 {
00068 public:
00070 typedef Histogram<T> Self;
00071
00073 typedef HistogramBase Superclass;
00074
00076 typedef SmartPointer<Self> SmartPtr;
00077
00079 typedef T BinType;
00080
00083 Histogram ( const size_t numBins = 0 ) : m_Bins( numBins ) {}
00084
00087 virtual ~Histogram () {}
00088
00090 virtual void Resize( const size_t numberOfBins, const bool reset = true )
00091 {
00092 this->m_Bins.resize( numberOfBins );
00093
00094 if ( reset )
00095 this->Reset();
00096 }
00097
00099 typename Self::SmartPtr Clone () const
00100 {
00101 return typename Self::SmartPtr( this->CloneVirtual() );
00102 }
00103
00105 virtual size_t GetNumBins() const
00106 {
00107 return this->m_Bins.size();
00108 }
00109
00116 void Reset ()
00117 {
00118 std::fill( this->m_Bins.begin(), this->m_Bins.end(), 0 );
00119 }
00120
00122 const T operator[] ( const size_t index ) const
00123 {
00124 assert( index < this->GetNumBins() );
00125 return this->m_Bins[index];
00126 }
00127
00129 T& operator[] ( const size_t index )
00130 {
00131 assert( index < this->GetNumBins() );
00132 return this->m_Bins[index];
00133 }
00134
00137 T SampleCount () const
00138 {
00139 T sampleCount = 0;
00140
00141 for ( size_t i=0; i<this->m_Bins.size(); ++i )
00142 sampleCount += this->m_Bins[i];
00143
00144 return sampleCount;
00145 }
00146
00149 size_t GetMaximumBinIndex () const;
00150
00153 T GetMaximumBinValue () const
00154 {
00155 return this->m_Bins[ this->GetMaximumBinIndex() ];
00156 }
00157
00162 double GetEntropy() const;
00163
00170 double GetKullbackLeiblerDivergence( const Self& other ) const;
00171
00177 void Increment ( const size_t sample )
00178 {
00179 ++this->m_Bins[sample];
00180 }
00181
00184 void AddWeightedSymmetricKernel( const size_t bin , const size_t kernelRadius , const T* kernel , const T factor = 1 );
00185
00188 void AddWeightedSymmetricKernelFractional( const double bin , const size_t kernelRadius , const T* kernel , const T factor = 1 );
00189
00199 void IncrementFractional ( const double bin )
00200 {
00201 const T relative = static_cast<T>( bin - floor(bin) );
00202 this->m_Bins[static_cast<size_t>(bin)] += (1 - relative);
00203 if ( bin<(this->GetNumBins()-1) )
00204 this->m_Bins[static_cast<size_t>(bin+1)] += relative;
00205 }
00206
00214 void Increment ( const size_t sample, const double weight )
00215 {
00216 this->m_Bins[sample] += static_cast<T>( weight );
00217 }
00218
00226 void Decrement ( const size_t sample )
00227 {
00228 assert( this->m_Bins[sample] >= 1 );
00229 --this->m_Bins[sample];
00230 }
00231
00241 void DecrementFractional ( const double bin )
00242 {
00243 T relative = static_cast<T>( bin - floor(bin) );
00244 this->m_Bins[static_cast<size_t>(bin)] -= (1 - relative);
00245 if ( bin<(this->GetNumBins()-1) )
00246 this->m_Bins[static_cast<size_t>(bin+1)] -= relative;
00247 }
00248
00258 void Decrement ( const size_t sample, const double weight )
00259 {
00260 assert( this->m_Bins[sample] >= weight );
00261 this->m_Bins[sample] -= static_cast<T>( weight );
00262 }
00263
00273 void AddHistogram ( const Self& other );
00274
00284 void RemoveHistogram ( const Self& other );
00285
00286
00288 void ConvertToCumulative()
00289 {
00290 for ( size_t idx = 1; idx < this->GetNumBins(); ++idx )
00291 {
00292 this->m_Bins[idx] += this->m_Bins[idx-1];
00293 }
00294 }
00299 void Normalize( const T normalizeTo = 1 );
00300
00305 void NormalizeMaximum( const T normalizeTo = 1 );
00306
00309 Types::DataItem GetPercentile( const Types::DataItem percentile ) const;
00310
00311 protected:
00313 virtual Self* CloneVirtual() const
00314 {
00315 return new Self( *this );
00316 }
00317
00318 private:
00320 std::vector<T> m_Bins;
00321 };
00322
00324
00325 }
00326
00327 #include "cmtkHistogram.txx"
00328
00329 #endif // #ifndef __cmtkHistogram_h_included_