www.pudn.com > colortracker.rar > probColorSearch.h


/* 
 *  probColorSearch.h 
 *   
 * 
 *  Created by Bret Fortenberry 2003. 
 *  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, Ian Fasel, Josh Susskind 
 */ 
#ifndef _PROBCOLORSEARCH_H_
#define _PROBCOLORSEARCH_H_

// #ifdef __APPLE_CC__
// #include 
// #include 
// #include 
// #include 
// #else
#include "mpisearch.h"
#include "square.h"
#include "featuredata.h"
#include "rimage.h" 
// #endif

// #include "ffcommon.h"

#include 
#include 
#include 

template < class T >
class ProbColorSearch : MPISearchObjectDetector {
 public:
    enum SearchType {
        Max, 
        Mean
    };

	ProbColorSearch();
	~ProbColorSearch();
	int CreateData(int width, int height, int searchWidth, int searchHeight); // create a rectangle in data
	double probSearch(SearchType type, RImage &pixels, int size, TSquare& win, TSquare& var, 
                      double thresh, int LaplacianPrior);

 private:
	int set_width, set_height;
 
	double logPrior(int x, int y, double d, TSquare &win, TSquare& var, int LaplacianPrior); 
};

template < class T >
ProbColorSearch::ProbColorSearch(){};

template < class T >
ProbColorSearch::~ProbColorSearch(){};


template < class T >
int ProbColorSearch::CreateData(int width, int height, int searchWidth, int searchHeight){
	const int origin = 0;
 	data.features = new Feature;
	data.features[0].numcorners = 4;
	data.numStdAdjusts = 0;
	data.numfeatures = 1;
	data.numcascades = 0;
	data.features[0].corners = new Corner[4]; 
	data.features[0].corners[0].x = origin; 						data.features[0].corners[0].y = origin; 							data.features[0].corners[0].value =   1;
	data.features[0].corners[1].x = origin+searchWidth; data.features[0].corners[1].y = origin; 							data.features[0].corners[1].value =  -1;
	data.features[0].corners[2].x = origin; 						data.features[0].corners[2].y = origin+searchHeight; data.features[0].corners[2].value =  -1;
	data.features[0].corners[3].x = origin+searchWidth; data.features[0].corners[3].y = origin+searchHeight; data.features[0].corners[3].value =   1;
	
	data.patch_width = searchWidth + 1;
	data.patch_height = searchHeight + 1;
	set_width = width; 
	set_height = height;
	stream.init(width, height, data, 1);

	return 1;
}

template < class T >
double ProbColorSearch::logPrior(int x, int y, double d, TSquare &win, TSquare& var, int LaplacianPrior) 
{			
	if (LaplacianPrior == 1){
		// Laplacian prior is the default
		const double c = 3*log(2.0) + log(var.x * var.y*var.size);
		double zscore = fabs(x-win.x)*var.x + fabs(y-win.y)*var.y + fabs(d-win.size)*var.size;
		return ( -c - zscore);
	}
	else{ // Gaussian prior
		const double c = 1.5 * log(2*PI); // log(sqrt(cube(2*PI)))
		return (- sqr(x - win.x)*var.x 
				- sqr(y - win.y)*var.y 
				- sqr(d - win.size)*var.size
				- c - 0.5 * (var.x + var.y + var.size));
	}
}

template < class T >
double ProbColorSearch::probSearch(SearchType type, RImage &pixels, int size, TSquare& win, TSquare& var, double thresh, int LaplacianPrior){
	 // Cache the corners
	
	if(pixels.width != set_width || pixels.height != set_height || !stream.allocated) {
        stream.reset(pixels.width, pixels.height, data, 1);
		set_width = pixels.width; set_height = pixels.height;
	}
  
	
	// Integrate the image
  static_cast* >(stream.images[0])->integrate(pixels);
  //stream.images[0]->print();
	
  bool bPrior = var.x > 0 && var.y > 0 && var.size > 0;
  bool firstTime = true;

  double lpostbest = 0;
  double max_llike = 0, llike = 0;
  double lprior = 0; // log prior
  double ptot = 0;
	double d1;

  double mx = 0;
  double my = 0;
  double md = 0;

  double sx = 0;
  double sy = 0;
  double sd = 0;

	// Do the search
  int scale_index;
  float scale_factor;
  int x, y;
  int numWindows = 0;

  MPIImagePyramid::const_iterator scale = stream.mpi->begin();
  MPIImagePyramid::const_iterator last_scale = stream.mpi->end(); 
 
	scale_factor = 0; 
	while ( scale_factor*size < .092*pixels.width) { 
		scale_index = scale.getScale(scale_factor); 
		scale++; 
	}
  for( ; scale != last_scale; ++scale){
    // get pointers to cached values for this scale
    scale_index = scale.getScale(scale_factor);
    //T sf2 = scale_factor * scale_factor;
    CornerCache< T > **corners = stream.corners[scale_index];
    CornerCache< T > *feature_corners = corners[0];
    //T *fns = stream.fns[scale_index];
    d1 = scale_factor * size ;
    T a,b,c,d;
    MPIScaledImage::const_iterator window = (*scale).begin(), last_window = (*scale).end();
    for( ; window != last_window; ++window, ++numWindows){
      // Do the per window computation 
        llike = 0;
        for(int corner = 0; corner < data.features[0].numcorners; corner+=4) {
            a = window.getPixel0( feature_corners[corner].scaledIndex ) * feature_corners[corner].value;
            b = window.getPixel0( feature_corners[corner+1].scaledIndex ) * feature_corners[corner+1].value;
            c = window.getPixel0( feature_corners[corner+2].scaledIndex ) * feature_corners[corner+2].value;
            d = window.getPixel0( feature_corners[corner+3].scaledIndex ) * feature_corners[corner+3].value;
        llike += a + b + c + d; // this is the result of getFeat
        }

        if (llike < thresh) {
            continue;
        }

        window.getCoords(x,y);
        if (bPrior){
            lprior = logPrior(x,y,d1,win,var,LaplacianPrior);
        }
		
        double lpost;
        lpost = llike + lprior;
  
 
	  	double p = exp(lpost);
        ptot += p;
		
  		switch(type) {
	  	 case Max:
		  	if (firstTime || (lpost > lpostbest)){
			  	firstTime = false;
				  mx = x;
				  my = y;
				  md = d1;
				  lpostbest = lpost;
					max_llike = llike;
					
	  		} // end if max
  			break;
		   case Mean:
			  {
				  // do mean here
				  mx += p*x;
  				my += p*y;
	  			md += p * d1;
		 		
			  	sx += p*(x*x);
  				sy += p*(y*y);
	  			sd += p*(d1*d1);
		  	}
		  	break;
		   default:
			  break;
        }
 			
    } // End window loop
  } // End scale loop
  switch(type) {
    case Max:
      break;
	case Mean:
	{
		mx /= ptot;
		my /= ptot;
		md /= ptot;

		sx = sx/ptot - mx*mx;
		sy = sy/ptot - mx*mx;
		sd = sd/ptot - mx*mx;
	}
		break;
	default:
		break;
  }
  win = TSquare(md,mx,my,0);
  var = TSquare(sd,sx,sy,0);

  if(type == Max) {
    return max_llike;
  }
		
  //get value of mean feature  need to update fimage to random access x,y before implement
  /*int scale = (int) (md/size);	
  int a = static_cast((mx - 1));
  int b = static_cast((my - 1));
	double f = 0;

	for(int corner = 0; corner < data.feature[0].numcorners; corner++) {

			f+= ii.getPixel(a + scale * data.feature[0].corners[corner].x,
						     b + scale * data.feature[0].corners[corner].y) 
					* data.feature[0].corners[corner].value;

	} // end feature loop
	return f;*/
	return -1;
	
}

#endif
 
 
/* 
 *  
 * 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. 
 *  
 */