cmtkJointHistogram.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: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkJointHistogram.h"
00034 
00035 #include <Base/cmtkMathUtil.h>
00036 
00037 namespace
00038 cmtk
00039 {
00040 
00043 
00044 template<class T> void 
00045 JointHistogram<T>::GetMarginalEntropies ( double& HX, double& HY ) 
00046   const 
00047 {
00048   const T sampleCount = this->SampleCount();
00049 
00050   HX = HY = 0;
00051   for ( size_t i=0; i<NumBinsX; ++i ) 
00052     {
00053     const double project = this->ProjectToX( i );
00054     if ( project ) 
00055       {
00056       const double pX = project / sampleCount;
00057       HX -= pX * log(pX);
00058       }
00059     }
00060   
00061   for ( size_t j=0; j<NumBinsY; ++j ) 
00062     {
00063     const double project = this->ProjectToY( j );
00064     if ( project ) 
00065       {
00066       const double pY = project / sampleCount;
00067       HY -= pY * log(pY);
00068       }
00069   }
00070 }
00071 
00072 template<class T> double
00073 JointHistogram<T>::GetJointEntropy() const 
00074 {
00075   double HXY = 0;
00076   
00077   const T sampleCount = this->SampleCount();
00078   for ( size_t idx = 0; idx < this->m_TotalNumberOfBins; ++idx )
00079     {
00080     if ( JointBins[idx] ) 
00081       {
00082       const double pXY = ((double)JointBins[idx]) / sampleCount;
00083       HXY -= pXY * log(pXY);
00084       }
00085     }
00086   
00087   return HXY;
00088 }
00089 
00090 template<class T> 
00091 Histogram<T>* 
00092 JointHistogram<T>::GetMarginalX() const 
00093 {
00094   Histogram<T>* marg = new Histogram<T>( NumBinsX );
00095   
00096   marg->SetRange( this->GetRangeX() );
00097   for ( size_t i = 0; i < NumBinsX; ++i )
00098     (*marg)[i] = this->ProjectToX( i );
00099   
00100   return marg;
00101 }
00102 
00103 template<class T> 
00104 Histogram<T>* 
00105 JointHistogram<T>::GetMarginalY() const 
00106 {
00107   Histogram<T>* marg = new Histogram<T>( NumBinsY );
00108   
00109   marg->SetRange( this->GetRangeY() );
00110   for ( size_t i = 0; i < NumBinsY; ++i )
00111     (*marg)[i] = this->ProjectToY( i );
00112   
00113   return marg;
00114 }
00115 
00116 template class JointHistogram<int>;
00117 template class JointHistogram<unsigned int>;
00118 template class JointHistogram<float>;
00119 template class JointHistogram<double>;
00120 
00121 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines