contrib/mul/vil3d/algo/vil3d_histogram.h
Go to the documentation of this file.
00001 #ifndef vil3d_histogram_h_
00002 #define vil3d_histogram_h_
00003 //:
00004 // \file
00005 // \brief Construct histogram from pixels in given image.
00006 // \author Tim Cootes
00007 
00008 #include <vcl_vector.h>
00009 #include <vxl_config.h>
00010 #include <vcl_algorithm.h>
00011 #include <vil3d/vil3d_image_view.h>
00012 
00013 //: Construct histogram from pixels in given image
00014 template<class T>
00015 inline
00016 void vil3d_histogram(const vil3d_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 
00025   const T* plane = image.origin_ptr();
00026   unsigned ni = image.ni(),nj = image.nj(),nk = image.nk(),np = image.nplanes();
00027   vcl_ptrdiff_t istep=image.istep(),jstep=image.jstep(),kstep=image.kstep(),pstep = image.planestep();
00028   for (unsigned p=0;p<np;++p,plane += pstep)
00029   {
00030     const T* slice = plane;
00031     for (unsigned k=0;k<nk;++k,slice += kstep)
00032     {
00033       const T* row = slice;
00034       for (unsigned j=0;j<nj;++j,row += jstep)
00035       {
00036         const T* pixel = row;
00037         for (unsigned i=0;i<ni;++i,pixel+=istep)
00038         {
00039           int index = int(0.5+s*(double(*pixel) - x0));
00040           if (index>=0 && (unsigned)index<n_bins) histo[index]+=1;
00041         }
00042       }
00043     }
00044   }
00045 }
00046 
00047 //: Construct histogram from pixels in given image of bytes
00048 //  Resulting histogram has 256 bins
00049 void vil3d_histogram_byte(const vil3d_image_view<vxl_byte>& image,
00050                           vcl_vector<double>& histo);
00051 
00052 #endif // vil3d_histogram_h_