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

MPHistogram.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 __MPHISTOGRAM_H__
00014 #define __MPHISTOGRAM_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 MPHistogram {
00025 private:
00026         int m_numBins;
00027         int m_numColors;
00028         T m_hist[N][N][N];
00029         T m_initialHist[N][N][N];
00030 
00031         inline T *begin () {
00032                 return (&(m_hist[0][0][0]));
00033         }
00034 
00035         inline const T *end () const {
00036                 return(&(m_hist[N-1][N-1][N-1])+1);
00037         }
00038 
00039         inline int val2bin(const unsigned int &val) {
00040 //              static const float alpha = 0.0; /* -0.5 left edge of distribution */
00041                 static const float binwidth = static_cast<double>(m_numColors)/static_cast<double>(N);
00042                 static const float oneoverbinwidth = 1.0/binwidth;
00043                 return(static_cast<int>(static_cast<T>(val)*oneoverbinwidth));
00044         }
00045 
00046         T sum() {
00047                 T s = 0.0;
00048                 for (T *it = begin(); it != end(); it++) s += *it;
00049                 return s;
00050         }
00051 
00052 
00053 public:
00054    /* Default constructor */
00055         MPHistogram<T,N>() {
00056                 m_numColors = 256;
00057                 m_numBins = N*N*N;
00058                 create_initial_hist();
00059                 initialize();
00060         };
00061 
00062         inline T & get_prob(const RGBTRIPLE & rgb) {
00063                 return m_hist[val2bin(rgb.rgbtRed)][val2bin(rgb.rgbtGreen)][val2bin(rgb.rgbtBlue)];
00064         }
00065 
00066         inline void addtobin(const RGBTRIPLE & rgb, const T value = 1.0) {
00067                 m_hist[val2bin(rgb.rgbtRed)][val2bin(rgb.rgbtGreen)][val2bin(rgb.rgbtBlue)] += value;
00068                 //cout << "after added to bin it = " << m_hist[val2bin(rgb.rgbtRed)][val2bin(rgb.rgbtGreen)][val2bin(rgb.rgbtBlue)] << endl;
00069         }
00070 
00071         void normalize() {
00072                 T oneoversum = 1.0/sum();
00073                 for (T *it = begin(); it != end(); it++) {
00074                         if (*it) *it *= oneoversum;
00075                 }
00076         }
00077 
00078         void clear() {
00079                 memset(m_hist, 0, sizeof(T)*m_numBins);
00080         }
00081 
00083         void initialize() {
00084                 memcpy(m_hist, m_initialHist, sizeof(T)*m_numBins);
00085 //              clear();
00086 //              add_uniform();
00087 //              normalize();
00088         }
00090 
00091         void add_uniform (T weight = 1.0) {
00092                 const T uniform = weight*(1.0/static_cast<T>(m_numBins));
00093                 for (T *it = begin(); it != end(); it++) *it = uniform + *it*(1-weight);
00094         }
00095 
00097         void weighted_add (MPHistogram & _cur, float weight = 0.5f) {
00098                 T *toptr, *fromptr;
00099                 for (toptr = begin(), fromptr = _cur.begin(); toptr != end(); toptr++, fromptr++)
00100                         *toptr = *fromptr*weight + *toptr*(1-weight);
00101         }
00103 
00104                 void update_hist (MPHistogram & _cur, MPHistogram & _log, float weight = 0.5f) {
00105                 T *toptr, *fromptr, *logptr;
00106                 T oneoversum = 1.0/_cur.sum();
00107                 fromptr = _cur.begin();
00108                 logptr = _log.begin(); 
00109                 for (toptr = begin(); toptr != end(); fromptr++, toptr++, logptr++) {
00110                         *fromptr *= oneoversum;
00111                         *toptr = static_cast<T>(*fromptr*weight + *toptr*(1-weight));
00112                         *logptr = log(*toptr);
00113                         //cout << "cur = " << *fromptr << ", to = " << *toptr << ", log = " << *logptr << endl;
00114                 }
00115         }
00117 
00118         void print_values (std::ostream &os) {
00119 //              static const float binwidth = static_cast<double>(m_numColors)/static_cast<double>(N);
00120 //              for (int r = 0; r < N; r++) {
00121 //                      for (int g = 0; g < N; g++) {
00122 //                              for (int b = 0; b < N; b++) {
00123 //                                      os << r*binwidth << "\t" << g*binwidth << "\t" << b*binwidth << "\t" << m_hist[r][g][b] << std::endl;
00124 //                              }
00125 //                      }
00126 //              }
00127                 for (T *it = begin(); it != end(); it++) os << *it << std::endl;
00128         }
00129 
00130         void load_from_file (std::ifstream &is) {
00131                 initialize();
00132                 for (T *it = begin(); it != end(); it++) is >> *it;
00133         }
00134 
00135         void save_to_file (std::ostream &os) {
00136                 print_values (os);
00137         }
00138 
00140         void create_initial_hist() {
00141                 const T uniform = 1.0/static_cast<T>(m_numBins);
00142                 T *it = &m_initialHist[0][0][0];
00143                 for (int i = 0; i < m_numBins; i++, it++) 
00144                         *it = uniform;
00145         }
00147 
00148         void InitColorModel(int face=1)
00149         {
00150     int r, g, b;
00151                 int binWidth = (int)(256/N);
00152    
00153 //              cout << "initializing model face = " << face << endl;
00154     skinMixGauss smg;
00155     nonSkinMixGauss nsmg;
00156     for (r = binWidth/2; r < 256; r+=binWidth) {
00157       for (g = binWidth/2; g < 256; g+=binWidth) {
00158         for (b = binWidth/2; b < 256; b+=binWidth) {           
00159           RGBTRIPLE rgbt;
00160                                         rgbt.rgbtRed = (BYTE)r;
00161                                         rgbt.rgbtGreen = (BYTE)g;
00162                                         rgbt.rgbtBlue = (BYTE)b;
00163                                         double prob;
00164                                         if (face)
00165                 prob = smg.getMixProb(rgbt);
00166                                         else
00167                 prob = nsmg.getMixProb(rgbt);
00168                                         addtobin(rgbt, static_cast<T>(prob));
00169                                 } 
00170       }
00171     }
00172 
00173                 normalize();
00174         }
00175 
00176 };
00177 
00178 /* ================================================================ */
00179 
00180 #endif //__MPHISTOGRAM_H__
00181 
00182 
00183 /*
00184  * 
00185  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00186  * 
00187  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00188  *    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.
00189  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00190  * 
00191  * 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.
00192  * 
00193  */

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