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 
00014 #define LN_2 0.69314718056
00015 
00016 
00017 //: Calculate the Mutual Information between the images.
00018 template<class T>
00019 double brip_mutual_info(const vil_image_view<T>& image1,
00020                         const vil_image_view<T>& image2,
00021                         double min, double max, unsigned n_bins);
00022 
00023 
00024 //: Calculate the entropy of a histogram
00025 inline
00026 double brip_hist_entropy(const vcl_vector<double>& histogram, double mag)
00027 {
00028   double entropy = 0.0;
00029   for ( vcl_vector<double>::const_iterator h_itr = histogram.begin();
00030         h_itr != histogram.end(); ++h_itr ){
00031     double prob = (*h_itr)/mag;
00032     entropy += -(prob?prob*vcl_log(prob):0); // if prob=0 this value is defined as 0
00033   }
00034   return entropy/LN_2; // divide by ln(2) to convert this measure to base 2
00035 }
00036 
00037 
00038 //: Calculate the entropy of a joint histogram
00039 inline
00040 double brip_hist_entropy(const vcl_vector<vcl_vector<double> >& histogram, double mag)
00041 {
00042   double entropy = 0.0;
00043   for ( vcl_vector<vcl_vector<double> >::const_iterator h_itr = histogram.begin();
00044         h_itr != histogram.end(); ++h_itr ){
00045     entropy += brip_hist_entropy(*h_itr,mag);
00046   }
00047   return entropy;
00048 }
00049 
00050 #endif // brip_mutual_info_h_