covEstimate.C

Go to the documentation of this file.
00001 /*!@file VFAT/covEstimate.C quick estimator for coveriance matrix
00002  */
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: T Nathan Mundhenk <mundhenk@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/VFAT/covEstimate.C $
00036 // $Id: covEstimate.C 10794 2009-02-08 06:21:09Z itti $
00037 //
00038 // ############################################################
00039 // ############################################################
00040 // ##### --- VFAT ---
00041 // ##### Vision Feature Analysis Tool:
00042 // ##### T. Nathan Mundhenk nathan@mundhenk.com
00043 // ##### Laurent Itt itti@pollux.usc.edu
00044 // #####
00045 // ############################################################
00046 // ############################################################
00047 // Create object and copy pointer to data matrix
00048 
00049 #include "VFAT/covEstimate.H"
00050 
00051 #include "Util/Assert.H"
00052 #include "Image/Point2D.H"
00053 
00054 #include <fstream>
00055 
00056 template <class T>
00057 covEstimate<T>::covEstimate(std::vector<std::vector<T> > &_inputSpace,
00058                             covHolder<T> &_ch)
00059 {
00060   cov_currentStep = 0.0F;
00061   // call resize space
00062   std::cerr << "Inputing space\n";  setNew(_inputSpace,true,_ch,0);
00063 }
00064 
00065 /*********************************************************************/
00066 
00067 template <class T>
00068 covEstimate<T>::covEstimate(std::vector<std::vector<T*> > &_inputSpace,
00069                             covHolder<T> &_ch)
00070 {
00071   cov_currentStep = 0.0F;
00072   // call resize space
00073   std::cerr << "Inputing space\n";  setNew(_inputSpace,true,_ch,0);
00074   cov_samples = 0;
00075 }
00076 
00077 /*********************************************************************/
00078 template <class T>
00079 covEstimate<T>::covEstimate()
00080 {
00081   cov_currentStep = 0.0F;
00082 }
00083 
00084 /*********************************************************************/
00085 template <class T>
00086 covEstimate<T>::~covEstimate()
00087 {}
00088 
00089 // Find the mean values in the data set
00090 
00091 /*********************************************************************/
00092 
00093 template <class T>
00094 void covEstimate<T>::findMean()
00095 {
00096   T grandMean = 0;
00097   if(cov_usePtr == false)
00098   {
00099     for(unsigned int i = 0; i < cov_dim; i++)
00100     {
00101       T total = 0;
00102       T TSS   = 0;
00103       for(unsigned int k = 0; k < cov_samples; k++)
00104       {
00105         total += cov_inputSpace->at(i)[k];
00106         TSS   += pow(cov_inputSpace->at(i)[k],2);
00107       }
00108       cov_covHolder->mean[i] = total/cov_samples;
00109       cov_covHolder->STD[i]  = sqrt((TSS/cov_samples)
00110                                    - pow(cov_covHolder->mean[i],2));
00111       grandMean += cov_covHolder->mean[i];
00112     }
00113   }
00114   else
00115   {
00116     if(cov_useFloatSpecial == false)
00117     {
00118       for(unsigned int i = 0; i < cov_dim; i++)
00119       {
00120         T total = 0;
00121         T TSS   = 0;
00122         for(unsigned int k = 0; k < cov_samples; k++)
00123         {
00124           total += *cov_inputSpacePtr->at(i)[k];
00125           TSS   += pow(*cov_inputSpacePtr->at(i)[k],2);
00126         }
00127         cov_covHolder->mean[i] = total/cov_samples;
00128 
00129         T P = (TSS/cov_samples) - pow(cov_covHolder->mean[i],2);
00130 
00131         // rounding errors in floting points can make this less than 0
00132         if(P > 0)
00133           cov_covHolder->STD[i] = sqrt(P);
00134         else
00135           cov_covHolder->STD[i] = 0.0F;
00136 
00137         grandMean += cov_covHolder->mean[i];
00138       }
00139     }
00140     else
00141     {
00142       for(unsigned int i = 0; i < cov_dim; i++)
00143       {
00144         float total = 0;
00145         double TSS  = 0;
00146         for(unsigned int k = 0; k < cov_samples; k++)
00147         {
00148           total += *cov_FinputSpacePtr->at(i)[k];
00149           TSS   += (double)pow(*cov_FinputSpacePtr->at(i)[k],2);
00150         }
00151         cov_covHolder->mean[i] = total/cov_samples;
00152 
00153         T P = (TSS/cov_samples) - pow(cov_covHolder->mean[i],2);
00154 
00155         // rounding errors in floting points can make this less than 0
00156         if(P > 0)
00157           cov_covHolder->STD[i] = sqrt(P);
00158         else
00159           cov_covHolder->STD[i] = 0.0F;
00160 
00161         grandMean += cov_covHolder->mean[i];
00162       }
00163     }
00164   }
00165   cov_covHolder->grandMean = grandMean/cov_dim;
00166   cov_currentStep = 1.0F;
00167 }
00168 
00169 /*********************************************************************/
00170 
00171 template <class T>
00172 void covEstimate<T>::findMinMax()
00173 {
00174   T *minHold;
00175   T *maxHold;
00176   if(cov_usePtr == false)
00177   {
00178     for(unsigned int i = 0; i < cov_dim; i++)
00179     {
00180       minHold = &cov_covHolder->mean[i];
00181       maxHold = &cov_covHolder->mean[i];
00182       for(unsigned int k = 0; k < cov_samples; k++)
00183       {
00184         if(cov_inputSpace->at(i)[k] > *maxHold)
00185           maxHold = &cov_inputSpace->at(i)[k];
00186         if(cov_inputSpace->at(i)[k] < *minHold)
00187           minHold = &cov_inputSpace->at(i)[k];
00188       }
00189       cov_covHolder->min[i] = *minHold;
00190       cov_covHolder->max[i] = *maxHold;
00191     }
00192   }
00193   else
00194   {
00195     if(cov_useFloatSpecial == false)
00196     {
00197       for(unsigned int i = 0; i < cov_dim; i++)
00198       {
00199         minHold = &cov_covHolder->mean[i];
00200         maxHold = &cov_covHolder->mean[i];
00201         for(unsigned int k = 0; k < cov_samples; k++)
00202         {
00203           if(*cov_inputSpacePtr->at(i)[k] > *maxHold)
00204             maxHold = cov_inputSpacePtr->at(i)[k];
00205           if(*cov_inputSpacePtr->at(i)[k] < *minHold)
00206             minHold = cov_inputSpacePtr->at(i)[k];
00207         }
00208         cov_covHolder->min[i] = *minHold;
00209         cov_covHolder->max[i] = *maxHold;
00210       }
00211     }
00212     else
00213     {
00214       for(unsigned int i = 0; i < cov_dim; i++)
00215       {
00216         float FminHold = cov_covHolder->mean[i];
00217         float FmaxHold = cov_covHolder->mean[i];
00218         for(unsigned int k = 0; k < cov_samples; k++)
00219         {
00220           if(*cov_FinputSpacePtr->at(i)[k] > FmaxHold)
00221             FmaxHold = *cov_FinputSpacePtr->at(i)[k];
00222           if(*cov_FinputSpacePtr->at(i)[k] < FminHold)
00223             FminHold = *cov_FinputSpacePtr->at(i)[k];
00224         }
00225         cov_covHolder->min[i] = FminHold;
00226         cov_covHolder->max[i] = FmaxHold;
00227       }
00228     }
00229   }
00230 }
00231 
00232 /*********************************************************************/
00233 // translate space so that the mean is centered on the origen
00234 
00235 template <class T>
00236 void covEstimate<T>::translateSpace()
00237 {
00238   if(cov_usePtr == true)
00239   {
00240     if(cov_useFloatSpecial == false)
00241     {
00242       for(unsigned int i = 0; i < cov_dim; i++)
00243       {
00244         for(unsigned int k = 0; k < cov_samples; k++)
00245         {
00246           cov_transSpace[i][k] = *cov_inputSpacePtr->at(i)[k] -
00247             cov_covHolder->mean[i];
00248         }
00249       }
00250     }
00251     else
00252     {
00253       for(unsigned int i = 0; i < cov_dim; i++)
00254       {
00255         for(unsigned int k = 0; k < cov_samples; k++)
00256         {
00257           cov_transSpace[i][k] = (T)*cov_FinputSpacePtr->at(i)[k] -
00258             cov_covHolder->mean[i];
00259         }
00260       }
00261     }
00262   }
00263   else
00264   {
00265     for(unsigned int i = 0; i < cov_dim; i++)
00266     {
00267       for(unsigned int k = 0; k < cov_samples; k++)
00268       {
00269         cov_transSpace[i][k] = cov_inputSpace->at(i)[k] -
00270           cov_covHolder->mean[i];
00271       }
00272     }
00273   }
00274 
00275   cov_currentStep = 2.0F;
00276 }
00277 
00278 /*********************************************************************/
00279 // find the eigen vectors, then rotate to (approx) 0 covariance
00280 // NOTE: to increase accuracy call this method several consecutive times
00281 // with doSimple equal to false. Or use a combination of simple and
00282 // following several non-simple rotations (???????)
00283 
00284 template <class T>
00285 void covEstimate<T>::findRotatios(bool doSimple = true)
00286 {
00287   cov_doSimple = doSimple;
00288   cov_listSize = 0;
00289 
00290   // For each feature
00291   for(unsigned int i = 0; i < cov_dim; i++)
00292   {
00293     // for each feature (upper triangular)
00294     for(unsigned int j = i + 1; j < cov_dim; j++,cov_listSize++)
00295     {
00296       T meanRatio = 0;
00297       T eucDist   = 0;
00298       T eucSum    = 0;
00299 
00300       // for each sample
00301       for(unsigned int k = 0; k < cov_samples; k++)
00302       {
00303         T *trans_ik = &cov_transSpace[i][k];
00304         T *trans_jk = &cov_transSpace[j][k];
00305 
00306         /* find ratio between x,y and the distance to coord */
00307         eucDist = sqrt(pow(*trans_ik,2) +
00308                        pow(*trans_jk,2));
00309 
00310         /* sum and find average ratios weighted for distance */
00311         if(*trans_ik != 0)
00312         {
00313           meanRatio += (*trans_jk/(*trans_ik))
00314             * eucDist;
00315         }
00316         eucSum += eucDist;
00317       }
00318 
00319       //alter to weighted average by distance
00320       if(eucSum != 0)
00321         cov_covHolder->meanRatio[i][j] = meanRatio/eucSum;
00322       else
00323         cov_covHolder->meanRatio[i][j] = 0;
00324 
00325 
00326       cov_covHolder->meanTheta[i][j] = atan(cov_covHolder->meanRatio[i][j]);
00327 
00328       T *meanTheta = &cov_covHolder->meanTheta[i][j];
00329 
00330       for(unsigned int k = 0; k < cov_samples; k++)
00331       {
00332         //LINFO("C1 %d %d %d",i,j,k);
00333         T *trans_ik = &cov_transSpace[i][k];
00334         T *trans_jk = &cov_transSpace[j][k];
00335         T *rot_ik   = &cov_rotSpace[i][k];
00336         T *rot_jk   = &cov_rotSpace[j][k];
00337 
00338         int inv1, inv2;
00339         // invert the sin from the last axis to the first only
00340 
00341         inv1 = 1;
00342         inv2 = -1;
00343 
00344         T X = *trans_ik;
00345         T Y = *trans_jk;
00346 
00347         // X' = [Xcos(theta)*Y(inv1)sin(theta)]
00348         *rot_ik = (X * cos(*meanTheta))
00349           + (Y * (inv1*sin(*meanTheta)));
00350 
00351         // Y' = [X(inv2)sin(theta)*Ycos(theta)]
00352         *rot_jk = (X * (inv2*(sin(*meanTheta))))
00353           + (Y * cos(*meanTheta));
00354       }
00355 
00356       T SS = 0;
00357       for(unsigned int k = 0; k < cov_samples; k++)
00358       {
00359         SS += pow(cov_rotSpace[i][k],2)/cov_samples;
00360       }
00361       if(cov_sumSquare[i] == 0)
00362       {
00363         cov_sumSquare[i] = SS;
00364       }
00365       cov_sumSquare[i] += pow(sqrt(SS) * cos(-1*(*meanTheta)),2);
00366 
00367       SS = 0;
00368       for(unsigned int k = 0; k < cov_samples; k++)
00369       {
00370         SS += pow(cov_rotSpace[j][k],2)/cov_samples;
00371       }
00372       if(cov_sumSquare[j] == 0)
00373       {
00374         cov_sumSquare[j] = SS;
00375       }
00376       cov_sumSquare[j] += pow(sqrt(SS) * cos(-1*(*meanTheta)),2);
00377     }
00378   }
00379   cov_currentStep = 3.0F;
00380 }
00381 
00382 /*********************************************************************/
00383 
00384 template <class T>
00385 void covEstimate<T>::findVariance()
00386 {
00387    for(unsigned int i = 0; i < cov_dim; i++)
00388   {
00389     if(cov_sumSquare[i] > 0)
00390       cov_covHolder->eigenVal[i] = sqrt(cov_sumSquare[i]);
00391     else
00392       cov_covHolder->eigenVal[i] = 0;
00393   }
00394   cov_currentStep = 4.0F;
00395 }
00396 
00397 /*********************************************************************/
00398 
00399 template <class T>
00400 T covEstimate<T>::getP(std::vector<T> sample, covHolder<T> &ch, int stop)
00401 {
00402   int stopper = ch.dim-stop;
00403 
00404   ASSERT(sample.size() == ch.dim);
00405 
00406   typename std::vector<T>::iterator iSample;
00407   typename std::vector<T>::iterator iTrans;
00408   typename std::vector<T>::iterator iMean;
00409   typename std::vector<T>::iterator iBias;
00410 
00411   iSample = ch.mean.begin();
00412   iMean   = sample.begin();
00413   iTrans  = cov_meanTranslate.begin();
00414   iBias   = ch.bias.begin();
00415 
00416   while(iSample != ch.mean.end())
00417   {
00418     *iTrans = *iMean - *iSample;
00419     ++iTrans; ++iMean; ++iSample;
00420   }
00421 
00422   // more stuff
00423   for(unsigned int i = 0; i < sample.size(); i++)
00424   {
00425     for(unsigned int j = i + 1; j < sample.size(); j++)
00426     {
00427       //alter to weighted average by distance
00428 
00429       T *meanTheta = &ch.meanTheta[i][j];
00430 
00431       int inv1, inv2;
00432       // invert the sin from the last axis to the first only
00433 
00434       inv1 = 1;
00435       inv2 = -1;
00436 
00437       T X = cov_meanTranslate[i];
00438       T Y = cov_meanTranslate[j];
00439       T rot_ik,rot_jk;
00440 
00441       // X' = [Xcos(theta)*Y(inv1)sin(theta)]
00442       rot_ik = (X * cos(*meanTheta))
00443         + (Y * (inv1*sin(*meanTheta)));
00444 
00445       // Y' = [X(inv2)sin(theta)*Ycos(theta)]
00446       rot_jk = (X * (inv2*(sin(*meanTheta))))
00447         + (Y * cos(*meanTheta));
00448 
00449       if(cov_sumSquare[i] == 0)
00450       {
00451         cov_sumSquare[i] = rot_ik;
00452       }
00453       cov_sumSquare[i] += pow(rot_ik * cos(-1*(*meanTheta)),2);
00454 
00455       if(cov_sumSquare[j] == 0)
00456       {
00457         cov_sumSquare[j] = rot_jk;
00458       }
00459       cov_sumSquare[j] += pow(rot_jk * cos(-1*(*meanTheta)),2);
00460     }
00461   }
00462 
00463   for(unsigned int i = 0; i < sample.size(); i++)
00464   {
00465     if(cov_sumSquare[i] > 0)
00466       cov_sumSquare[i] = sqrt(cov_sumSquare[i]);
00467     else
00468       cov_sumSquare[i] = 0;
00469   }
00470 
00471   iTrans = cov_sumSquare.begin();
00472   int i = 0;
00473   T P = 0;
00474   T TP = 0;
00475   while(iTrans != cov_sumSquare.end())
00476   {
00477     if(ch.eigenVal[i] != 0)
00478       TP = gauss(*iTrans,0,ch.eigenVal[i])*(*iBias);
00479 
00480     if(i == 0)
00481       P = TP;
00482     else
00483     {
00484       if(i < stopper)
00485         if(TP != 0)
00486         {
00487           P *= TP;
00488         }
00489     }
00490     i++;
00491     ++iTrans; ++iBias;
00492   }
00493   P = P*ch.samples;
00494 
00495   return P;
00496 }
00497 
00498 /*********************************************************************/
00499 
00500 template <class T>
00501 T covEstimate<T>::getD(std::vector<T> sample, covHolder<T> &ch, int stop)
00502 {
00503   int stopper = ch.dim-stop;
00504   T SS        = 0.0F;
00505 
00506   ASSERT(sample.size() == ch.dim);
00507 
00508   typename std::vector<T>::iterator iSample;
00509   typename std::vector<T>::iterator iMean;
00510   typename std::vector<T>::iterator iBias;
00511 
00512   iSample = ch.mean.begin();
00513   iMean   = sample.begin();
00514   iBias   = ch.bias.begin();
00515 
00516   for(int i = 0; i < stopper; i++)
00517   {
00518     SS += pow((*iMean - *iSample)/(*iBias),2);
00519     ++iMean; ++iSample; ++iBias;
00520   }
00521   return sqrt(SS);
00522 }
00523 
00524 /*********************************************************************/
00525 
00526 template <class T>
00527 T covEstimate<T>::getD(std::vector<T> *point1,std::vector<T> *point2,
00528                        std::vector<T> *biasSet, bool useGause)
00529 {
00530   typename std::vector<T>::iterator p1 = point1->begin();
00531   typename std::vector<T>::iterator p2 = point2->begin();
00532   typename std::vector<T>::iterator b  = biasSet->begin();
00533 
00534   double D = 0.0F;
00535 
00536   if(useGause == false)
00537   {
00538     while(p1 != point1->end())
00539     {
00540       D += pow(((*p1 - *p2)),2);
00541       ++p1; ++p2; ++b;
00542     }
00543     D = 1/sqrt(D);
00544   }
00545   else
00546   {
00547     while(p1 != point1->end())
00548     {
00549       D += gauss(*p1,*p2,(*b/8));
00550       ++p1; ++p2; ++b;
00551     }
00552     D = 1/D;
00553   }
00554   return D;
00555 }
00556 
00557 /*********************************************************************/
00558 
00559 #if 0
00560 // FIXME this function doesn't compile cleanly
00561 template <class T>
00562 void covEstimate<T>::matchPmean(std::vector<covHolder<T> > *chNew,
00563                                 long sizeNew,
00564                                 std::vector<covHolder<T> > *chOld,
00565                                 long sizeOld, long minSize)
00566 {
00567   typename std::vector<covHolder<T> >::iterator chNewi;
00568   typename std::vector<covHolder<T> >::iterator chOldi;
00569   std::vector<bool> reserveTable;
00570   reserveTable.resize(sizeNew,false);
00571   chNewi = chNew->begin();
00572   for(long ii = 0; ii < sizeNew; ii++, ++chNewi)
00573   {
00574     chNewi->isMatched = false;
00575   }
00576   chOldi = chOld->begin();
00577   for(long  jj = 0; jj < sizeOld; jj++, ++chOldi)
00578   {
00579     chOldi->isMatched = false;
00580   }
00581 
00582 
00583   for(int i = 0; i < sizeNew; i++)
00584   {
00585     chNewi = chNew->begin();
00586     double bestD = 0.0F;
00587     unsigned long bestClassO = 0;
00588     unsigned long bestClassN = 0;
00589     bool setThis = false;
00590     for(int ii = 0; ii < sizeNew; ii++, ++chNewi)
00591     {
00592       if(chNewi->isMatched == false)
00593       {
00594         if(chNewi->isLarge == true)
00595         {
00596           chOldi = chOld->begin();
00597           for(int jj = 0; jj < sizeOld; jj++, ++chOldi)
00598           {
00599             if(chOldi->isMatched == false)
00600             {
00601               if(chOldi->isLarge == true)
00602               {
00603                 double D = getD(&(chNewi->mean),&(chOldi->mean),
00604                                 &(chOldi->bias),false);
00605                 LINFO("D %f for %d and %d",D,ii,jj);
00606                 if(D > bestD)
00607                 {
00608                   bestD = D;
00609                   bestClassN = ii;
00610                   bestClassO = jj;
00611                   setThis = true;
00612                   LINFO("SET BEST D");
00613                   for(unsigned int m = 0; m < chNewi->dim; m++)
00614                   {
00615                     LINFO("MATCH : %f to %f",chNewi->mean[m],chOldi->mean[m]);
00616                   }
00617                 }
00618               }
00619             }
00620           }
00621         }
00622       }
00623     }
00624     if(setThis == true)
00625     {
00626       //LINFO("FINAL %d MATCHED TO %d",bestClassN,bestClassO);
00627       //LINFO("NEW matchID %d"
00628       //            ,chOld->at(bestClassO).matchID);
00629       //LINFO("SAMPLES NEW %d OLD %d",chNew->at(bestClassN).samples,
00630       //            chOld->at(bestClassO).samples);
00631       //for(unsigned int m = 0; m < chNew->at(bestClassN).dim; m++)
00632       //{
00633       //        LINFO("FINAL MATCH : %f to %f",chNew->at(bestClassN).mean[m]
00634       //              ,chOld->at(bestClassO).mean[m]);
00635       //}
00636       // how long has this object been alive for?
00637       if(chNew->at(bestClassN).matchID == chOld->at(bestClassO).matchID)
00638       {
00639         chNew->at(bestClassN).lifeSpan = chOld->at(bestClassO).lifeSpan + 1;
00640         // compute 1st and 2nd order partial derivatives for this objects
00641         // features as well as the partial first order integral
00642         for(unsigned int m = 0; m < chNew->at(bestClassN).dim; m++)
00643         {
00644           chNew->at(bestClassN).speed[m] = chOld->at(bestClassO).mean[m] -
00645             chNew->at(bestClassN).mean[m];
00646 
00647           chNew->at(bestClassN).avgspeed[m] =
00648             (chOld->at(bestClassO).avgspeed[m] +
00649              chNew->at(bestClassN).speed[m])/2;
00650 
00651           chNew->at(bestClassN).accel[m] = chOld->at(bestClassO).speed[m] -
00652             chNew->at(bestClassN).speed[m];
00653 
00654           chNew->at(bestClassN).avgaccel[m] =
00655             (chOld->at(bestClassO).avgaccel[m] +
00656              chNew->at(bestClassN).accel[m])/2;
00657 
00658           chNew->at(bestClassN).distance[m] += chNew->at(bestClassN).speed[m];
00659         }
00660       }
00661       else
00662       {
00663         if(chNew->at(bestClassN).lifeSpan != 0)
00664         {
00665           chNew->at(bestClassN).lifeSpan = 0;
00666           // reset derivatives and integrals if we are new
00667           for(unsigned int m = 0; m < chNew->at(bestClassN).dim; m++)
00668           {
00669             chNew->at(bestClassN).speed = 0;
00670             chNew->at(bestClassN).accel = 0;
00671             chNew->at(bestClassN).distance = 0;
00672           }
00673         }
00674       }
00675 
00676       chNew->at(bestClassN).matchID = chOld->at(bestClassO).matchID;
00677       chNew->at(bestClassN).isMatched = true;
00678       chOld->at(bestClassO).isMatched = true;
00679       if(chNew->at(bestClassN).matchID < reserveTable.size())
00680         reserveTable[chNew->at(bestClassN).matchID] = true;
00681     }
00682   }
00683 
00684   // if some classes are left over, assign them their own class
00685 
00686   if(sizeNew > sizeOld)
00687   {
00688     typename std::vector<bool>::iterator rTable;
00689     unsigned long last = 0;
00690     chNewi = chNew->begin();
00691     for(int ii = 0; ii < sizeNew; ii++, ++chNewi)
00692     {
00693       if(chNewi->isMatched == false)
00694       {
00695         unsigned long cc = last;
00696         for(rTable = reserveTable.begin() + last; rTable != reserveTable.end();
00697             ++rTable, cc++)
00698         {
00699           if(*rTable == false)
00700           {
00701             chNewi->isMatched = true;
00702             chNewi->matchID = cc;
00703             last = cc;
00704             //LINFO("%d SET BUT NOT MATCHED AS %d",ii,cc);
00705             *rTable = true;
00706             break;
00707           }
00708         }
00709       }
00710     }
00711   }
00712 }
00713 #endif
00714 
00715 /*********************************************************************/
00716 
00717 template <class T>
00718 void covEstimate<T>::matchPmeanAccum(std::vector<covHolder<T> > *ch,
00719                                      unsigned int *sizeCH,
00720                                      std::vector<covHolder<T> > *accum,
00721                                      unsigned int *sizeAccum,
00722                                      long minSize)
00723 {
00724 
00725   // For storage, see covHolder.H to see where all this stuff is going
00726 
00727   typename std::vector<covHolder<T> >::iterator chi;
00728   typename std::vector<covHolder<T> >::iterator accumi;
00729 
00730   chi = ch->begin();
00731   for(long  jj = 0; jj < (signed)*sizeCH; jj++, ++chi)
00732   {
00733     chi->isMatched = false;
00734   }
00735 
00736   accumi = accum->begin();
00737   for(long  jj = 0; jj < (signed)*sizeAccum; jj++, ++accumi)
00738   {
00739     accumi->isMatched = false;
00740   }
00741 
00742   // Do a stupid N^2 match of every class to every class to find the best
00743   // matches. Match the first best (closest pair) then remove them. Then
00744   // match the second best until matching cannot be further completed.
00745   // This is sub optimal in complexity, but the numbers are so small it
00746   // is not worth my time to optimize this part of the code (total number
00747   // of classes < 20 most of the time)
00748 
00749   for(int i = 0; i < (signed)*sizeAccum; i++)
00750   {
00751     accumi = accum->begin();
00752     double bestD = 0.0F;
00753     unsigned long bestClassO = 0;
00754     unsigned long bestClassN = 0;
00755     bool setThis = false;
00756     for(int ii = 0; ii < (signed)*sizeAccum; ii++, ++accumi)
00757     {
00758       if(accumi->isMatched == false)
00759       {
00760         if(accumi->isLarge == true)
00761         {
00762           chi = ch->begin();
00763           for(int jj = 0; jj < (signed)*sizeCH; jj++, ++chi)
00764           {
00765             if(chi->isMatched == false)
00766             {
00767               if(chi->isLarge == true)
00768               {
00769                 // compute distance between classes
00770                 double D = getD(&(accumi->mean),&(chi->mean),
00771                                 &(chi->bias),false);
00772                 if(D > bestD)
00773                 {
00774                   bestD = D;
00775                   bestClassN = ii;
00776                   bestClassO = jj;
00777                   setThis = true;
00778                 }
00779               }
00780             }
00781           }
00782         }
00783       }
00784     }
00785 
00786     // We have found a good match for this class from the last iteration
00787     // copy over some properties, compute running averages etc. incrememnt
00788     // life span.
00789 
00790     if(setThis == true)
00791     {
00792       // how long has this object been alive for?
00793       accum->at(bestClassN).lifeSpan++;
00794       // compute first and second order derivatives over features
00795       // as well as averages and
00796       for(unsigned int m = 0; m < accumi->dim; m++)
00797       {
00798         ch->at(bestClassO).speed[m] = ch->at(bestClassO).mean[m] -
00799           accum->at(bestClassN).mean[m];
00800 
00801         ch->at(bestClassO).accel[m] = ch->at(bestClassO).speed[m] -
00802             accum->at(bestClassN).speed[m];
00803 
00804         ch->at(bestClassO).distance[m] = ch->at(bestClassO).speed[m]
00805           + accum->at(bestClassN).distance[m];
00806 
00807         ch->at(bestClassO).avgspeed[m] =
00808           (ch->at(bestClassO).speed[m] +
00809            accum->at(bestClassN).avgspeed[m])/2;
00810 
00811         ch->at(bestClassO).avgaccel[m] =
00812           ((ch->at(bestClassO).avgspeed[m] -
00813             accum->at(bestClassN).avgspeed[m])
00814            + accum->at(bestClassN).avgaccel[m])/2;
00815       }
00816       ch->at(bestClassO).matchID = accum->at(bestClassN).matchID;
00817       ch->at(bestClassO).isMatched = true;
00818       ch->at(bestClassO).lifeSpan = accum->at(bestClassN).lifeSpan;
00819       accum->at(bestClassN) = ch->at(bestClassO);
00820     }
00821   }
00822 
00823   // reset any unmatched classes
00824   for(int i = 0; i < (signed)*sizeAccum; i++)
00825   {
00826     // no match was found for this class, therefor we reset some parts
00827     // we only decay some of the running averages. We hold onto some
00828     // values incase it turns out that it matches next iteration
00829 
00830     if(accum->at(i).isMatched == false)
00831     {
00832       accum->at(i).lifeSpan = 0;
00833       // reset absolute speed and accel, but decay averages
00834       for(unsigned int m = 0; m < accum->at(i).dim; m++)
00835       {
00836         accum->at(i).speed[m] = 0;
00837         accum->at(i).accel[m] = 0;
00838         accum->at(i).distance[m] = 0;
00839         accum->at(i).avgspeed[m] = accum->at(i).avgspeed[m]/2;
00840         accum->at(i).avgaccel[m] = ((accum->at(i).avgspeed[m] -
00841                                 accum->at(i).avgspeed[m])
00842                                + accum->at(i).avgaccel[m])/2;
00843       }
00844     }
00845   }
00846 
00847   // if some classes are left over, assign them their own class
00848   // As such, this iteration we may find more classes then in the last
00849   // iteration. As such, we cannot throw them away, but insted assign them
00850   // their own class
00851 
00852   if(*sizeCH > *sizeAccum)
00853   {
00854     //unsigned long last = *sizeAccum;
00855     chi = ch->begin();
00856     for(int ii = 0; ii < (signed)*sizeCH; ii++, ++chi)
00857     {
00858       if(chi->isMatched == false)
00859       {
00860         if(chi->isLarge == true)
00861         {
00862           chi->isMatched = true;
00863           chi->matchID = *sizeAccum;
00864           accum->at(*sizeAccum) = *chi;
00865           *sizeAccum = *sizeAccum + 1;
00866           //LINFO("%d SET BUT NOT MATCHED AS %d",ii,*sizeAccum);
00867         }
00868       }
00869     }
00870   }
00871 }
00872 
00873 /*********************************************************************/
00874 
00875 template <class T>
00876 T covEstimate<T>::gauss(T X, T mu, T std)
00877 {
00878   return (1/(sqrt(2*3.14159*pow(std,2))))
00879   *exp(-1*(pow((X - mu),2)/(2*pow(std,2))));
00880 }
00881 
00882 /*********************************************************************/
00883 
00884 template <class T>
00885 void covEstimate<T>::run()
00886 {
00887   //LINFO("FIND MEAN");
00888   findMean();
00889   //LINFO("FIND MIN and MAX");
00890   findMinMax();
00891   //LINFO("TRANSLATE SPACE");
00892   translateSpace();
00893   //LINFO("FIND ROTATIOS");
00894   findRotatios(true);
00895   //LINFO("FIND VARAINCE");
00896   findVariance();
00897 }
00898 
00899 /*********************************************************************/
00900 template <class T>
00901 void covEstimate<T>::setNew(std::vector<std::vector<T> > &_inputSpace,
00902                             bool doResize, covHolder<T> &_ch, T initVal)
00903 {
00904   cov_covHolder       = &_ch;
00905   cov_usePtr          = false;
00906   cov_useFloatSpecial = false;
00907 
00908   cov_inputSpace      = &(_inputSpace);
00909   cov_dim             = cov_inputSpace->size();
00910   cov_samples         = cov_inputSpace->at(0).size();
00911 
00912   if((doResize == true) || (_inputSpace.size() != cov_transSpace.size()))
00913   {
00914     resize(_inputSpace.size(),_inputSpace[0].size(),initVal);
00915     cov_covHolder->samples = _inputSpace[0].size();
00916   }
00917 }
00918 
00919 /*********************************************************************/
00920 
00921 template <class T>
00922 void covEstimate<T>::setNew(std::vector<std::vector<T*> > &_inputSpace,
00923                             bool doResize, covHolder<T> &_ch, T initVal)
00924 {
00925   cov_covHolder       = &_ch;
00926   cov_usePtr          = true;
00927   cov_useFloatSpecial = false;
00928 
00929   cov_inputSpacePtr   = &(_inputSpace);
00930   cov_dim             = cov_inputSpace->size();
00931   cov_samples         = cov_inputSpace->at(0).size();
00932 
00933   if((doResize == true) || (_inputSpace.size() != cov_transSpace.size()))
00934   {
00935     resize(_inputSpace.size(),_inputSpace[0].size(),initVal);
00936     cov_covHolder->samples = _inputSpace[0].size();
00937   }
00938 }
00939 
00940 /*********************************************************************/
00941 
00942 template <class T>
00943 void covEstimate<T>::setNew(std::vector<std::vector<T> > &_inputSpace,
00944                             T initVal, int samples, int dim, covHolder<T> &_ch,
00945                             bool _resize = false)
00946 {
00947   cov_covHolder          = &_ch;
00948   cov_usePtr             = false;
00949   cov_useFloatSpecial    = false;
00950 
00951   cov_inputSpace         = &(_inputSpace);
00952   cov_dim                = dim;
00953   cov_samples            = samples;
00954   cov_covHolder->samples = samples;
00955 
00956   if(_resize == true)
00957     resize(dim,samples,initVal);
00958 
00959 }
00960 
00961 /*********************************************************************/
00962 
00963 template <class T>
00964 void covEstimate<T>::setNew(std::vector<std::vector<T*> > &_inputSpace,
00965                             T initVal, int samples, int dim, covHolder<T> &_ch,
00966                             bool _resize = false)
00967 {
00968   cov_covHolder          = &_ch;
00969   cov_usePtr             = true;
00970   cov_useFloatSpecial    = false;
00971 
00972   cov_inputSpacePtr      = &(_inputSpace);
00973   cov_dim                = dim;
00974   cov_samples            = samples;
00975   cov_covHolder->samples = samples;
00976 
00977   if(_resize == true)
00978     resize(dim,samples,initVal);
00979 
00980   cov_covHolder->samples = samples;
00981 
00982 }
00983 
00984 /*********************************************************************/
00985 
00986 template <class T>
00987 void covEstimate<T>::setNewF(std::vector<std::vector<float*> > &_inputSpace,
00988                             T initVal, int samples, int dim, covHolder<T> &_ch,
00989                             bool _resize = false)
00990 {
00991   cov_covHolder          = &_ch;
00992   cov_usePtr             = true;
00993   cov_useFloatSpecial    = true;
00994 
00995   cov_FinputSpacePtr     = &(_inputSpace);
00996   cov_dim                = dim;
00997   cov_samples            = samples;
00998   cov_covHolder->samples = samples;
00999 
01000   if(_resize == true)
01001     resize(dim,samples,initVal);
01002 
01003   cov_covHolder->samples = samples;
01004 
01005 }
01006 
01007 /*********************************************************************/
01008 
01009 template <class T>
01010 void covEstimate<T>::resize(unsigned int _dimensions, unsigned int _samples,
01011                             T zero)
01012 {
01013   unsigned int dimensions = _dimensions;
01014   unsigned int samples = _samples;
01015   cov_tempTS.resize(samples,zero);
01016   cov_transSpace.resize(dimensions,cov_tempTS);
01017   cov_rotSpace.resize(dimensions,cov_tempTS);
01018   cov_meanTranslate.resize(dimensions,zero);
01019   cov_meanRot.resize(dimensions,zero);
01020   cov_sumSquare.resize(dimensions,zero);
01021   cov_list_i.resize(((int)((pow((float)dimensions,2)/2)+dimensions)),0);
01022   cov_list_j.resize(((int)((pow((float)dimensions,2)/2)+dimensions)),0);
01023   cov_listSize = 0;
01024   cov_currentStep = 0.5F;
01025 }
01026 
01027 /*********************************************************************/
01028 
01029 template <class T>
01030 void covEstimate<T>::printDebug()
01031 {
01032   int doThis = (int)cov_currentStep*10;
01033   switch(doThis)
01034   {
01035   case 5 : std::cerr << "SPACE RESIZED D:" << cov_transSpace.size()
01036                          << " S:" << cov_transSpace[1].size()
01037                          << " List Size:" << cov_list_i.size()
01038                          << " Data Zero:" << cov_transSpace[1][1]
01039                          << "\n"; break;
01040   case 10 : std::cerr << "Mean values: \n";
01041     if(cov_usePtr)
01042       for(unsigned int i = 0; i < cov_dim; i++)
01043       {
01044         std::cerr << i << " : " << cov_covHolder->mean[i] << "\n";
01045       }
01046     else
01047       for(unsigned int i = 0; i < cov_dim; i++)
01048       {
01049         std::cerr << i << " : " << cov_covHolder->mean[i] << "\n";
01050       }
01051     break;
01052   case 20 : std::cerr << "Space translated\n"; break;
01053   case 30 : std::cerr << "Space Ratios\n";
01054       for(unsigned int i = 0; i < cov_transSpace.size(); i++)
01055       {
01056         for(unsigned int j = 0; j < cov_transSpace.size(); j++)
01057         {
01058           std::cerr << cov_covHolder->meanRatio[i][j] << "\t";
01059         }
01060         std::cerr << "\n";
01061       }
01062       std::cerr << "\n";
01063       std::cerr << "Space Angles\n";
01064       for(unsigned int i = 0; i < cov_transSpace.size(); i++)
01065       {
01066         for(unsigned int j = 0; j < cov_transSpace.size(); j++)
01067         {
01068           std::cerr << cov_covHolder->meanTheta[i][j] << "\t";
01069         }
01070         std::cerr << "\n";
01071       }
01072       std::cerr << "\n";
01073       break;
01074   case 40 :  std::cerr << "Cov Eigen\n";
01075     for(unsigned int i = 0; i < cov_dim; i++)
01076     {
01077       std::cerr << cov_covHolder->eigenVal[i] << "\t";
01078     }
01079     std::cerr << "\n";
01080     break;
01081   default: std::cerr << "No debug info on state\n" ; break;
01082   }
01083 }
01084 
01085 /*********************************************************************/
01086 
01087 template <class T>
01088 void covEstimate<T>::printEigenVals()
01089 {
01090   std::cout << "EIGEN VALUES\n";
01091   for(unsigned int i = 0; i < cov_dim; i++)
01092   {
01093     std::cout << i << ":\t" << cov_covHolder->eigenVal[i] << "\n";
01094   }
01095   std::cout << "RATIOS\n";
01096   for(unsigned int i = 0; i < cov_dim; i++)
01097   {
01098     for(unsigned int j = 0; j < cov_dim; j++)
01099     {
01100       std::cout << cov_covHolder->meanRatio[i][j] << "\t";
01101     }
01102     std::cout << "\n";
01103   }
01104 }
01105 
01106 /*********************************************************************/
01107 
01108 template <class T>Image<float> covEstimate<T>::returnMatrixSlice(int dim1,
01109                                                                  int dim2,
01110                                                                  int spaceSize)
01111 {
01112   Image<float> output;
01113   output.resize(spaceSize,spaceSize);
01114   T temp1 = 0;
01115   T temp2 = 0;
01116 
01117   if(cov_currentStep >= 2)
01118   {
01119     temp1 = cov_covHolder->mean[dim1];
01120     temp2 = cov_covHolder->mean[dim2];
01121     drawLine(output, Point2D<int>((spaceSize-(int)temp1),0),
01122              Point2D<int>((spaceSize-(int)temp1),spaceSize),128.0F,1);
01123     drawLine(output, Point2D<int>(0,(int)temp2),
01124              Point2D<int>(spaceSize,(int)temp2),128.0F,1);
01125 
01126   }
01127 
01128   for(unsigned int i = 0; i < cov_samples; i++)
01129   {
01130     output.setVal(spaceSize-((int)cov_transSpace[dim1][i] + (int)temp1)
01131                     ,(int)cov_transSpace[dim2][i] + (int)temp2,255.0F);
01132   }
01133   return output;
01134 }
01135 
01136 
01137 /*********************************************************************/
01138 
01139 template <class T>
01140 Image<float> covEstimate<T>::returnCovSlice(int dim1, int dim2,
01141                                             int spaceSize,
01142                                             bool inorm)
01143 {
01144   Image<float> output;
01145   output.resize(spaceSize,spaceSize);
01146   return returnCovSlice(dim1,dim2,output,inorm);
01147 }
01148 
01149 /*********************************************************************/
01150 
01151 template <class T>
01152 Image<float> covEstimate<T>::returnCovSlice(int dim1, int dim2,
01153                                             Image<float> &output,
01154                                             bool inorm)
01155 {
01156   LINFO("Creating cov Slice Image");
01157   ASSERT(output.getWidth() == output.getHeight());
01158   int spaceSize = output.getWidth();
01159 
01160   T norm = 1;
01161   T Xtrans = 0;
01162   T Ytrans = 0;
01163   if(cov_usePtr == true)
01164   {
01165     LINFO("DIMS %d %d", dim1,dim2);
01166 
01167     LINFO("IMAGE SIZE %d %d",output.getWidth(),output.getHeight());
01168     if(inorm == true)
01169     {
01170       norm = spaceSize;
01171       T maxX = 0;
01172       T minX = *cov_inputSpacePtr->at(dim1)[0];
01173       T maxY = 0;
01174       T minY = *cov_inputSpacePtr->at(dim2)[0];
01175       for(unsigned int i = 0; i < cov_samples; i++)
01176       {
01177         T temp1 = *cov_inputSpacePtr->at(dim1)[i];
01178         if(temp1 < minX)
01179           minX = temp1;
01180         if(temp1 > maxX)
01181           maxX = temp1;
01182 
01183         T temp2 = *cov_inputSpacePtr->at(dim2)[i];
01184         if(temp2 < minY)
01185           minY = temp2;
01186         if(temp2 > maxY)
01187           maxY = temp2;
01188       }
01189       T tx = maxX - minX;
01190       T ty = maxY - minY;
01191       if(tx > ty)
01192         norm = ((spaceSize-1)/tx)*0.9F;
01193       else
01194         norm = ((spaceSize-1)/ty)*0.9F;
01195       T h1 = 0; T h2 = 0;
01196       for(unsigned int i = 0; i < cov_samples; i++)
01197       {
01198         T temp1 = norm * (*cov_inputSpacePtr->at(dim1)[i]);
01199         T temp2 = norm * (*cov_inputSpacePtr->at(dim2)[i]);
01200         if(temp1 < h1)
01201           h1 = temp1;
01202         if(temp2 < h2)
01203           h2 = temp2;
01204       }
01205       Xtrans = fabs(h1);
01206       Ytrans = fabs(h2);
01207     }
01208     LINFO("NORMALIZER %f",norm);
01209     LINFO("Xtrans %f Ytrans %f",Xtrans,Ytrans);
01210     for(unsigned int i = 0; i < cov_samples; i++)
01211     {
01212       LINFO("SETTING %d %d",
01213             (spaceSize-1)-((int)(Xtrans+
01214                                  (norm*(*cov_inputSpacePtr->at(dim1)[i])))),
01215             ((int)(Ytrans+(norm*(*cov_inputSpacePtr->at(dim2)[i])))));
01216 
01217       drawPoint(output, (spaceSize-1)
01218                 -((int)(Xtrans+(norm*(*cov_inputSpacePtr->at(dim1)[i]))))
01219                 ,((int)(Ytrans+(norm*(*cov_inputSpacePtr->at(dim2)[i])))),128.0F);
01220     }
01221   }
01222   else
01223   {
01224     for(unsigned int i = 0; i < cov_samples; i++)
01225     {
01226       drawPoint(output, spaceSize-((int)cov_inputSpace->at(dim1)[i])
01227                 ,(int)(cov_inputSpace->at(dim2)[i]),128.0F);
01228     }
01229   }
01230 
01231   // ************************** now draw eigen vector and values
01232 
01233   T ortho, Rise1, Run1, Rise2, Run2;
01234 
01235   if(cov_covHolder->meanRatio[dim1][dim2] != 0)
01236     ortho = -1/cov_covHolder->meanRatio[dim1][dim2];
01237   else
01238   {
01239     ortho =-1/0.0000001;
01240     std::cout << "Cov matrix 1 is 0\n";
01241   }
01242 
01243   if(cov_covHolder->eigenVal[dim1] != 0)
01244     Rise1 = 1/sqrt((1+pow(cov_covHolder->meanRatio[dim1][dim2],2))
01245                    /pow(cov_covHolder->eigenVal[dim1],2));
01246   else
01247   {
01248     Rise1 = 1/sqrt((1+pow(cov_covHolder->meanRatio[dim1][dim2],2))
01249                    /pow(0.0000001,2));
01250     std::cout << "Cov matrix 2 is 0\n";
01251   }
01252   Run1 = (pow(cov_covHolder->eigenVal[dim1],2)
01253                 - pow(Rise1,2));
01254   if(Run1 > 0)
01255     Run1 = sqrt(Run1);
01256   else
01257   {
01258     Run1 = 0;
01259     std::cout << "Cov matrix 3 is 0\n";
01260   }
01261   if(cov_covHolder->eigenVal[dim2] != 0)
01262     Rise2 = 1/sqrt((1+pow(ortho,2))
01263                    /pow(cov_covHolder->eigenVal[dim2],2));
01264   else
01265   {
01266     Rise2 = 1/sqrt((1+pow(ortho,2))
01267                    /pow(0.0000001,2));
01268     std::cout << "Cov matrix 4 is 0\n";
01269   }
01270   T temp = pow(cov_covHolder->eigenVal[dim2],2) - pow(Rise2,2);
01271   if(temp > 0)
01272     Run2 = sqrt(temp);
01273   else
01274     Run2 = 0;
01275 
01276   LINFO("Ytrans %f Xtrans %f norm %f",Ytrans,Xtrans,norm);
01277   std::cerr << Run1 << " " << Rise1 << " " << Run2 << " " << Rise2 << "\n";
01278 
01279   int P1a = (int)(Ytrans+norm*(cov_covHolder->mean[dim2] + Run1));
01280   int P1b = (int)(spaceSize - (Xtrans+norm*((cov_covHolder->mean[dim1])
01281                                             + Rise1)));
01282   int P2a = (int)(Ytrans+norm*(cov_covHolder->mean[dim2] - Run1));
01283   int P2b = (int)(spaceSize - (Xtrans+norm*((cov_covHolder->mean[dim1])
01284                                             - Rise1)));
01285 
01286   int P3a = (int)(Ytrans+norm*(cov_covHolder->mean[dim2] - Run2));
01287   int P3b = (int)(spaceSize - (Xtrans+norm*((cov_covHolder->mean[dim1])
01288                                             - Rise2)));
01289   int P4a = (int)(Ytrans+norm*(cov_covHolder->mean[dim2] + Run2));
01290   int P4b = (int)(spaceSize - (Xtrans+norm*((cov_covHolder->mean[dim1])
01291                                             + Rise2)));
01292 
01293   LINFO("######-> %d,%d to %d %d", P1b,P1a,P2b,P2a);
01294   LINFO("######-> %d,%d to %d %d", P3a,P3b,P4a,P4b);
01295   char size[20];
01296   sprintf(size,"%f",norm);
01297   writeText(output, Point2D<int>(1,1),size,0.0f);
01298 
01299   drawLine(output, Point2D<int>((int)P1b,(int)P1a),
01300            Point2D<int>((int)P2b,(int)P2a),255.0f,1);
01301   drawLine(output, Point2D<int>((int)P3b,(int)P4a),
01302            Point2D<int>((int)P4b,(int)P3a),255.0f,1);
01303   LINFO("DONE");
01304   return output;
01305 }
01306 
01307 /*********************************************************************/
01308 
01309 template <class T>
01310 void covEstimate<T>::dumpMatrix(std::string fileName,int index, std::string ID)
01311 {
01312   std::string eigFile, rotFile, meanFile;
01313   eigFile  = fileName + ".EIG.out.txt";
01314   rotFile  = fileName + ".ROT.out.txt";
01315   meanFile = fileName + ".MEAN.out.txt";
01316   std::ofstream eig(eigFile.c_str(),std::ios::app);
01317   std::ofstream rot(rotFile.c_str(),std::ios::app);
01318   std::ofstream mean(meanFile.c_str(),std::ios::app);
01319   mean  << ID << "\t" << index << "\t";
01320   eig   << ID << "\t" << index << "\t";
01321   mean  << cov_covHolder->samples << "\t";
01322   rot   << ID << "\t" << index << "\n";
01323   for(unsigned int i = 0; i < cov_covHolder->dim; i++)
01324   {
01325     mean << cov_covHolder->mean[i] << "\t";
01326     eig << cov_covHolder->eigenVal[i] << "\t";
01327     for(unsigned int j = 0; j < cov_covHolder->dim; j++)
01328     {
01329       rot << cov_covHolder->meanRatio[i][j] << "\t";
01330     }
01331     rot << "\n";
01332   }
01333   mean << "\n";
01334   eig << "\n";
01335   rot.close();
01336   mean.close();
01337   eig.close();
01338 
01339 }
01340 
01341 
01342 /*********************************************************************/
01343 
01344 template <class T>
01345 void covEstimate<T>::dumpMatrix(std::string fileName, covHolder<T> &_ch, int index,
01346                                 std::string ID)
01347 {
01348   cov_covHolder = &_ch;
01349   dumpMatrix(fileName,index,ID);
01350 }
01351 
01352 /*********************************************************************/
01353 
01354 template <class T>
01355 unsigned long covEstimate<T>::getSampleSize()
01356 {
01357   return cov_samples;
01358 }
01359 
01360 
01361 template class covEstimate<float>;
01362 template class covEstimate<double>;
Generated on Sun May 8 08:42:33 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3