ehbsw.hpp
Go to the documentation of this file.
00001 #include <cstring>
00002 #include <cmath>
00003 #include <cstdlib>
00004 
00005 #include <iostream>
00006 using namespace std;
00007 
00008 
00009 
00010 //first version of integral histograms
00011 class EHBSW
00012 {
00013 protected:
00014   const int* I_pu;
00015   float *Iout_pf; // output map
00016   int* wavefront1_pu;
00017   int* wavefront2_pu;
00018   float* thehist_pu;
00019   int w_u;
00020   int h_u;
00021   int hs_u; //half_win_size
00022   int vs_u; //vocabulary_size
00023 
00024 
00025   int x_u; 
00026   int y_u;
00027 
00028   void initline(int y_line)
00029   {
00030     memset(wavefront1_pu,0,vs_u*sizeof(int));
00031     memset(wavefront2_pu,0,vs_u*sizeof(int));
00032     //memset(thehist_pu,0,(vs_u+1)*sizeof(int));
00033     x_u=hs_u;
00034     for(int j=y_line-hs_u; j<=y_line+hs_u; j++)
00035     {
00036       for(int i=0; i<2*hs_u+1; i++)
00037       {
00038         ++(wavefront1_pu[I_pu[i+w_u*j]]);
00039       }
00040     }
00041     for(int k=0; k<vs_u; k++)
00042     {
00043       thehist_pu[k]=wavefront1_pu[k];
00044     }
00045     this->process_hist();
00046   }
00047     
00048   // no check!
00049   void advance_one()
00050   {
00051     x_u++;
00052     int i1=x_u + hs_u;
00053     int i2=x_u - hs_u - 1;
00054     for(int j=y_u-hs_u; j<=y_u+hs_u; j++)
00055     {
00056       wavefront1_pu[I_pu[i1+w_u*j]]++;
00057       wavefront2_pu[I_pu[i2+w_u*j]]++;
00058     }
00059     
00060     //  int g=0;
00061     //  for(int j=y_u-hs_u; j<=y_u+hs_u; j++, g++)
00062     //    cout<<"("<<i1<<","<<j<<")"<<I_pu[i1+w_u*j]<<",";
00063     //  cout<<endl;
00064     //  g=0;
00065     //  for(int j=y_u-hs_u; j<=y_u+hs_u; j++,g++)
00066     //    cout<<"("<<i2<<","<<j<<")"<<I_pu[i2+w_u*j]<<",";
00067     // cout<<endl;
00068 
00069     for(int k=0; k<vs_u; k++) 
00070     {
00071       thehist_pu[k]=wavefront1_pu[k]-wavefront2_pu[k];
00072       //cout<<thehist_pu[k]<<",";
00073     }
00074     //cout<<thehist_pu[vs_u]<<endl<<endl;
00075     this->process_hist();
00076   }
00077   
00078   virtual void process_hist() = 0;
00079 
00080 public:
00084   EHBSW(float* Ioutin, int* Iin, int win, int hin, int hsin, int vsin):w_u(win),h_u(hin),hs_u(hsin)
00085   {
00086     vs_u=vsin+1; //+1 to account for empty pos
00087     Iout_pf=Ioutin;
00088     memset(Iout_pf,0,win*hin*sizeof(float));
00089     //I_pu=new int[win*hin];
00090     //memcpy(I_pu, Iin, win*hin*sizeof(int));
00091     I_pu=Iin;
00092     wavefront1_pu=new int[vs_u]; //these do not need the bias val
00093     wavefront2_pu=new int[vs_u]; // -- " --
00094     thehist_pu=new float[vs_u+1]; //+1 account for empty bin + bias
00095     thehist_pu[vs_u] = 1.0; //should never be changed (bias)
00096     //this->initline(this->hs);
00097     
00098     //init hist    
00099     //x_u=hs_u;
00100     //y_u=hs_u; 
00101   }
00102 
00103   EHBSW(const EHBSW& a);
00104   
00105   ~EHBSW()
00106   {
00107     delete [] wavefront1_pu;
00108     delete [] wavefront2_pu;
00109     delete [] thehist_pu;
00110     //delete [] I_pu;
00111   }
00112 
00113   void run_image()
00114   {
00115 
00116     for(y_u=hs_u; y_u<h_u-hs_u; y_u++)
00117     {
00118       this->initline(y_u);
00119       for(int i=hs_u+1; i<w_u-hs_u; i++)//first is already done
00120       {
00121         this->advance_one();
00122       }
00123     }
00124   }
00125 
00126 };
00127 
00128   
00129 class EHBSW_PowNorm : public EHBSW //for now alpha=0.5 AND for logistic regression
00130 {
00131 private:
00132   const float* classifier_pf;
00133   
00134   void process_hist()
00135   {
00136 
00137     //L2 norm |sqrt(x)|_2 == sqrt(|x|_1)  
00138     float N=0; //(this->hs*2+1)*(this->hs*2+1); // assuming no missing
00139     // points. Could be solved
00140     // with a fallback bin for
00141     // illegal descs.
00142     float score=0;
00143     for (int v=0; v<vs_u; v++) //+1 for the zero (empty)
00144     {
00145       //cout<<"("<<thehist_pu[v+1]<<","<<classifier_pf[v]<<")";
00146       score += sqrt(thehist_pu[v+1])*classifier_pf[v]; //v+1 to skip empty val
00147       N += thehist_pu[v+1];
00148     }
00149     //cout<<endl;
00150     //cout<<x_u<<" "<<y_u<<" "<<N<<endl;
00151     N=sqrt(N);
00152     if (x_u<hs_u || x_u>w_u-hs_u)
00153       cout<<"alarm! "<<x_u<<endl;
00154     Iout_pf[x_u+y_u*w_u]=1.0/(1.0+exp(-score/N));
00155   }
00156 public:
00157 
00158   EHBSW_PowNorm(float* Ioutin, int* Iin, int win, int hin, int hsin, int vsin, float* c): EHBSW(Ioutin, Iin, win, hin, hsin, vsin)
00159   {
00160     //classifier_pf=new float[vsin];
00161     //memcpy(classifier_pf,c, vs_u*sizeof(float));
00162     classifier_pf=c;
00163   }
00164   
00165   ~EHBSW_PowNorm()
00166   {
00167     //delete [] classifier_pf;
00168   }
00169 };
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 //################ DEPRECATED!
00182 
00183 class EHBSW_LinHist_L1 : public EHBSW
00184 {
00185 private:
00186   const float* classifier_pf;
00187   // TODO --- THIS CLASS IS OUTDATED!!!
00188 
00189   void process_hist()
00190   {
00191     //L1 norm
00192     float N=0;//(this->hs*2+1)*(this->hs*2+1); // assuming no missing
00193     // points. Could be solved
00194     // with a fallback bin for
00195     // illegal descs.
00196     float score=0;
00197     for (int v=0; v<vs_u; v++) //+1 accounts for bias
00198     {
00199       //cout<<thehist_pu[v]<<",";
00200       score += thehist_pu[v]*classifier_pf[v]; //v+1 to skip empty line //re-add +1 in hist[v<-]
00201       N += thehist_pu[v];
00202     }
00203     //cout<<endl;
00204     //cout<<x_u<<" "<<y_u<<" "<<w_u<<endl;
00205     Iout_pf[x_u+y_u*w_u]=score/N;
00206   }
00207 public:
00208 
00209   EHBSW_LinHist_L1(float* Ioutin, int* Iin, int win, int hin, int hsin, int vsin, float* c): EHBSW(Ioutin, Iin, win, hin, hsin, vsin)
00210   {
00211     //classifier_pf=new float[vsin];
00212     //memcpy(classifier_pf,c, vs_u*sizeof(float));
00213     classifier_pf=c;
00214   }
00215   
00216   ~EHBSW_LinHist_L1()
00217   {
00218     //delete [] classifier_pf;
00219   }
00220 };


iri_bow_object_detector
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 22:45:45