www.pudn.com > colortracker.rar > mpiprobsearchaux.cpp


#include "pp_preprocessor.h" 
#include "mpiprobsearchaux.h" 
#include  
#include  
using namespace std; 
 
/* ================================================================ */ 
 
template  
static inline double max(T x, U y){ return static_cast(x > y ? x : y);} 
template  
static inline T min(T x, U y){ return static_cast(x < y ? x : y);} 
 
/* ================================================================ */ 
 
void HypothesesPerScale::setParams (const double scale, const double shift, const int numX, const int numY) { 
	m_scale = scale; 
	m_shift = shift; 
	m_numX = numX; 
	m_numY = numY;		 
 
	/* create image of ones to be of same dimensions as scale image */ 
	for (int y=0; y < m_intones.getImHeight(); y++) { 
		for (int x=0; x < m_intones.getImWidth(); x++) 
			m_intones.setImPixel(x, y, 1); 
	}	 
	m_intones.integrate(); 
} 
 
/* ================================================================ */ 
 
void HypothesesPerScale::makezero() { 
	int numpix = m_numX * m_numY; 
	memset(m_intimage.getArray(), 0, sizeof(double)*m_intimage.getIntNumPix()); 
} 
 
/* ================================================================ */ 
 
ScalePyramid::ScalePyramid(const int imageWidth, const int imageHeight) { 
	set_imagedims(imageWidth, imageHeight); 
} 
 
/* ================================================================ */ 
 
void ScalePyramid::set_imagedims(const int imageWidth, const int imageHeight) { 
	m_imageWidth = imageWidth; 
	m_imageHeight = imageHeight; 
} 
 
/* ================================================================ */ 
 
void ScalePyramid::compute_scales(const double scaleUp, const int minSize, const int minScaleUp) { 
	int scale_factor = minSize + 1; 
	m_numscales = 0; 
	while(scale_factor <= m_imageHeight) 
	{ 
	  double d = scale_factor; /* size of box */ 
		m_scales.push_back(d); 
		m_numscales++; 
		double testd = d+minScaleUp; 
		scale_factor = max (testd, int(static_cast(scale_factor * (1+scaleUp))) 
		); 
	} 
} 
 
/* ================================================================ */ 
 
void ScalePyramid::set_numhyp(const double stride, const int minStride) { 
	/* annoying loop to determine total number of hypotheses */ 
	m_numhyp = 0; 
	for (int i=0; i < m_numscales; i++) { 
		double s = get_shiftamount(minStride, stride, m_scales[i]); 
 
		/* formula for size of image given number of hypotheses is:  
					width = (n - 1) * shift + scale */ 
		const int numX = 1+(m_imageWidth-m_scales[i])/s; 
		const int numY = 1+(m_imageHeight-m_scales[i])/s; 
		m_numhyp += numX*numY; 
	} 
} 
 
/* ================================================================ */ 
 
void ScalePyramid::initPyramid(const double stride, const int minStride) { 
	int i; 
	set_numhyp(stride, minStride); 
 
	/* add hypotheses object for each scale, normalizing by the total number of hypotheses */ 
	for (i=0; i < m_numscales; i++) { 
		double s = get_shiftamount(minStride, stride, m_scales[i]); 
 
		/* formula for size of image given number of hypotheses is: 
					width = (n - 1) * shift + scale */ 
		const int numX = 1+(m_imageWidth-m_scales[i])/s; 
		const int numY = 1+(m_imageHeight-m_scales[i])/s; 
		add_hypothesesPerScale(m_scales[i], s, numX, numY, m_numhyp); 
	} 
} 
 
/* ================================================================ */ 
 
double ScalePyramid::get_shiftamount(const int minStride, const double stride, const double d) { 
	return(max(minStride,d*stride)); 
} 
 
/* ================================================================ */ 
 
int ScalePyramid::get_numScales() { 
	return (m_scale_pyramid.size()); 
} 
 
/* ================================================================ */ 
 
void ScalePyramid::add_hypothesesPerScale(const double scale, const double shift, const int numX, const int numY, const int numhyp) { 
	HypothesesPerScale* hps = new	HypothesesPerScale(numX, numY); 
	hps->setParams(scale, shift, numX, numY); 
	int numpix = hps->m_numX * hps->m_numY; 
	TIntegral &img = hps->m_intimage; 
	const double normval = 1.0/static_cast(numhyp); 
	for (int y=0; y < img.getImHeight(); y++) { 
		for (int x=0; x < img.getImWidth(); x++) 
			img.setImPixel(x,y,normval); 
	} 
//	img.integrate(); 
	m_scale_pyramid.push_back(hps); 
} 
 
/* ================================================================ */ 
 
HypothesesPerScale &ScalePyramid::get_hypothesesPerScale(const int index) { 
	return(*m_scale_pyramid[index]); 
} 
 
/* ================================================================ */ 
 
int ScalePyramid::get_numhyp() { 
	return(m_numhyp); 
} 
 
/* ================================================================ */ 
 
void ScalePyramid::makezero() { 
	for (VHPS::iterator it = m_scale_pyramid.begin(); it != m_scale_pyramid.end(); it++) 
		(*it)->makezero(); 
} 
 
/* ================================================================ */ 
 
ScalePyramid::~ScalePyramid() { 
	for (VHPS::iterator it = m_scale_pyramid.begin(); it != m_scale_pyramid.end(); it++)  
		delete *it; 
} 
 
void ScalePyramid::copy(ScalePyramid ©to) { 
	copyto.m_numscales = m_numscales; 
	copyto.m_numhyp = m_numhyp; 
	copyto.m_imageWidth = m_imageWidth; 
	copyto.m_imageHeight = m_imageHeight; 
	copyto.m_scale_pyramid.reserve(m_scale_pyramid.size()); 
	copyto.m_scales.reserve(m_scales.size()); 
	std::copy(m_scale_pyramid.begin(), m_scale_pyramid.end(), copyto.m_scale_pyramid.begin()); 
	std::copy(m_scales.begin(), m_scales.end(), copyto.m_scales.begin()); 
} 
 
/* ================================================================ */ 
 
void UniformWindow::set_scalebounds(const double s) { 
	//m_curhypscale = s; 
	double adjscaledev = m_scaledev * s; 
	m_scaleboundup = s + adjscaledev; 
	m_scalebounddown = s - adjscaledev; 
} 
 
/* ================================================================ */ 
 
void UniformWindow::set_scalebounds(const int s) { 
	m_intscaleboundup = s + m_intscaledev; 
	m_intscalebounddown = s - m_intscaledev; 
} 
 
/* ================================================================ */ 
 
bool UniformWindow::is_validscale(const double scale) { 
 	return (scale >= m_scalebounddown && scale <= m_scaleboundup); 
} 
 
/* ================================================================ */ 
 
bool UniformWindow::is_validscale(const int scale) { 
 	return (scale >= m_intscalebounddown && scale <= m_intscaleboundup); 
} 
 
/* ================================================================ */ 
 
/* s = scale at time t 
	 lw = box.i, box.j, box.s, scale at time t+1 
	 hr = half radius 
	 hx, hy = image coordinates of hypothesis h at time t	*/ 
void UniformWindow::get_location_window(const double scale_t, TBox &lw_tnew, const double hr, const int hx_t, const int hy_t, const double shiftPct, const int minStride, const int numx, const int numy) { 
	 
	double shift_tnew = lw_tnew.scale*shiftPct; 
	if (shift_tnew < minStride) shift_tnew = minStride; 
 
	double hrhyp = hr * lw_tnew.scale / shift_tnew; 
  double cx_t = hx_t + (scale_t / 2); 
  double cy_t = hy_t + (scale_t / 2); 
  double x_tnew = cx_t - lw_tnew.scale/2; 
  double y_tnew = cy_t - lw_tnew.scale/2; 
  lw_tnew.x = x_tnew/shift_tnew - hrhyp; 
  lw_tnew.y = y_tnew/shift_tnew - hrhyp; 
 
  lw_tnew.x = floor(lw_tnew.x); 
 	lw_tnew.y = floor(lw_tnew.y); 
	lw_tnew.size = floor (2*hrhyp + 1 + .5); 
 
	if (lw_tnew.size < 1) lw_tnew.size = 1; 
 
	/* make the window fit  -- must overlap at least one hypothesis */ 
	lw_tnew.x  = min(lw_tnew.x, numx-1); 
	lw_tnew.y  = min(lw_tnew.y ,numy-1);  
	if ((lw_tnew.x + lw_tnew.size ) < 1) { /* move up until upper end is at least at 1. */ 
		lw_tnew.x = 1-lw_tnew.size; 
  } 
	if ((lw_tnew.y + lw_tnew.size) < 1) { /* move up until upper end is at least at 1. */ 
		lw_tnew.y = 1-lw_tnew.size;   
	} 
 
		///////////////////////////////////// 
	/* not sure if this is best way to do this -- turns even sized kernels into odd */ 
	if ( ((int)scale_t % 2 == 0) != ((int)lw_tnew.size % 2 == 0)) 
		lw_tnew.size++; 
// 	if (((int)(lw_tnew.size) % 2 == 0) && 
// 			((int)(scale_t) % 2 != 0)) 
// 		lw_tnew.size++; 
	///////////////////////////////////// 
 
	/* The derivation as written by John Hershey:  
		h_t(s, i, j).x = i * shift 
		h_t(s, i, j).y = j * shift 
 
		c.x = h_t(s, i, j).x + s/2 
		c.y = h_t(s, i, j).y + s/2 
 
		x' = c.x - s'/2 
		y' = c.y - s'/2 
 
		i' = x'/shift' 
		j' = y'/shift' 
 
		hrhyp = hr * s' / shift' 
 
		box.i = i' - hrhyp 
		box.j = j' - hrhyp 
		box.s = 2*hrhyp 
 
			or 
 
		-hrhyp + (h_t(s,i,j).x + s/2 - s' / 2) / shift' 
 
		lw_tnew.x = -hrhyp + ((hx_t + scale_t/2 - lw_tnew.scale/2) / shift_tnew) ; 
		lw_tnew.y = -hrhyp + ((hy_t + scale_t/2 - lw_tnew.scale/2) / shift_tnew) ; 
	*/ 
} 
 
/* ================================================================ */