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

mpiprobsearchaux.cpp

Go to the documentation of this file.
00001 #include "pp_preprocessor.h"
00002 #include "mpiprobsearchaux.h"
00003 #include <cmath>
00004 #include <algorithm>
00005 using namespace std;
00006 
00007 /* ================================================================ */
00008 
00009 template <class T, class U>
00010 static inline double max(T x, U y){ return static_cast<T>(x > y ? x : y);}
00011 template <class T, class U>
00012 static inline T min(T x, U y){ return static_cast<T>(x < y ? x : y);}
00013 
00014 /* ================================================================ */
00015 
00016 void HypothesesPerScale::setParams (const double scale, const double shift, const int numX, const int numY) {
00017         m_scale = scale;
00018         m_shift = shift;
00019         m_numX = numX;
00020         m_numY = numY;          
00021 
00022         /* create image of ones to be of same dimensions as scale image */
00023         for (int y=0; y < m_intones.getImHeight(); y++) {
00024                 for (int x=0; x < m_intones.getImWidth(); x++)
00025                         m_intones.setImPixel(x, y, 1);
00026         }       
00027         m_intones.integrate();
00028 }
00029 
00030 /* ================================================================ */
00031 
00032 void HypothesesPerScale::makezero() {
00033         int numpix = m_numX * m_numY;
00034         memset(m_intimage.getArray(), 0, sizeof(double)*m_intimage.getIntNumPix());
00035 }
00036 
00037 /* ================================================================ */
00038 
00039 ScalePyramid::ScalePyramid(const int imageWidth, const int imageHeight) {
00040         set_imagedims(imageWidth, imageHeight);
00041 }
00042 
00043 /* ================================================================ */
00044 
00045 void ScalePyramid::set_imagedims(const int imageWidth, const int imageHeight) {
00046         m_imageWidth = imageWidth;
00047         m_imageHeight = imageHeight;
00048 }
00049 
00050 /* ================================================================ */
00051 
00052 void ScalePyramid::compute_scales(const double scaleUp, const int minSize, const int minScaleUp) {
00053         int scale_factor = minSize + 1;
00054         m_numscales = 0;
00055         while(scale_factor <= m_imageHeight)
00056         {
00057           double d = scale_factor; /* size of box */
00058                 m_scales.push_back(d);
00059                 m_numscales++;
00060                 double testd = d+minScaleUp;
00061                 scale_factor = max (testd, int(static_cast<double>(scale_factor * (1+scaleUp)))
00062                 );
00063         }
00064 }
00065 
00066 /* ================================================================ */
00067 
00068 void ScalePyramid::set_numhyp(const double stride, const int minStride) {
00069         /* annoying loop to determine total number of hypotheses */
00070         m_numhyp = 0;
00071         for (int i=0; i < m_numscales; i++) {
00072                 double s = get_shiftamount(minStride, stride, m_scales[i]);
00073 
00074                 /* formula for size of image given number of hypotheses is: 
00075                                         width = (n - 1) * shift + scale */
00076                 const int numX = 1+(m_imageWidth-m_scales[i])/s;
00077                 const int numY = 1+(m_imageHeight-m_scales[i])/s;
00078                 m_numhyp += numX*numY;
00079         }
00080 }
00081 
00082 /* ================================================================ */
00083 
00084 void ScalePyramid::initPyramid(const double stride, const int minStride) {
00085         int i;
00086         set_numhyp(stride, minStride);
00087 
00088         /* add hypotheses object for each scale, normalizing by the total number of hypotheses */
00089         for (i=0; i < m_numscales; i++) {
00090                 double s = get_shiftamount(minStride, stride, m_scales[i]);
00091 
00092                 /* formula for size of image given number of hypotheses is:
00093                                         width = (n - 1) * shift + scale */
00094                 const int numX = 1+(m_imageWidth-m_scales[i])/s;
00095                 const int numY = 1+(m_imageHeight-m_scales[i])/s;
00096                 add_hypothesesPerScale(m_scales[i], s, numX, numY, m_numhyp);
00097         }
00098 }
00099 
00100 /* ================================================================ */
00101 
00102 double ScalePyramid::get_shiftamount(const int minStride, const double stride, const double d) {
00103         return(max(minStride,d*stride));
00104 }
00105 
00106 /* ================================================================ */
00107 
00108 int ScalePyramid::get_numScales() {
00109         return (m_scale_pyramid.size());
00110 }
00111 
00112 /* ================================================================ */
00113 
00114 void ScalePyramid::add_hypothesesPerScale(const double scale, const double shift, const int numX, const int numY, const int numhyp) {
00115         HypothesesPerScale* hps = new   HypothesesPerScale(numX, numY);
00116         hps->setParams(scale, shift, numX, numY);
00117         int numpix = hps->m_numX * hps->m_numY;
00118         TIntegral<double> &img = hps->m_intimage;
00119         const double normval = 1.0/static_cast<double>(numhyp);
00120         for (int y=0; y < img.getImHeight(); y++) {
00121                 for (int x=0; x < img.getImWidth(); x++)
00122                         img.setImPixel(x,y,normval);
00123         }
00124 //      img.integrate();
00125         m_scale_pyramid.push_back(hps);
00126 }
00127 
00128 /* ================================================================ */
00129 
00130 HypothesesPerScale &ScalePyramid::get_hypothesesPerScale(const int index) {
00131         return(*m_scale_pyramid[index]);
00132 }
00133 
00134 /* ================================================================ */
00135 
00136 int ScalePyramid::get_numhyp() {
00137         return(m_numhyp);
00138 }
00139 
00140 /* ================================================================ */
00141 
00142 void ScalePyramid::makezero() {
00143         for (VHPS::iterator it = m_scale_pyramid.begin(); it != m_scale_pyramid.end(); it++)
00144                 (*it)->makezero();
00145 }
00146 
00147 /* ================================================================ */
00148 
00149 ScalePyramid::~ScalePyramid() {
00150         for (VHPS::iterator it = m_scale_pyramid.begin(); it != m_scale_pyramid.end(); it++) 
00151                 delete *it;
00152 }
00153 
00154 void ScalePyramid::copy(ScalePyramid &copyto) {
00155         copyto.m_numscales = m_numscales;
00156         copyto.m_numhyp = m_numhyp;
00157         copyto.m_imageWidth = m_imageWidth;
00158         copyto.m_imageHeight = m_imageHeight;
00159         copyto.m_scale_pyramid.reserve(m_scale_pyramid.size());
00160         copyto.m_scales.reserve(m_scales.size());
00161         std::copy(m_scale_pyramid.begin(), m_scale_pyramid.end(), copyto.m_scale_pyramid.begin());
00162         std::copy(m_scales.begin(), m_scales.end(), copyto.m_scales.begin());
00163 }
00164 
00165 /* ================================================================ */
00166 
00167 void UniformWindow::set_scalebounds(const double s) {
00168         //m_curhypscale = s;
00169         double adjscaledev = m_scaledev * s;
00170         m_scaleboundup = s + adjscaledev;
00171         m_scalebounddown = s - adjscaledev;
00172 }
00173 
00174 /* ================================================================ */
00175 
00176 void UniformWindow::set_scalebounds(const int s) {
00177         m_intscaleboundup = s + m_intscaledev;
00178         m_intscalebounddown = s - m_intscaledev;
00179 }
00180 
00181 /* ================================================================ */
00182 
00183 bool UniformWindow::is_validscale(const double scale) {
00184         return (scale >= m_scalebounddown && scale <= m_scaleboundup);
00185 }
00186 
00187 /* ================================================================ */
00188 
00189 bool UniformWindow::is_validscale(const int scale) {
00190         return (scale >= m_intscalebounddown && scale <= m_intscaleboundup);
00191 }
00192 
00193 /* ================================================================ */
00194 
00195 /* s = scale at time t
00196          lw = box.i, box.j, box.s, scale at time t+1
00197          hr = half radius
00198          hx, hy = image coordinates of hypothesis h at time t   */
00199 void UniformWindow::get_location_window(const double scale_t, TBox<double> &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) {
00200         
00201         double shift_tnew = lw_tnew.scale*shiftPct;
00202         if (shift_tnew < minStride) shift_tnew = minStride;
00203 
00204         double hrhyp = hr * lw_tnew.scale / shift_tnew;
00205   double cx_t = hx_t + (scale_t / 2);
00206   double cy_t = hy_t + (scale_t / 2);
00207   double x_tnew = cx_t - lw_tnew.scale/2;
00208   double y_tnew = cy_t - lw_tnew.scale/2;
00209   lw_tnew.x = x_tnew/shift_tnew - hrhyp;
00210   lw_tnew.y = y_tnew/shift_tnew - hrhyp;
00211 
00212   lw_tnew.x = floor(lw_tnew.x);
00213         lw_tnew.y = floor(lw_tnew.y);
00214         lw_tnew.size = floor (2*hrhyp + 1 + .5);
00215 
00216         if (lw_tnew.size < 1) lw_tnew.size = 1;
00217 
00218         /* make the window fit  -- must overlap at least one hypothesis */
00219         lw_tnew.x  = min(lw_tnew.x, numx-1);
00220         lw_tnew.y  = min(lw_tnew.y ,numy-1); 
00221         if ((lw_tnew.x + lw_tnew.size ) < 1) { /* move up until upper end is at least at 1. */
00222                 lw_tnew.x = 1-lw_tnew.size;
00223   }
00224         if ((lw_tnew.y + lw_tnew.size) < 1) { /* move up until upper end is at least at 1. */
00225                 lw_tnew.y = 1-lw_tnew.size;  
00226         }
00227 
00229         /* not sure if this is best way to do this -- turns even sized kernels into odd */
00230         if ( ((int)scale_t % 2 == 0) != ((int)lw_tnew.size % 2 == 0))
00231                 lw_tnew.size++;
00232 //      if (((int)(lw_tnew.size) % 2 == 0) &&
00233 //                      ((int)(scale_t) % 2 != 0))
00234 //              lw_tnew.size++;
00236 
00237         /* The derivation as written by John Hershey: 
00238                 h_t(s, i, j).x = i * shift
00239                 h_t(s, i, j).y = j * shift
00240 
00241                 c.x = h_t(s, i, j).x + s/2
00242                 c.y = h_t(s, i, j).y + s/2
00243 
00244                 x' = c.x - s'/2
00245                 y' = c.y - s'/2
00246 
00247                 i' = x'/shift'
00248                 j' = y'/shift'
00249 
00250                 hrhyp = hr * s' / shift'
00251 
00252                 box.i = i' - hrhyp
00253                 box.j = j' - hrhyp
00254                 box.s = 2*hrhyp
00255 
00256                         or
00257 
00258                 -hrhyp + (h_t(s,i,j).x + s/2 - s' / 2) / shift'
00259 
00260                 lw_tnew.x = -hrhyp + ((hx_t + scale_t/2 - lw_tnew.scale/2) / shift_tnew) ;
00261                 lw_tnew.y = -hrhyp + ((hy_t + scale_t/2 - lw_tnew.scale/2) / shift_tnew) ;
00262         */
00263 }
00264 
00265 /* ================================================================ */

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