www.pudn.com > colortracker.rar > MPHistogram.h
/* * MPColorTracker.cpp * * * Created by Bret Fortenberry 2004. * Copyright (c) 2003 Machine Perception Laboratory * University of California San Diego. * Please read the disclaimer and notes about redistribution * at the end of this file. * * Authors: Bret Fortenberry, Josh Susskind */ #ifndef __MPHISTOGRAM_H__ #define __MPHISTOGRAM_H__ #include#include #include "common.h" #include "mixgauss.h" /* ================================================================ */ template class MPHistogram { private: int m_numBins; int m_numColors; T m_hist[N][N][N]; T m_initialHist[N][N][N]; inline T *begin () { return (&(m_hist[0][0][0])); } inline const T *end () const { return(&(m_hist[N-1][N-1][N-1])+1); } inline int val2bin(const unsigned int &val) { // static const float alpha = 0.0; /* -0.5 left edge of distribution */ static const float binwidth = static_cast (m_numColors)/static_cast (N); static const float oneoverbinwidth = 1.0/binwidth; return(static_cast (static_cast (val)*oneoverbinwidth)); } T sum() { T s = 0.0; for (T *it = begin(); it != end(); it++) s += *it; return s; } public: /* Default constructor */ MPHistogram () { m_numColors = 256; m_numBins = N*N*N; create_initial_hist(); initialize(); }; inline T & get_prob(const RGBTRIPLE & rgb) { return m_hist[val2bin(rgb.rgbtRed)][val2bin(rgb.rgbtGreen)][val2bin(rgb.rgbtBlue)]; } inline void addtobin(const RGBTRIPLE & rgb, const T value = 1.0) { m_hist[val2bin(rgb.rgbtRed)][val2bin(rgb.rgbtGreen)][val2bin(rgb.rgbtBlue)] += value; //cout << "after added to bin it = " << m_hist[val2bin(rgb.rgbtRed)][val2bin(rgb.rgbtGreen)][val2bin(rgb.rgbtBlue)] << endl; } void normalize() { T oneoversum = 1.0/sum(); for (T *it = begin(); it != end(); it++) { if (*it) *it *= oneoversum; } } void clear() { memset(m_hist, 0, sizeof(T)*m_numBins); } //////////// modified 5/17 //////////////////////////// void initialize() { memcpy(m_hist, m_initialHist, sizeof(T)*m_numBins); // clear(); // add_uniform(); // normalize(); } /////////////////////////////////////////////////////// void add_uniform (T weight = 1.0) { const T uniform = weight*(1.0/static_cast (m_numBins)); for (T *it = begin(); it != end(); it++) *it = uniform + *it*(1-weight); } ////////////// fully implemented 5/17 ////////////////////// void weighted_add (MPHistogram & _cur, float weight = 0.5f) { T *toptr, *fromptr; for (toptr = begin(), fromptr = _cur.begin(); toptr != end(); toptr++, fromptr++) *toptr = *fromptr*weight + *toptr*(1-weight); } //////////////////////////////////////////////////////////// void update_hist (MPHistogram & _cur, MPHistogram & _log, float weight = 0.5f) { T *toptr, *fromptr, *logptr; T oneoversum = 1.0/_cur.sum(); fromptr = _cur.begin(); logptr = _log.begin(); for (toptr = begin(); toptr != end(); fromptr++, toptr++, logptr++) { *fromptr *= oneoversum; *toptr = static_cast (*fromptr*weight + *toptr*(1-weight)); *logptr = log(*toptr); //cout << "cur = " << *fromptr << ", to = " << *toptr << ", log = " << *logptr << endl; } } //////////////////////////////////////////////////////////// void print_values (std::ostream &os) { // static const float binwidth = static_cast (m_numColors)/static_cast (N); // for (int r = 0; r < N; r++) { // for (int g = 0; g < N; g++) { // for (int b = 0; b < N; b++) { // os << r*binwidth << "\t" << g*binwidth << "\t" << b*binwidth << "\t" << m_hist[r][g][b] << std::endl; // } // } // } for (T *it = begin(); it != end(); it++) os << *it << std::endl; } void load_from_file (std::ifstream &is) { initialize(); for (T *it = begin(); it != end(); it++) is >> *it; } void save_to_file (std::ostream &os) { print_values (os); } ////////////// added 5/17 /////////////////////////// void create_initial_hist() { const T uniform = 1.0/static_cast (m_numBins); T *it = &m_initialHist[0][0][0]; for (int i = 0; i < m_numBins; i++, it++) *it = uniform; } ///////////////////////////////////////////////////// void InitColorModel(int face=1) { int r, g, b; int binWidth = (int)(256/N); // cout << "initializing model face = " << face << endl; skinMixGauss smg; nonSkinMixGauss nsmg; for (r = binWidth/2; r < 256; r+=binWidth) { for (g = binWidth/2; g < 256; g+=binWidth) { for (b = binWidth/2; b < 256; b+=binWidth) { RGBTRIPLE rgbt; rgbt.rgbtRed = (BYTE)r; rgbt.rgbtGreen = (BYTE)g; rgbt.rgbtBlue = (BYTE)b; double prob; if (face) prob = smg.getMixProb(rgbt); else prob = nsmg.getMixProb(rgbt); addtobin(rgbt, static_cast (prob)); } } } normalize(); } }; /* ================================================================ */ #endif //__MPHISTOGRAM_H__ /* * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 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. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * 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. * */