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 #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 }