cmtkHistogram.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
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 /* Pointer to kernel values */, const T factor = 1  );
00185   
00188   void AddWeightedSymmetricKernelFractional( const double bin , const size_t kernelRadius , const T* kernel /* Pointer to kernel values */, 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 } // namespace cmtk
00326 
00327 #include "cmtkHistogram.txx"
00328 
00329 #endif // #ifndef __cmtkHistogram_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines