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

MPHistogramHue.h

Go to the documentation of this file.
00001 /*
00002  *  MPColorTracker.cpp
00003  *  
00004  *
00005  *  Created by Bret Fortenberry 2004.
00006  *  Copyright (c) 2003 Machine Perception Laboratory
00007  *  University of California San Diego.
00008  *  Please read the disclaimer and notes about redistribution
00009  *  at the end of this file.
00010  *
00011  *  Authors: Bret Fortenberry, Josh Susskind
00012  */
00013 #ifndef __MPHISTOGRAMHUE_H__
00014 #define __MPHISTOGRAMHUE_H__
00015 
00016 #include <fstream>
00017 #include <iostream>
00018 #include "common.h"
00019 #include "mixgauss.h"
00020 
00021 /* ================================================================ */
00022 
00023 template <typename T, unsigned int N>
00024 class MPHistogramHue {
00025                 
00026 private:
00027         int m_numColors;
00028   float m_frac;
00029         T m_hist[N];
00030         T m_initialHist[N];
00031 
00032         inline T *begin () {
00033                 return (&(m_hist[0]));
00034         }
00035 
00036         inline const T *end () const {
00037                 return(&(m_hist[N]));
00038         }
00039 
00040         T sum() {
00041                 T s = 0.0;
00042                 for (T *it = begin(); it != end(); it++) s += *it;
00043                 return s;
00044         }
00045 
00046         inline float getHue(const RGBTRIPLE & rgb) {
00047                 float   delta,h;
00048                 unsigned char r = rgb.rgbtRed;
00049                 unsigned char g = rgb.rgbtGreen;
00050                 unsigned char b = rgb.rgbtBlue;
00051                 unsigned char cmax = 0, cmin = 0;
00052                 
00053                 if (r >= g) {cmax = r; cmin = g;}
00054                 else {cmax = g; cmin = r;}
00055                 if (b > cmax) cmax = b; 
00056                 else if (b < cmin) cmin = b;
00057                 delta   = static_cast<float>(cmax - cmin);
00058                 if (cmax == r) h = (g-b)/delta;
00059                 else if (cmax == g) h = 2 + (b-r)/delta;
00060                 else h = 4 + (r-g)/delta;       
00061                 h = (h < 0) ? static_cast<float>(h*60.0 + 360.0) : static_cast<float>(h*60.0);
00062                 
00063                 return h;
00064         }
00065 
00066 public:
00067    /* Default constructor */
00068         MPHistogramHue<T,N>() {
00069                 m_numColors = 256;
00070                 m_frac = static_cast<float>(N/360.0f);
00071                 create_initial_hist();
00072                 initialize();
00073         };
00074 
00075         inline T & get_prob(const RGBTRIPLE & rgb) {
00076                 float h;                
00077                 
00078                 h = getHue(rgb);
00079                 return m_hist[static_cast<int>(h*m_frac)];
00080         }
00081 
00082         inline void addtobin(const RGBTRIPLE & rgb, const T value = 1.0) {
00083                 float h;                
00084                 
00085                 h = getHue(rgb);
00086                 m_hist[static_cast<int>(h*m_frac)] += value;
00087         }
00088 
00089         void normalize() {
00090                 T oneoversum = 1.0/sum();
00091                 for (T *it = begin(); it != end(); it++) {
00092                         if (*it) *it *= oneoversum;
00093                 }
00094         }
00095 
00096         void clear() {
00097                 memset(m_histCur, 0, sizeof(T)*N);
00098         }
00099 
00101         void initialize() {
00102                 memcpy(m_hist, m_initialHist, sizeof(T)*N);
00103 //              clear();
00104 //              add_uniform();
00105 //              normalize();
00106         }
00108 
00109         void add_uniform (T weight = 1.0) {
00110                 const T uniform = weight*(1.0/static_cast<T>(N));
00111                 for (T *it = begin(); it != end(); it++) *it = uniform + *it*(1-weight);
00112         }
00113 
00115         void weighted_add (MPHistogramHue & cur, float weight = 0.5f) {
00116                 T *toptr, *fromptr;
00117                 fromptr = cur.begin();
00118                 for (toptr = begin(); toptr != end(); fromptr++, toptr++)
00119                         *toptr = static_cast<T>(*fromptr*weight + *toptr*(1-weight));
00120         }
00121 
00123         void update_hist (MPHistogramHue & _cur, MPHistogramHue & _log, float weight = 0.5f) {
00124                 T *toptr, *fromptr, *logptr;
00125                 T oneoversum = 1.0/_cur.sum();
00126                 fromptr = _cur.begin();
00127                 logptr = _log.begin(); 
00128                 for (toptr = begin(); toptr != end(); fromptr++, toptr++, logptr++) {
00129                         *fromptr *= oneoversum;
00130                         *toptr = static_cast<T>(*fromptr*weight + *toptr*(1-weight));
00131                         *logptr = log(*toptr);
00132                         //cout << "cur = " << *fromptr << ", to = " << *toptr << ", log = " << *logptr << endl;
00133                 }
00134         }
00136 
00137         void print_values (std::ostream &os) {
00138                 for (T *it = begin(); it != end(); it++) os << *it << std::endl;
00139         }
00140 
00141         void load_from_file (std::ifstream &is) {
00142                 initialize();
00143                 for (T *it = begin(); it != end(); it++) is >> *it;
00144         }
00145 
00146         void save_to_file (std::ostream &os) {
00147                 print_values (os);
00148         }
00149 
00151         void create_initial_hist() {
00152                 const T uniform = 1.0/static_cast<T>(N);
00153                 for (int i = 0; i < N; i++)
00154                         m_initialHist[i] = uniform;
00155         }
00157         
00158 //      void InitColorModel()
00159 //      {
00160 //              int i;
00161 //              double dh = 360.0/N;
00162 //              double h= dh/2.0;
00163 //              double frac;
00164 //              double meanHue = (double) 17.9520;  // mean parameter for flesh color model
00165 //              double SDHue = (double) 12.16429201;   //  standard deviation parameter for flesh color model
00166 //              for (i = 0; i < N; i++) {
00167 //                      if (h > meanHue)
00168 //                              frac = h - meanHue;
00169 //                      else
00170 //                              frac = meanHue - h;
00171 //                      if (frac > 180.0) 
00172 //                              frac = 360.0 - frac;
00173 //                      frac = (frac/SDHue);
00174 //                      m_hist[i] = static_cast<T>(exp(-(.5*(frac*frac))));
00175 //                      h+=dh;
00176 //              }       
00177 //              normalize();
00178 //      }
00179         
00181         /* this is the initial value for background and foreground of rgb
00182            it takes a while to compute and might not be ideal for hue
00183         */
00184         void InitColorModel(int face)
00185         {
00186     int r, g, b;
00187 //              cout << "initializing model face = " << face << endl;
00188     skinMixGauss smg;
00189     nonSkinMixGauss nsmg;
00190     for (r = 0; r < 256; r+=10) {
00191       for (g = 0; g < 256; g+=10) {
00192         for (b = 0; b < 256; b+=10) {           
00193           RGBTRIPLE rgbt;
00194                                         rgbt.rgbtRed = (BYTE)r;
00195                                         rgbt.rgbtGreen = (BYTE)g;
00196                                         rgbt.rgbtBlue = (BYTE)b;
00197                                         double prob;
00198                                         if (face)
00199                 prob = smg.getMixProb(rgbt);
00200                                         else
00201                 prob = nsmg.getMixProb(rgbt);
00202                                         addtobin(rgbt, static_cast<T>(prob));
00203                                 }   
00204       }
00205     }
00206 
00207                 normalize();
00208         }
00209 
00210 };
00211  
00212 
00213 /* ================================================================ */
00214 
00215 #endif // __MPHISTOGRAMHUE_H__
00216 
00217 
00218 /*
00219  * 
00220  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00221  * 
00222  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00223  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00224  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00225  * 
00226  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00227  * 
00228  */

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