www.pudn.com > colortracker.rar > MPHistogramHue.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 __MPHISTOGRAMHUE_H__
#define __MPHISTOGRAMHUE_H__

#include 
#include 
#include "common.h"
#include "mixgauss.h"

/* ================================================================ */

template 
class MPHistogramHue {
		
private:
	int m_numColors;
  float	m_frac;
	T m_hist[N];
	T m_initialHist[N];

	inline T *begin () {
		return (&(m_hist[0]));
	}

	inline const T *end () const {
		return(&(m_hist[N]));
	}

	T sum() {
		T s = 0.0;
		for (T *it = begin(); it != end(); it++) s += *it;
		return s;
	}

	inline float getHue(const RGBTRIPLE & rgb) {
		float	delta,h;
		unsigned char r = rgb.rgbtRed;
		unsigned char g = rgb.rgbtGreen;
		unsigned char b = rgb.rgbtBlue;
		unsigned char cmax = 0, cmin = 0;
		
		if (r >= g) {cmax = r; cmin = g;}
		else {cmax = g; cmin = r;}
		if (b > cmax) cmax = b; 
		else if (b < cmin) cmin = b;
		delta	= static_cast(cmax - cmin);
		if (cmax == r) h = (g-b)/delta;
		else if (cmax == g) h = 2 + (b-r)/delta;
		else h = 4 + (r-g)/delta;	
		h = (h < 0) ? static_cast(h*60.0 + 360.0) : static_cast(h*60.0);
		
		return h;
	}

public:
   /* Default constructor */
	MPHistogramHue() {
		m_numColors = 256;
		m_frac = static_cast(N/360.0f);
		create_initial_hist();
		initialize();
	};

	inline T & get_prob(const RGBTRIPLE & rgb) {
		float h;		
		
		h = getHue(rgb);
		return m_hist[static_cast(h*m_frac)];
	}

	inline void addtobin(const RGBTRIPLE & rgb, const T value = 1.0) {
		float h;		
		
		h = getHue(rgb);
		m_hist[static_cast(h*m_frac)] += value;
	}

	void normalize() {
		T oneoversum = 1.0/sum();
		for (T *it = begin(); it != end(); it++) {
			if (*it) *it *= oneoversum;
		}
	}

	void clear() {
		memset(m_histCur, 0, sizeof(T)*N);
	}

	//////////// modified 5/17 ////////////////////////////
	void initialize() {
		memcpy(m_hist, m_initialHist, sizeof(T)*N);
// 		clear();
// 		add_uniform();
// 		normalize();
	}
	///////////////////////////////////////////////////////

	void add_uniform (T weight = 1.0) {
		const T uniform = weight*(1.0/static_cast(N));
		for (T *it = begin(); it != end(); it++) *it = uniform + *it*(1-weight);
	}

	////////////// fully implemented 5/17 //////////////////////
	void weighted_add (MPHistogramHue & cur, float weight = 0.5f) {
		T *toptr, *fromptr;
		fromptr = cur.begin();
		for (toptr = begin(); toptr != end(); fromptr++, toptr++)
			*toptr = static_cast(*fromptr*weight + *toptr*(1-weight));
	}

	////////////// fully implemented 5/17 //////////////////////
	void update_hist (MPHistogramHue & _cur, MPHistogramHue & _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) {
		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(N);
		for (int i = 0; i < N; i++)
			m_initialHist[i] = uniform;
	}
	/////////////////////////////////////////////////////
	
// 	void InitColorModel()
// 	{
// 		int i;
// 		double dh = 360.0/N;
// 		double h= dh/2.0;
// 		double frac;
// 		double meanHue = (double) 17.9520;  // mean parameter for flesh color model
// 		double SDHue = (double) 12.16429201;   //  standard deviation parameter for flesh color model
// 		for (i = 0; i < N; i++) {
// 			if (h > meanHue)
// 				frac = h - meanHue;
// 			else
// 				frac = meanHue - h;
// 			if (frac > 180.0) 
// 				frac = 360.0 - frac;
// 			frac = (frac/SDHue);
// 			m_hist[i] = static_cast(exp(-(.5*(frac*frac))));
// 			h+=dh;
// 		}	
// 		normalize();
// 	}
	
	/////////////////////////////////////////////////////
	/* this is the initial value for background and foreground of rgb
	   it takes a while to compute and might not be ideal for hue
	*/
	void InitColorModel(int face)
	{
    int r, g, b;
// 		cout << "initializing model face = " << face << endl;
    skinMixGauss smg;
    nonSkinMixGauss nsmg;
    for (r = 0; r < 256; r+=10) {
      for (g = 0; g < 256; g+=10) {
        for (b = 0; b < 256; b+=10) {           
          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 // __MPHISTOGRAMHUE_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. 
 *  
 */