test-ComplexObject.C

Go to the documentation of this file.
00001 /*!@file BeoSub/test-ComplexObject.C ComplexObject test module */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Zack Gossman <gossman@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/test-ComplexObject.C $
00035 // $Id: test-ComplexObject.C 14376 2011-01-11 02:44:34Z pez $
00036 //
00037 
00038 
00039 
00040 //NOTE: new stuff seems to have made recognition occure much less often. Why? Do NOT commit until this is FIXed!
00041 #ifndef TESTCOMPLEXOBJECT_H_DEFINED
00042 #define TESTCOMPLEXOBJECT_H_DEFINED
00043 
00044 #include "BeoSub/ComplexObject.H"
00045 
00046 //CAMERA STUFF
00047 #include "Image/Image.H"
00048 #include "Image/Pixels.H"
00049 #include "Image/Transforms.H"
00050 #include "Image/MathOps.H"
00051 #include "GUI/XWindow.H"
00052 #include "Component/ModelManager.H"
00053 #include "Devices/FrameGrabberFactory.H"
00054 #include "Raster/Raster.H"
00055 #include "Util/Timer.H"
00056 #include "Util/Types.H"
00057 #include "Util/log.H"
00058 #include "BeoSub/BeoSubCanny.H"
00059 
00060 #include <cstdio>
00061 #include <cstdlib>
00062 #include <cstring>
00063 #include <fstream>
00064 #include <iostream>
00065 
00066 //END CAMERA STUFF
00067 
00068 int main(int argc, char **argv)
00069 {
00070   Image< PixRGB<byte> > outputImg;
00071 
00072   Point2D<int> tl, tr, br, bl;
00073   MYLOGVERB=LOG_CRIT;
00074   bool hasSetup = false;
00075   //Parse the command line options
00076   const char *infilename = NULL;  //Name of the input image
00077   //char *objectfile = NULL; //File of the ComplexObject
00078   std::string objectfile="";
00079   int matchcount = 0;
00080   const char *showMatches = NULL; //input to determine number of displayed matches
00081   bool showAllMatches = false;
00082 
00083   //int count = 0;
00084 
00085   const char* objectdbfilename = NULL;
00086   std::string line = "";
00087 
00088   std::vector < rutz::shared_ptr<VisualObjectMatch> > matches;
00089   Image< PixRGB<byte> > fusedImg;
00090   Image< PixRGB<byte> > keypointImg;
00091 
00092   std::vector < rutz::shared_ptr<ComplexObject> > cov;
00093   std::vector < rutz::shared_ptr<ComplexObject> >::iterator coIter, coIterStop;
00094   rutz::shared_ptr<ComplexObject> co;
00095 
00096   rutz::shared_ptr<XWindow> wini;
00097 
00098 
00099 
00100   if(argc < 3){
00101     fprintf(stderr,"\n<USAGE> %s database_filename image showall\n",argv[0]);
00102     fprintf(stderr,"\n      database_filename:   The path and filename of the object database.\n");
00103     fprintf(stderr,"\n      image:             An image to process. Must be in PGM format.\n");
00104     fprintf(stderr,"                           Type 'none' for camera input.\n");
00105     fprintf(stderr,"\n      showall(Y/N):     Whether one match (N) or all matches (Y)\n");
00106     fprintf(stderr,"\n                         should be found.\n");
00107     exit(1);
00108   }
00109 
00110   //NOTE: the file of the database_filename is simple
00111   //it is a list of .obj files, with one file per line
00112   //be careful not to have spaces
00113 
00114 
00115   objectdbfilename = argv[1];
00116   std::string fname = objectdbfilename;
00117 
00118   std::string filepathstr = fname.substr(0, fname.find_last_of('/')+1);
00119 
00120 
00121    std::ifstream is(objectdbfilename);
00122   if (is.is_open() == false)
00123      { LERROR("Cannot open '%s' -- USING EMPTY", objectdbfilename); exit(0); }
00124 
00125   while(!is.eof()) {
00126     getline(is, line);
00127     objectfile = line;
00128 
00129     if (strcmp(objectfile.c_str(), "") == 0) {
00130       continue;
00131     }
00132 
00133     printf("Loading: %s\n", objectfile.c_str());
00134 
00135     //    co.reset(new ComplexObject("MyObject", (char*)(filepathstr + objectfile).c_str()));
00136     co.reset(new ComplexObject((char*)(filepathstr + objectfile).c_str()));
00137     cov.push_back(co);
00138   }
00139   // is>>showMatches;
00140 
00141   //objectfile = argv[1];
00142 
00143   infilename = argv[2];
00144 
00145   showMatches = argv[3];
00146 
00147 
00148   if(!strcmp(showMatches, "Y") || !strcmp(showMatches, "y")){
00149     showAllMatches = true;
00150   }
00151 
00152 
00153 
00154 
00155 
00156   // instantiate a model manager (for camera input):
00157   ModelManager manager("ComplexObject Tester");
00158   // Instantiate our various ModelComponents:
00159   nub::soft_ref<FrameIstream>
00160     gb(makeIEEE1394grabber(manager, "COcam", "cocam"));
00161 
00162   if(!strcmp(infilename, "none")){
00163     //GRAB image from camera to be tested
00164     manager.addSubComponent(gb);
00165 
00166     // set the camera number (in IEEE1394 lingo, this is the
00167     // "subchannel" number):
00168     gb->setModelParamVal("FrameGrabberSubChan", 0);
00169     gb->setModelParamVal("FrameGrabberBrightness", 128);
00170     gb->setModelParamVal("FrameGrabberHue", 180);
00171   }
00172   manager.start();
00173 
00174   Image< PixRGB<byte> > Img;
00175 
00176   if(!strcmp(infilename, "none")){
00177   }
00178   else{
00179     //TO TEST FROM FILE
00180     Img = Raster::ReadRGB(infilename);
00181   }
00182 
00183 
00184 
00185    while(1){
00186     //Get image to be matched
00187     //TO TEST FROM CAMERA
00188     if(!strcmp(infilename, "none")){
00189       Img = gb->readRGB(); //grab();
00190     }
00191 
00192 
00193     coIter = cov.begin();
00194     coIterStop = cov.end();
00195     //rutz::shared_ptr<VisualObject> vo(new VisualObject("mypic", "mypicfilename", Img));
00196 
00197   rutz::shared_ptr<VisualObject>
00198     vo(new VisualObject("mypic", "mypicfilename", Img,
00199                         Point2D<int>(-1,-1), std::vector<float>(),
00200                         std::vector< rutz::shared_ptr<Keypoint> >(), false));
00201   outputImg = Img;
00202 
00203 
00204     while(coIter != coIterStop) {
00205          matchcount = (*coIter)->matchKeypoints(showAllMatches, vo, VOMA_SIMPLE, matches, keypointImg, fusedImg);
00206 
00207 
00208 
00209     if(!hasSetup){
00210 
00211       wini.reset(new XWindow(Img.getDims(), -1, -1, "input window"));
00212       wini->setPosition(0, 0);
00213 
00214       Dims ndims(Img.getDims().w(), (Img.getDims().h()*2));
00215 
00216       hasSetup = true;
00217     }
00218 
00219 
00220    // for (uint i = 0; i < matches.size(); i++) {
00221     if (matches.size() > 0) {
00222      // count = 1;
00223     LINFO("\nMATCHCOUNT :: %d\tmatches.size() :: %d\n", matchcount, int(matches.size()));
00224       matches[0]->getTransfTestOutline(tl,tr,br,bl);
00225    // }
00226 
00227      // if(count-- > 0 ) {
00228       //drawRect(outputImg, Rectangle::tlbrI(tl.j, tl.i, br.j, br.i), PixRGB<byte>(255, 0, 0));
00229       int size = 3;
00230 
00231       Point2D<int> *tmp = NULL;
00232       for(int i = 0; i < 4; i++) {
00233       switch(i) {
00234         case 0:
00235           tmp = &tl;
00236           break;
00237 
00238         case 1:
00239           tmp = &tr;
00240           break;
00241 
00242         case 2:
00243           tmp = &bl;
00244           break;
00245 
00246         case 3:
00247           tmp = &br;
00248           break;
00249       }
00250 
00251 
00252       if (tmp->i <= 3) {
00253         tmp->i = 4;
00254       }
00255       else if (tmp->i >=  outputImg.getWidth()-3) {
00256         tmp->i =  outputImg.getWidth() - 4;
00257       }
00258 
00259       if (tmp->j <= 3) {
00260         tmp->j = 4;
00261       }
00262       else if (tmp->j >=  outputImg.getHeight()-3) {
00263         tmp->j =  outputImg.getHeight() - 4;
00264       }
00265       }
00266 
00267 
00268       drawLine(outputImg, tl, tr, PixRGB<byte>(255, 0, 0), size);
00269       drawLine(outputImg, tr, br, PixRGB<byte>(255, 0, 0), size);
00270       drawLine(outputImg, br, bl, PixRGB<byte>(255, 0, 0), size);
00271       drawLine(outputImg, bl, tl, PixRGB<byte>(255, 0, 0), size);
00272 
00273       Point2D<int> com = (tl+tr+bl+br)/4;
00274       drawCross(outputImg, com, PixRGB<byte>(255, 0, 0), 10, size);
00275     }
00276 
00277 
00278 
00279     //show images to screen
00280 
00281     *coIter++;
00282     }
00283     wini->drawImage(outputImg);
00284    }
00285   return 0;
00286 
00287 }
00288 
00289 
00290 #endif
00291 // ######################################################################
00292 /* So things look consistent in everyone's emacs... */
00293 /* Local Variables: */
00294 /* indent-tabs-mode: nil */
00295 /* End: */
Generated on Sun May 8 08:40:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3