test-img.C

Go to the documentation of this file.
00001 /*!@file RCBot/test-img.C
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: Lior Elazary <elazary@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/RCBot/test-img.C $
00036 // $Id: test-img.C 13993 2010-09-20 04:54:23Z itti $
00037 //
00038 
00039 #include "Image/OpenCVUtil.H"
00040 #include "Component/ModelManager.H"
00041 #include "Image/Image.H"
00042 #include "Image/Pixels.H"
00043 #include "Image/ColorOps.H"
00044 #include "Image/ShapeOps.H"
00045 #include "Image/MathOps.H"
00046 #include "Image/CutPaste.H"
00047 #include "Image/Transforms.H"
00048 #include "Media/FrameSeries.H"
00049 #include "Raster/Raster.H"
00050 #include "Util/log.H"
00051 #include "Util/Timer.H"
00052 #include "Learn/SOFM.H"
00053 #include "GUI/XWinManaged.H"
00054 #include "CMapDemo/SaliencyCMapMT.H"
00055 #include "SIFT/ScaleSpace.H"
00056 #include "SIFT/VisualObject.H"
00057 #include "SIFT/Keypoint.H"
00058 #include "SIFT/VisualObjectDB.H"
00059 #include "Image/FourierEngine.H"
00060 #include "RCBot/Motion/MotionEnergy.H"
00061 
00062 #include <signal.h>
00063 
00064 
00065 //! Number of frames over which average framerate is computed
00066 #define NAVG 20
00067 
00068 static bool goforever = true;
00069 //! Signal handler (e.g., for control-C)
00070 void terminate(int s)
00071 { LERROR("*** INTERRUPT ***"); goforever = false; exit(0);}
00072 
00073 ImageSet<float> bias(14);
00074 ImageSet<float> cmap1(14);
00075 ImageSet<float> cmap2(14);
00076 
00077 struct        KeyPoint {
00078         int x;
00079         int y;
00080         float val;
00081 };
00082 
00083 #define ROI_SIZE 10
00084 std::vector<KeyPoint>* getKeypoints(Image<float> &ima){
00085         Image<float> wima = ima; //Copy the image for debuging. Could be removed when actuly running because we dont
00086         //care about the image any more.
00087         std::vector<KeyPoint> *keypoints = new std::vector<KeyPoint>;
00088 
00089         //Try PCA
00090 
00091         //get the 10 most intersting keypoints and thier relationship
00092         for(int i=0; i<10; i++){
00093 
00094                  float val; Point2D<int> winner; findMax(wima, winner, val);
00095                  KeyPoint key;
00096                  key.x = winner.i;
00097                  key.y = winner.j;
00098                  key.val = val;
00099                  keypoints->push_back(key);
00100 
00101                  //IOR
00102                  drawDisk(wima, winner, ROI_SIZE, 0.0F);
00103 
00104         }
00105 
00106 
00107         return keypoints;
00108 
00109 }
00110 
00111 
00112 double compImg(std::vector< rutz::shared_ptr<Keypoint> > &obj1,
00113                 std::vector< rutz::shared_ptr<Keypoint> > &obj2){
00114 
00115 
00116         if (obj1.size() != obj2.size()){
00117                 LINFO("Objects size dont match %"ZU" %"ZU, obj1.size(), obj2.size());
00118                 //return 999999;
00119         }
00120 
00121         std::vector<double> objM1;
00122         std::vector<double> objM2;
00123         std::vector<double> objDiff;
00124 
00125 
00126         //map cordinates
00127         if (obj1.size() > obj2.size()){
00128                 for(unsigned int i=0; i<obj2.size(); i++){
00129                         objM1.push_back(obj1[i]->getO()*360+obj1[i]->getY()*160+obj1[i]->getX());
00130                         objM2.push_back(obj2[i]->getO()*360+obj2[i]->getY()*160+obj2[i]->getX());
00131                 }
00132         } else {
00133                 for(unsigned int i=0; i<obj1.size(); i++){
00134                         objM1.push_back(obj1[i]->getO()*360+obj1[i]->getY()*160+obj1[i]->getX());
00135                         objM2.push_back(obj2[i]->getO()*360+obj2[i]->getY()*160+obj2[i]->getX());
00136                 }
00137         }
00138 
00139 
00140         //sort the arrays
00141         std::sort(objM1.begin(), objM1.end());
00142         std::sort(objM2.begin(), objM2.end());
00143 
00144         //find the diffrance
00145         for(unsigned int i=0; i<objM1.size(); i++){
00146                 objDiff.push_back(fabs(objM1[i] - objM2[i]));
00147         }
00148 
00149 
00150         printf("M1: ");
00151         for(unsigned int i=0; i<objM1.size(); i++){
00152                 printf("%f ", objM1[i]);
00153         }
00154         printf("\n");
00155 
00156         printf("M2: ");
00157         for(unsigned int i=0; i<objM2.size(); i++){
00158                 printf("%f ", objM2[i]);
00159         }
00160         printf("\n");
00161 
00162         printf("Diff: ");
00163         for(unsigned int i=0; i<objDiff.size(); i++){
00164                 printf("%f ", objDiff[i]);
00165         }
00166         printf("\n");
00167 
00168         //compute the relationship
00169 
00170         double sum = 0;
00171         printf("Delta: ");
00172         for(unsigned int i=0; i<objDiff.size()-1; i++){
00173                 double diff = fabs(objDiff[i]-objDiff[i+1]);
00174                 sum += diff;
00175                 printf("%f ", diff);
00176         }
00177         printf("\n");
00178 
00179 
00180         LINFO("Diffrance %f", sum);
00181 
00182         return sum;
00183 }
00184 
00185 
00186 
00187 int main(int argc, char** argv)
00188 {
00189 
00190         MYLOGVERB = LOG_INFO;  // suppress debug messages
00191 
00192         CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00193 
00194         // Instantiate a ModelManager:
00195         ModelManager manager("Test SOFM");
00196 
00197         // Instantiate our various ModelComponents:
00198         nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00199         manager.addSubComponent(ifs);
00200 
00201 
00202         nub::ref<SaliencyMT> smt(new SaliencyMT(manager, orb, 0));
00203         manager.addSubComponent(smt);
00204 
00205         // Parse command-line:
00206         if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false)
00207                 return(1);
00208 
00209         //catch signals and redirect them to terminate for clean exit:
00210         signal(SIGHUP, terminate); signal(SIGINT, terminate);
00211         signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00212         signal(SIGALRM, terminate);
00213 
00214 
00215         // let's get all our ModelComponent instances started:
00216         manager.start();
00217 
00218         Timer masterclock;                // master clock for simulations
00219 
00220 
00221         masterclock.reset();
00222         ifs->update(masterclock.getSimTime());
00223         const Image< PixRGB<byte> > input = ifs->readRGB();
00224         const int w = input.getWidth();
00225         const int h = input.getHeight();
00226 
00227         LINFO("Input %ix%i", w, h);
00228         XWinManaged xwin2(Dims(256*3, 256), -1, -1, "SOFM Output");
00229         XWinManaged xwin(Dims(256*3, 256), -1, -1, "SOFM Output");
00230 
00231         // main loop:
00232         int ii=0;
00233 
00234         uint64 avgtime = 0; int avgn = 0; // for average framerate
00235         float fps = 0.0F;                 // to display framerate
00236         Timer tim;                        // for computation of framerate
00237 
00238         char info[1000];  // general text buffer for various info messages
00239         sprintf(info, "----");
00240 
00241         Image<PixRGB<byte> > disp(256*3, 256, NO_INIT);
00242         Image<PixRGB<byte> > disp2(256*3, 256, NO_INIT);
00243 
00244 
00245         //Get the image
00246         //Compute Saliency Map
00247         Image<PixRGB<byte> > cima = rescale(Raster::ReadRGB("/home/elazary/images/backyard/trial1/frame001000.ppm"), Dims(160,120));
00248 
00249         rutz::shared_ptr<VisualObject> voComp(new VisualObject("Loc3", "Loc3", cima));
00250 
00251         Image<float> SMapComp = smt->getSMap(cima);
00252         for(int i=0; i<14; i++){
00253                 if (smt->cmaps[i].initialized()){
00254                         cmap1[i] = smt->cmaps[i];
00255 
00256                 }
00257         }
00258 
00259 
00260         //inplaceNormalize(SMapComp, 0.0F, 255.0F);
00261 
00262 
00263 
00264         double minSMapDist = 999999999;
00265         int minSMap = 0;
00266 
00267         // double minSMapDistFFT = 99999999;
00268         // int minSMapFFT = 0;
00269 
00270         double minSiftDist = 99999999;
00271         int minSift = 0;
00272 
00273         while(goforever) {
00274 
00275                 // read new image in?
00276 
00277                 FrameState is = FRAME_NEXT;
00278                 for(int i=0; i<100 && is != FRAME_COMPLETE; i++) //advance by  100 frames
00279                         is = ifs->update(masterclock.getSimTime());
00280                 if (is == FRAME_COMPLETE)
00281                 {
00282                         LINFO("quitting because input is complete");
00283                         break;
00284                 }
00285 
00286                 if (is == FRAME_NEXT || is == FRAME_FINAL) // new frame
00287                 {
00288                         Image< PixRGB<byte> > ima = ifs->readRGB();
00289 
00290                         //Image<float> lum = luminance(ima);
00291 
00292                         //Compute Saliency Map
00293                         //
00294 
00295 
00296                         ///////////////////////////// SIFT ///////////////////////////
00297 
00298                         rutz::shared_ptr<VisualObject> vo(new VisualObject("Img", "Img", ima));
00299 
00300                         Image<PixRGB<byte> > img1 = cima;
00301                         Image<PixRGB<byte> > img2 = ima;
00302 
00303 
00304                         img1 = voComp->getKeypointImage();
00305                         img2 = vo->getKeypointImage();
00306 
00307 
00308                         std::vector< rutz::shared_ptr<Keypoint> > keypoints1 = voComp->getKeypoints();
00309                         std::vector< rutz::shared_ptr<Keypoint> > keypoints2 = vo->getKeypoints();
00310 
00311                         double imgDist = compImg(keypoints1, keypoints2);
00312                         //double imgDist = voComp->getFeatureDistSq(vo);
00313 
00314                         disp = rescale(ima, Dims(256, 256));
00315                         disp = concatX(disp, rescale(img1, Dims(256, 256)));
00316                         disp = concatX(disp, rescale(img2, Dims(256, 256)));
00317 
00318 
00319 
00320                         xwin.drawImage(disp);
00321 
00322                         getchar();
00323 
00324 
00325                         //////////////////////////////////////////////////////////////
00326 
00327                         /*
00328                         double distance = 0;
00329                         Image<float> SMap = smt->getSMap(ima);
00330                         for(int i=6; i<7; i++){
00331                                 if (smt->cmaps[i].initialized()){
00332                                         cmap2[i] = smt->cmaps[i];
00333 
00334                                         inplaceNormalize(cmap1[i], 0.0F, 255.0F);
00335                                         inplaceNormalize(cmap2[i], 0.0F, 255.0F);
00336 
00337                                         std::vector<KeyPoint> *keypoints1;
00338                                         std::vector<KeyPoint> *keypoints2;
00339 
00340                                         keypoints1 = getKeypoints(cmap1[i]);
00341                                         keypoints2 = getKeypoints(cmap2[i]);
00342 
00343                                         distance += compImg(*keypoints1, *keypoints2);
00344                                         //Show the keypoints
00345 
00346                                         Image<PixRGB<byte> > SMapCompDisp = toRGB((Image<byte>)cmap2[i]);
00347                                         Image<PixRGB<byte> > SMapDisp = toRGB((Image<byte>)cmap1[i]);
00348                                         Point2D<int> LastLoc(0,0);
00349 
00350                                         for(unsigned int j=0; j<keypoints1->size(); j++){
00351                                         //        LINFO("Draw keypoint1 at (%i,%i) val=%f",
00352                                         //                        (*keypoints1)[j].x, (*keypoints1)[j].y, (*keypoints1)[j].val);
00353                                                 drawDisk(SMapCompDisp, Point2D<int>((*keypoints1)[j].x,
00354                                                                         (*keypoints1)[j].y), ROI_SIZE, PixRGB<byte>(255,0,0));
00355 
00356                                                 Point2D<int> NewLoc((*keypoints1)[j].x, (*keypoints1)[j].y);
00357                                                 drawLine(SMapCompDisp, LastLoc, NewLoc, PixRGB<byte>(255,0,0));
00358                                                 LastLoc = NewLoc;
00359                                         }
00360                                         LastLoc.i =0; LastLoc.j=0;
00361                                         for(unsigned int j=0; j<keypoints2->size(); j++){
00362                                         //        LINFO("Draw keypoint2 at (%i,%i) val=%f",
00363                                         //                        (*keypoints2)[j].x, (*keypoints2)[j].y, (*keypoints2)[j].val);
00364                                                 drawDisk(SMapDisp, Point2D<int>((*keypoints2)[j].x,
00365                                                                         (*keypoints2)[j].y), ROI_SIZE, PixRGB<byte>(255,0,0));
00366                                                 Point2D<int> NewLoc((*keypoints2)[j].x, (*keypoints2)[j].y);
00367                                                 drawLine(SMapDisp, LastLoc, NewLoc, PixRGB<byte>(255,0,0));
00368                                                 LastLoc = NewLoc;
00369                                         }
00370 
00371                                         disp = rescale(ima, Dims(256, 256));
00372                                         disp = concatX(disp, rescale(SMapCompDisp, Dims(256, 256)));
00373                                         disp = concatX(disp, rescale(SMapDisp, Dims(256, 256)));
00374 
00375                                         xwin.drawImage(disp);
00376 
00377                                         getchar();
00378                                 }
00379                         }
00380                         */
00381 
00382                         //double SMapdist = distance(SMap, SMapComp);
00383       if (imgDist < minSMapDist){
00384         minSMapDist = imgDist;
00385         minSMap = ifs->frame();
00386       }
00387 
00388                         LINFO("Distance for %i = %f", ifs->frame(), imgDist);
00389 
00390                         // compute and show framerate and stats over the last NAVG frames:
00391                         avgtime += tim.getReset(); avgn ++;
00392                         if (avgn == NAVG)
00393                         {
00394                                 fps = 1000.0F / float(avgtime) * float(avgn);
00395                                 avgtime = 0; avgn = 0;
00396                         }
00397 
00398                         // create an info string:
00399                         sprintf(info, "%06u %.1ffps  ", ii++, fps);
00400 
00401 
00402                         ii++;
00403                         //          getchar();
00404                 }
00405 
00406 
00407         }
00408 
00409         LINFO("BestSmapMatch %i SMapDist = %f", minSMap, minSMapDist);
00410         // LINFO("BestSmapFFTMatch %i SMapDist = %f", minSMapFFT, minSMapDistFFT);
00411         LINFO("BestSiftMatch %i SiftDist = %f", minSift, minSiftDist);
00412 
00413         // stop all our ModelComponents
00414         manager.stop();
00415 
00416         // all done!
00417         return 0;
00418 }
00419 
00420 // ######################################################################
00421 /* So things look consistent in everyone's emacs... */
00422 /* Local Variables: */
00423 /* indent-tabs-mode: nil */
00424 /* End: */
Generated on Sun May 8 08:41:17 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3