vil_histogram.h
Go to the documentation of this file.
1 #ifndef vil_histogram_h_
2 #define vil_histogram_h_
3 //:
4 // \file
5 // \brief Construct histogram from pixels in given image.
6 // \author Tim Cootes
7 
8 #include <vector>
9 #include <algorithm>
10 #include <vil/vil_image_view.h>
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 #include <vxl_config.h>
15 
16 //: Construct histogram from pixels in given image
17 // \relatesalso vil_image_view
18 template<class T>
19 inline
20 void vil_histogram(const vil_image_view<T>& image,
21  std::vector<double>& histo,
22  double min, double max, unsigned n_bins)
23 {
24  histo.resize(n_bins);
25  std::fill(histo.begin(),histo.end(),0.0);
26  double x0 = double(min);
27  double s = double(n_bins-1)/(double(max)-x0);
28 
29  unsigned ni = image.ni(),nj = image.nj(),np = image.nplanes();
30  std::ptrdiff_t istep=image.istep(),jstep=image.jstep(),pstep = image.planestep();
31  const T* plane = image.top_left_ptr();
32  for (unsigned p=0;p<np;++p,plane += pstep)
33  {
34  const T* row = plane;
35  for (unsigned j=0;j<nj;++j,row += jstep)
36  {
37  const T* pixel = row;
38  for (unsigned i=0;i<ni;++i,pixel+=istep)
39  {
40  int index = int(0.5+s*(double(*pixel) - x0));
41  if (index>=0 && (unsigned)index<n_bins) histo[index]+=1;
42  }
43  }
44  }
45 }
46 
47 //: Construct histogram from pixels in given image of bytes
48 // Resulting histogram has 256 bins
49 // \relatesalso vil_image_view
51  std::vector<double>& histo);
52 
53 //: Instantiation macro for other types
54 #define VIL_HISTOGRAM_INSTANTIATE(T) \
55 template void vil_histogram(const vil_image_view<T>& image, \
56  std::vector<double>& histo, \
57  double min, double max, unsigned n_bins)
58 
59 #endif // vil_histogram_h_
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
void vil_histogram(const vil_image_view< T > &image, std::vector< double > &histo, double min, double max, unsigned n_bins)
Construct histogram from pixels in given image.
Definition: vil_histogram.h:20
std::ptrdiff_t planestep() const
Add this to your pixel pointer to get pixel on next plane.
A base class reference-counting view of some image data.
vnl_decnum max(vnl_decnum const &x, vnl_decnum const &y)
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
void vil_histogram_byte(const vil_image_view< vxl_byte > &image, std::vector< double > &histo)
Construct histogram from pixels in given image of bytes.
vnl_decnum min(vnl_decnum const &x, vnl_decnum const &y)
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.