contrib/brl/bseg/brip/brip_mutual_info.h
Go to the documentation of this file.
00001 // This is brl/bseg/brip/brip_mutual_info.h
00002 #ifndef brip_mutual_info_h_
00003 #define brip_mutual_info_h_
00004 //:
00005 // \file
00006 // \brief Calculate the mutual information between images
00007 // \author Matt Leotta
00008 //
00009 
00010 #include <vil/vil_image_view.h>
00011 #include <vcl_vector.h>
00012 #include <vcl_cmath.h>
00013 #include <vnl/vnl_math.h>
00014 
00015 
00016 //: Calculate the Mutual Information between the images.
00017 template<class T>
00018 double brip_mutual_info(const vil_image_view<T>& image1,
00019                         const vil_image_view<T>& image2,
00020                         double min, double max, unsigned n_bins);
00021 
00022 
00023 //: Calculate the entropy of a histogram
00024 inline
00025 double brip_hist_entropy(const vcl_vector<double>& histogram, double mag)
00026 {
00027   double entropy = 0.0;
00028   for ( vcl_vector<double>::const_iterator h_itr = histogram.begin();
00029         h_itr != histogram.end(); ++h_itr ){
00030     double prob = (*h_itr)/mag;
00031     entropy += -(prob?prob*vcl_log(prob):0); // if prob=0 this value is defined as 0
00032   }
00033   return entropy/vnl_math::ln2; // divide by ln(2) to convert this measure to base 2
00034 }
00035 
00036 
00037 //: Calculate the entropy of a joint histogram
00038 inline
00039 double brip_hist_entropy(const vcl_vector<vcl_vector<double> >& histogram, double mag)
00040 {
00041   double entropy = 0.0;
00042   for ( vcl_vector<vcl_vector<double> >::const_iterator h_itr = histogram.begin();
00043         h_itr != histogram.end(); ++h_itr ){
00044     entropy += brip_hist_entropy(*h_itr,mag);
00045   }
00046   return entropy;
00047 }
00048 
00049 #endif // brip_mutual_info_h_