The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

histogram_hue.h

Go to the documentation of this file.
00001 #ifndef __HISTOGRAM_HUE__
00002 #define __HISTOGRAM_HUE__
00003 
00004 #include "preprocessor.h"
00005 
00006 #include "Images/color.h"
00007 
00009 
00010 template<int BINSIZE> class histogram_hue {
00011 private:
00012         double m_hist[BINSIZE]; 
00013         int m_totalMemSize;
00014         int m_numBins;
00015         int m_numColors;
00016         MPColorTools m_color;
00017 
00018         inline double & get_prob(const int & bin) {
00019                 return(m_hist[bin]);
00020         }
00021 
00022         inline void rgb2bin(const RGBTRIPLE & rgb, int & bin) {
00023                 const double alpha = 0.0;       // -0.5 left edge of distribution
00024                 const double binwidth = static_cast<double>(m_numColors)/static_cast<double>(BINSIZE);
00025                 float h = 0.0, s = 0.0, v = 0.0;
00026                 m_color.RGBtoHSV(&rgb, h, s, v);
00027                 bin = (int) (h*m_numBins/360.0);
00028         }
00029 
00030   inline void addtobin(const int & bin, double value = 1.0) {
00031                 m_hist[bin] += value;
00032         }
00033 
00034 public:
00035         histogram_hue() {
00036                 m_numBins = BINSIZE;
00037                 m_totalMemSize = m_numBins * sizeof(double);
00038                 m_numColors = 256;
00039                 clear();
00040         }
00041         
00042         inline double *begin () {
00043                 return (&(m_hist[0]));
00044         }
00045 
00046         inline const double *end () {
00047                 return(&(m_hist[BINSIZE]));
00048         }
00049 
00050         inline double & operator[] (const int & bin) {
00051                 return(m_hist[bin]);
00052         }
00053 
00054   inline void addtobin(const RGBTRIPLE & rgb, const double value = 1.0) {
00055                 int bin;
00056                 rgb2bin(rgb, bin);
00057                 addtobin(bin, value);
00058         }
00059 
00060         inline double & get_prob(const RGBTRIPLE & rgb) {
00061                 int bin;
00062                 rgb2bin(rgb, bin);
00063                 return(get_prob(bin));
00064         }
00065 
00066         void clear () {
00067                 memset(m_hist, 0, m_totalMemSize);
00068         }
00069 
00070         histogram_hue & copy(histogram_hue & hist) {
00071                 memcpy(hist.m_hist, m_hist, m_totalMemSize);
00072                 return(hist);
00073         }
00074 
00075         void normalize() {
00076                 double s = sum();
00077                 for (double *it = begin(); it != end(); it++) {
00078                         if (*it)
00079                                 *it /= s;
00080                 }
00081         }
00082 
00083         double sum() {
00084                 double s = 0.0;
00085                 for (double *it = begin(); it != end(); it++)
00086                         s += *it;
00087                 return (s);
00088         }
00089 
00090         void add_uniform (double weight = 1.0) {
00091                 const double uniform = weight*(1.0/static_cast<double>(m_numBins));
00092                 for (double *it = begin(); it != end(); it++)
00093                         *it = uniform + *it*(1-weight);
00094         }
00095 
00096         void weighted_add (histogram_hue & hist, const double & weight) {
00097                 for (double *toptr = begin(), *fromptr = hist.begin(); toptr != end(); toptr++, fromptr++)
00098                         *toptr = *toptr*weight + *fromptr*(1-weight);
00099         }
00100 
00101         void print_values (std::ostream &os) {
00102                 int count = 1;
00103                 for (double *it = begin(); it != end(); it++) {
00104                         count++;
00105                         os << *it << endl;
00106                 }
00107                 count;
00108         }
00109 
00110         void load_from_file (ifstream &is) {
00111                 clear();
00112                 for (double *it = begin(); it != end(); it++)
00113                         is >> *it;
00114         }
00115 
00116         void save_to_file (ofstream &os) {
00117                 print_values (os);
00118         }
00119 };
00120 
00122 
00123 #endif __HISTOGRAM_HUE__

Generated on Mon Nov 8 17:07:36 2004 for MPT by  doxygen 1.3.9.1