core/vil/algo/vil_histogram.h
Go to the documentation of this file.
00001 #ifndef vil_histogram_h_
00002 #define vil_histogram_h_
00003 //:
00004 // \file
00005 // \brief Construct histogram from pixels in given image.
00006 // \author Tim Cootes
00007 
00008 #include <vil/vil_image_view.h>
00009 #include <vcl_vector.h>
00010 #include <vxl_config.h>
00011 #include <vcl_algorithm.h>
00012 
00013 //: Construct histogram from pixels in given image
00014 //  \relatesalso vil_image_view
00015 template<class T>
00016 inline
00017 void vil_histogram(const vil_image_view<T>& image,
00018                    vcl_vector<double>& histo,
00019                    double min, double max, unsigned n_bins)
00020 {
00021   histo.resize(n_bins);
00022   vcl_fill(histo.begin(),histo.end(),0.0);
00023   double x0 = double(min);
00024   double s = double(n_bins-1)/(double(max)-x0);
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   const T* plane = image.top_left_ptr();
00029   for (unsigned p=0;p<np;++p,plane += pstep)
00030   {
00031     const T* row = plane;
00032     for (unsigned j=0;j<nj;++j,row += jstep)
00033     {
00034       const T* pixel = row;
00035       for (unsigned i=0;i<ni;++i,pixel+=istep)
00036       {
00037         int index = int(0.5+s*(double(*pixel) - x0));
00038         if (index>=0 && (unsigned)index<n_bins) histo[index]+=1;
00039       }
00040     }
00041   }
00042 }
00043 
00044 //: Construct histogram from pixels in given image of bytes
00045 //  Resulting histogram has 256 bins
00046 //  \relatesalso vil_image_view
00047 void vil_histogram_byte(const vil_image_view<vxl_byte>& image,
00048                         vcl_vector<double>& histo);
00049 
00050 //: Instantiation macro for other types
00051 #define VIL_HISTOGRAM_INSTANTIATE(T) \
00052 template void vil_histogram(const vil_image_view<T>& image, \
00053                             vcl_vector<double>& histo, \
00054                             double min, double max, unsigned n_bins)
00055 
00056 #endif // vil_histogram_h_