cmtkHistogram.txx

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: 2732 $
00026 //
00027 //  $LastChangedDate: 2011-01-13 16:36:24 -0800 (Thu, 13 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
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 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines