cmtkHistogram.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 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: 2022 $
00026 //
00027 //  $LastChangedDate: 2010-07-21 15:26:03 -0700 (Wed, 21 Jul 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkHistogram.h"
00034 
00035 namespace
00036 cmtk
00037 {
00038 
00041 
00042 template<class T> 
00043 size_t
00044 Histogram<T>
00045 ::GetMaximumBinIndex () const 
00046 {
00047   T maximum = this->m_Bins[0];
00048   size_t maximumIndex = 0;
00049   
00050   for ( size_t i = 0; i<this->GetNumBins(); ++i ) 
00051     {
00052     if ( this->m_Bins[ i ] > maximum ) 
00053       {
00054       maximum = this->m_Bins[ i ];
00055       maximumIndex = i;
00056       }
00057     }
00058   return maximumIndex;
00059 }
00060 
00061 template<class T>
00062 double
00063 Histogram<T>
00064 ::GetEntropy() const 
00065 {
00066   double H = 0;
00067   
00068   const T sampleCount = this->SampleCount();
00069   if ( ! sampleCount ) 
00070     return MathUtil::GetDoubleNaN();
00071   
00072   for ( size_t i=0; i<this->GetNumBins(); ++i ) 
00073     {
00074     if ( this->m_Bins[i] ) 
00075       {
00076       const double pX = ((double)this->m_Bins[i]) / sampleCount;
00077       H -= pX*log(pX);
00078       }
00079     }
00080   return H;
00081 }
00082 
00083 template<class T> 
00084 void
00085 Histogram<T>
00086 ::AddHistogram
00087 ( const Self& other )
00088 {
00089   assert( this->GetNumBins() == other.GetNumBins() );
00090   
00091   for ( size_t i = 0; i<this->GetNumBins(); ++i ) 
00092     {
00093     this->m_Bins[i] += other.m_Bins[i];
00094     }
00095 }
00096 
00097 template<class T> 
00098 void 
00099 Histogram<T>
00100 ::RemoveHistogram
00101 ( const Self& other ) 
00102 {
00103   assert( this->GetNumBins() == other.GetNumBins() );
00104   
00105   for ( size_t i = 0; i<this->GetNumBins(); ++i ) 
00106     {
00107     assert( this->m_Bins[i] >= other.m_Bins[i] );
00108     this->m_Bins[i] -= other.m_Bins[i];
00109     }
00110 }
00111 
00112 template<class T>
00113 void
00114 Histogram<T>
00115 ::Normalize
00116 ( const T normalizeTo ) 
00117 {
00118   T sampleCount = this->SampleCount();
00119   for ( size_t i = 0; i < this->GetNumBins(); ++i )
00120     ( this->m_Bins[ i ] *= normalizeTo ) /= sampleCount;
00121 }
00122 
00123 template<class T>
00124 void
00125 Histogram<T>
00126 ::NormalizeMaximum
00127 ( const T normalizeTo ) 
00128 {
00129   T maximum = this->GetMaximumBinValue();
00130   for ( size_t i = 0; i < this->GetNumBins(); ++i )
00131     ( this->m_Bins[i] *= normalizeTo ) /= maximum;
00132 }
00133 
00134 template<class T>
00135 Types::DataItem 
00136 Histogram<T>
00137 ::GetPercentile( const Types::DataItem percentile ) const
00138 {
00139   const Types::DataItem threshold = percentile * this->SampleCount();
00140   Types::DataItem cumulative = 0;
00141   for ( size_t i = 0; i < this->GetNumBins(); ++i )
00142     {
00143     cumulative += (*this)[i];
00144     if ( cumulative >= threshold )
00145       return this->BinToValue( i );
00146     }
00147 
00148   return this->m_BinsLowerBound + this->m_BinWidth * (this->GetNumBins() - 1);
00149 }
00150 
00151 
00152 template class Histogram<int>;
00153 template class Histogram<unsigned int>;
00154 template class Histogram<long int>;
00155 template class Histogram<float>;
00156 template class Histogram<double>;
00157 
00158 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines