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 <algorithm>
00034
00035 namespace
00036 cmtk
00037 {
00038
00041
00042 template<class T>
00043 void
00044 Histogram<T>
00045 ::AddWeightedSymmetricKernel
00046 ( const size_t bin, const size_t kernelRadius, const T* kernel, const T factor )
00047 {
00048 this->m_Bins[bin] += factor * kernel[0];
00049 for ( size_t idx = 1; idx < kernelRadius; ++idx )
00050 {
00051 const T increment = factor * kernel[idx];
00052 if ( (bin + idx) < this->GetNumBins() )
00053 this->m_Bins[bin + idx] += increment;
00054 if ( bin >= idx )
00055 this->m_Bins[bin - idx] += increment;
00056 }
00057 }
00058
00059 template<class T>
00060 void
00061 Histogram<T>
00062 ::AddWeightedSymmetricKernelFractional
00063 ( const double bin, const size_t kernelRadius, const T* kernel, const T factor )
00064 {
00065 const T relative = static_cast<T>( bin - floor(bin) );
00066 const size_t binIdx = static_cast<size_t>( bin );
00067
00068 if ( (binIdx > 0) && (binIdx+1 < this->GetNumBins()) )
00069 {
00070 this->m_Bins[binIdx] += (1-relative) * factor * kernel[0];
00071 this->m_Bins[binIdx+1] += relative * factor * kernel[0];
00072 }
00073
00074 for ( size_t idx = 1; idx < kernelRadius; ++idx )
00075 {
00076 const T increment = factor * kernel[idx];
00077
00078 const size_t upIdx = binIdx+idx+1;
00079 if ( upIdx < this->GetNumBins() )
00080 {
00081 this->m_Bins[upIdx-1] += (1-relative) * increment;
00082 this->m_Bins[upIdx ] += relative * increment;
00083 }
00084
00085 const int dnIdx = binIdx-idx;
00086 if ( dnIdx >= 0 )
00087 {
00088 this->m_Bins[dnIdx ] += (1-relative) * increment;
00089 this->m_Bins[dnIdx+1] += relative * increment;
00090 }
00091 }
00092 }
00093
00094 template<class T>
00095 double
00096 Histogram<T>
00097 ::GetKullbackLeiblerDivergence( const Self& other ) const
00098 {
00099 assert( this->GetNumBins() == other.GetNumBins() );
00100
00101 const T sampleCount = this->SampleCount();
00102 const T sampleCountOther = other.SampleCount();
00103
00104 double dKL = 0;
00105 for ( size_t i=0; i<this->GetNumBins(); ++i )
00106 {
00107 if ( this->m_Bins[i] )
00108 {
00109 const double pX = ((double)this->m_Bins[i]) / sampleCount;
00110 const double qX = ((double)other.m_Bins[i]) / sampleCountOther;
00111 dKL += pX*log(pX/qX);
00112 }
00113 }
00114 return dKL;
00115 }
00116
00117 }