contrib/brl/bseg/brip/brip_histogram.txx
Go to the documentation of this file.
00001 // This is brl/bseg/brip/brip_histogram.txx
00002 #ifndef brip_histogram_txx_
00003 #define brip_histogram_txx_
00004 //:
00005 // \file
00006 // \brief Construct histogram from pixels in given image.
00007 // \author Matt Leotta
00008 
00009 #include "brip_histogram.h"
00010 
00011 #include <vcl_cassert.h>
00012 #include <vcl_algorithm.h>
00013 
00014 //: Construct histogram from pixels in the given image.
00015 template<class T>
00016 double brip_histogram(const vil_image_view<T>& image,
00017                       vcl_vector<double>& histo,
00018                       double min, double max, unsigned n_bins)
00019 {
00020   histo.resize(n_bins);
00021   vcl_fill(histo.begin(),histo.end(),0.0);
00022   double x0 = double(min);
00023   double s = double(n_bins-1)/(double(max)-x0);
00024   double total_weight = 0.0;
00025 
00026   unsigned ni=image.ni(), nj=image.nj(), np=image.nplanes();
00027   vcl_ptrdiff_t istep=image.istep(), jstep=image.jstep(), pstep=image.planestep();
00028 
00029   const T* plane = image.top_left_ptr();
00030   for (unsigned p=0; p<np; ++p,plane+=pstep)
00031   {
00032     const T* row = plane;
00033     for (unsigned j=0; j<nj; ++j,row+=jstep)
00034     {
00035       const T* pixel = row;
00036       for (unsigned i=0; i<ni; ++i,pixel+=istep)
00037       {
00038         int index = int(0.5+s*(double(*pixel) - x0));
00039         if (index>=0 && (unsigned)index<n_bins){
00040           histo[index]+= 1.0;
00041           total_weight += 1.0;
00042         }
00043       }
00044     }
00045   }
00046   return total_weight;
00047 }
00048 
00049 
00050 //: Construct weighted histogram from pixels in the given image using values in an image of weights.
00051 template<class T>
00052 double brip_weighted_histogram(const vil_image_view<T>& image,
00053                                const vil_image_view<double>& weights,
00054                                vcl_vector<double>& histo,
00055                                double min, double max, unsigned n_bins)
00056 {
00057   assert( (image.ni() == weights.ni()) &&
00058           (image.nj() == weights.nj()) &&
00059           (image.nplanes() == weights.nplanes()) );
00060 
00061   histo.resize(n_bins);
00062   vcl_fill(histo.begin(),histo.end(),0.0);
00063   double x0 = double(min);
00064   double s = double(n_bins-1)/(double(max)-x0);
00065   double total_weight = 0.0;
00066 
00067   unsigned ni=image.ni(), nj=image.nj(), np=image.nplanes();
00068   vcl_ptrdiff_t i_istep=image.istep(), i_jstep=image.jstep(), i_pstep=image.planestep();
00069   vcl_ptrdiff_t w_istep=weights.istep(), w_jstep=weights.jstep(), w_pstep=weights.planestep();
00070 
00071   const T* i_plane = image.top_left_ptr();
00072   const double* w_plane = weights.top_left_ptr();
00073   for (unsigned p=0; p<np; ++p,i_plane+=i_pstep,w_plane+=w_pstep)
00074   {
00075     const T* i_row = i_plane;
00076     const double* w_row = w_plane;
00077     for (unsigned j=0; j<nj; ++j,i_row+=i_jstep,w_row+=w_jstep)
00078     {
00079       const T* i_pixel = i_row;
00080       const double* w_pixel = w_row;
00081       for (unsigned i=0; i<ni; ++i,i_pixel+=i_istep,w_pixel+=w_istep)
00082       {
00083         int index = int(0.5+s*(double(*i_pixel) - x0));
00084         if (index>=0 && (unsigned)index<n_bins){
00085           histo[index]+= *w_pixel;
00086           total_weight += *w_pixel;
00087         }
00088       }
00089     }
00090   }
00091   return total_weight;
00092 }
00093 
00094 
00095 //: Construct the joint histogram between image1 and image2
00096 template<class T>
00097 double brip_joint_histogram(const vil_image_view<T>& image1,
00098                             const vil_image_view<T>& image2,
00099                             vcl_vector<vcl_vector<double> >& histo,
00100                             double min, double max, unsigned n_bins)
00101 {
00102   assert( (image1.ni() == image2.ni()) &&
00103           (image1.nj() == image2.nj()) &&
00104           (image1.nplanes() == image2.nplanes()) );
00105 
00106   vcl_vector<double> empty(n_bins, 0.0);
00107   histo.clear();
00108   for (unsigned i=0; i<n_bins; ++i)
00109     histo.push_back(empty);
00110 
00111   double x0 = double(min);
00112   double s = double(n_bins-1)/(double(max)-x0);
00113   double total_weight = 0.0;
00114 
00115   unsigned ni=image1.ni(), nj=image1.nj(), np=image1.nplanes();
00116   vcl_ptrdiff_t istep1=image1.istep(), jstep1=image1.jstep(), pstep1=image1.planestep();
00117   vcl_ptrdiff_t istep2=image2.istep(), jstep2=image2.jstep(), pstep2=image2.planestep();
00118 
00119   const T* plane1 = image1.top_left_ptr();
00120   const T* plane2 = image2.top_left_ptr();
00121   for (unsigned p=0; p<np; ++p,plane1+=pstep1,plane2+=pstep2)
00122   {
00123     const T* row1 = plane1;
00124     const T* row2 = plane2;
00125     for (unsigned j=0; j<nj; ++j,row1+=jstep1,row2+=jstep2)
00126     {
00127       const T* pixel1 = row1;
00128       const T* pixel2 = row2;
00129       for (unsigned i=0; i<ni; ++i,pixel1+=istep1,pixel2+=istep2)
00130       {
00131         int index1 = int(0.5+s*(double(*pixel1) - x0));
00132         int index2 = int(0.5+s*(double(*pixel2) - x0));
00133         if (index1>=0 && (unsigned)index1<n_bins &&
00134             index2>=0 && (unsigned)index2<n_bins){
00135           histo[index1][index2] += 1;
00136           total_weight += 1;
00137         }
00138       }
00139     }
00140   }
00141   return total_weight;
00142 }
00143 
00144 
00145 // Macro to perform manual instantiations
00146 #define BRIP_HISTOGRAM_INSTANTIATE(T) \
00147   template \
00148   double brip_histogram(const vil_image_view<T >& image, \
00149                         vcl_vector<double>& histo, \
00150                         double min, double max, unsigned n_bins); \
00151   template \
00152   double brip_weighted_histogram(const vil_image_view<T >& image, \
00153                                  const vil_image_view<double>& weights, \
00154                                  vcl_vector<double>& histo, \
00155                                  double min, double max, unsigned n_bins); \
00156   template \
00157   double brip_joint_histogram(const vil_image_view<T >& image1, \
00158                               const vil_image_view<T >& image2, \
00159                               vcl_vector<vcl_vector<double> >& histo, \
00160                               double min, double max, unsigned n_bins)
00161 
00162 #endif // brip_histogram_txx_