KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
i_color_histogram.h
Go to the documentation of this file.
1 
7 /*
8  * $Id: i_color_histogram.h 17393 2014-08-23 20:19:14Z predoehl $
9  */
10 
11 #ifndef KJB_CPP_COLOR_HISTOGRAM_H
12 #define KJB_CPP_COLOR_HISTOGRAM_H
13 
14 #define DEFAULT_COL_HIST_NUM_BINS 8
15 
16 #include "i_cpp/i_image.h"
17 #include <m_cpp/m_vector.h>
18 #include <l_cpp/l_readable.h>
19 #include <l_cpp/l_writeable.h>
20 
21 #include <numeric>
22 
23 namespace kjb
24 {
25 
40 class Color_histogram : public Readable, public Writeable
41 {
42 public:
43 
45  Color_histogram(unsigned int inum_bins = DEFAULT_COL_HIST_NUM_BINS);
46 
48  Color_histogram(unsigned int inum_bins, const kjb::Image & img);
49 
51  Color_histogram(const char * fname)
52  {
53  Readable::read(fname);
54  }
55 
57  Color_histogram(std::istream &is)
58  {
59  read(is);
60  }
61 
64  (
65  unsigned int inum_bins,
66  const Image & img,
67  int top_left_x,
68  int top_left_y,
69  int bottom_right_x,
70  int bottom_right_y
71  );
72 
74  Color_histogram(const Color_histogram & src);
75 
78 
81  (
82  const kjb::Image & img,
83  unsigned int inum_bins,
84  int top_left_x,
85  int top_left_y,
86  int bottom_right_x,
87  int bottom_right_y
88  );
89 
91  void compute_histogram(const kjb::Image & img, unsigned int inum_bins);
92 
94  void compute_histogram(const kjb::Image & img);
95 
98  (
99  const kjb::Image & img,
100  int top_left_x,
101  int top_left_y,
102  int bottom_right_x,
103  int bottom_right_y
104  );
105 
107  unsigned int get_num_bins() const
108  {
109  return num_bins;
110  }
111 
113  inline double get_value(float r, float g, float b) const
114  {
115  return m_histogram[compute_rgb_bin(r,g,b)];
116  }
117 
118  inline double get_value(int ibin_number) const
119  {
120  return m_histogram[ibin_number];
121  }
122 
123  void print_human_readable(std::ostream & out) const;
124 
125  double compare(const Color_histogram & ch) const;
126 
128  double sum() const
129  {
130  /*
131  double dsum = 0.0;
132  for(int i = 0; i < m_histogram.size(); ++i)
133  {
134  dsum+= m_histogram[i];
135  }
136  return dsum;
137  */
138  return std::accumulate( m_histogram.begin(), m_histogram.end(), 0.0 );
139  }
140 
142  virtual void read(std::istream& in);
143 
145  virtual void read(const char * fname)
146  {
147  Readable::read(fname);
148  }
149 
150 
152  virtual void write(std::ostream& out) const
153  ;
154 
156  virtual void write(const char * fname) const
157  {
158  Writeable::write(fname);
159  }
160 private:
161  Vector m_histogram;
162 
164  unsigned int compute_rgb_bin(float r, float g, float b) const;
165 
167  unsigned int find_bin(float ivalue) const;
168 
169  unsigned int num_bins;
170 
172  float bin_size;
173 };
174 
175 void write_histograms(const std::vector<Color_histogram> & chs, const std::string & file_name);
176 
177 void read_histograms(std::vector<Color_histogram> & chs, const std::string & file_name);
178 
180 
181 }
182 
183 #endif /*KJB_CPP_IMAGE_H */
Abstract class to write this object to an output stream.
Definition: l_writeable.h:41
double get_value(float r, float g, float b) const
Returns the value of the bin corresponding to the input (r,g,b) values.
Definition: i_color_histogram.h:113
#define DEFAULT_COL_HIST_NUM_BINS
Definition: i_color_histogram.h:14
Abstract class to read this object from an input stream.
Definition: l_readable.h:39
double accumulate(const Matrix_d< R, C, T > &mat, double init)
Definition: m_matrix_d.impl.h:432
Color_histogram & operator=(const Color_histogram &src)
Assignment operator.
Definition: i_color_histogram.cpp:86
virtual void read(std::istream &in)
Reads this camera from an input stream.
Definition: i_color_histogram.cpp:94
r
Definition: APPgetLargeConnectedEdges.m:127
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
virtual void write(std::ostream &out) const =0
Writes this Writeable to an output stream.
virtual void write(std::ostream &out) const
Writes this camera to an output stream.
Definition: i_color_histogram.cpp:105
virtual void write(const char *fname) const
Writes this camera to an output stream.
Definition: i_color_histogram.h:156
iterator end()
Definition: m_vector.h:557
iterator begin()
Definition: m_vector.h:537
void write_histograms(const std::vector< Color_histogram > &chs, const std::string &file_name)
Definition: i_color_histogram.cpp:251
Color_histogram(const char *fname)
Constructor. Constructs a color histogram by reading it from a file.
Definition: i_color_histogram.h:51
void read_histograms(std::vector< Color_histogram > &chs, const std::string &file_name)
Definition: i_color_histogram.cpp:267
void print_human_readable(std::ostream &out) const
Definition: i_color_histogram.cpp:213
virtual void read(const char *fname)
Reads this parametric_parapiped from a file.
Definition: i_color_histogram.h:145
Color_histogram(std::istream &is)
Constructor. Constructs a color histogram by reading it from an input stream.
Definition: i_color_histogram.h:57
double compare(const Color_histogram &ch) const
Definition: i_color_histogram.cpp:237
unsigned int get_num_bins() const
Returns the number of bins.
Definition: i_color_histogram.h:107
double sum() const
compute sum of all the bins in the histogram
Definition: i_color_histogram.h:128
virtual void read(std::istream &in)=0
Reads this Readable from an input stream.
Code for a wrapper class around the C struct KJB_Image.
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
Color_histogram(unsigned int inum_bins=DEFAULT_COL_HIST_NUM_BINS)
Constructor. Only memory for color histogram is allocated.
Definition: i_color_histogram.cpp:62
double get_value(int ibin_number) const
Definition: i_color_histogram.h:118
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
void compute_histogram(const kjb::Image &img, unsigned int inum_bins, int top_left_x, int top_left_y, int bottom_right_x, int bottom_right_y)
Computes a color histogram over the specified image region, and using the input number of bins...
Definition: i_color_histogram.cpp:121
Class to compute an RGB colour histogram over an image or a rectangular portion of it...
Definition: i_color_histogram.h:40