test-BeoSubBin.C

Go to the documentation of this file.
00001 /*!@file BeoSub/test-BeoSubBin.C find bin     */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Michael Montalbo <montalbo@usc.edu>
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/test-BeoSubBin.C $
00034 // $Id: test-BeoSubBin.C 12782 2010-02-05 22:14:30Z irock $
00035 
00036 #include "Image/OpenCVUtil.H"  // must be first to avoid conflicting defs of int64, uint64
00037 #include "BeoSub/BeoSubBin.H"
00038 
00039 #include "Component/ModelManager.H"
00040 
00041 #include "Media/FrameSeries.H"
00042 #include "Transport/FrameIstream.H"
00043 #include "Media/MediaOpts.H"
00044 
00045 #include "Image/CutPaste.H"
00046 #include "Image/Image.H"
00047 #include "Image/Pixels.H"
00048 #include "Image/ColorOps.H"
00049 #include "Raster/Raster.H"
00050 #include "Raster/GenericFrame.H"
00051 #include "Devices/DeviceOpts.H"
00052 
00053 #include "GUI/XWinManaged.H"
00054 
00055 #include "Util/Timer.H"
00056 #include "Image/DrawOps.H"
00057 #include "Image/ShapeOps.H"
00058 
00059 int main(int argc, char* argv[])
00060 {
00061 
00062   MYLOGVERB = LOG_INFO;
00063 
00064   ModelManager manager("ComplexObject Tester");
00065 
00066   nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00067   manager.addSubComponent(ifs);
00068 
00069   manager.exportOptions(MC_RECURSE);
00070 
00071   //manager.setOptionValString(&OPT_InputFrameSource, "V4L2");
00072   manager.setOptionValString(&OPT_FrameGrabberMode, "YUYV");
00073   //manager.setOptionValString(&OPT_FrameGrabberDims, "1024x576");
00074   manager.setOptionValString(&OPT_FrameGrabberByteSwap, "no");
00075   manager.setOptionValString(&OPT_FrameGrabberFPS, "30");
00076 
00077   // Parse command-line:
00078   if (manager.parseCommandLine(argc, argv,
00079                                "[image {*.ppm}]",
00080                                0, 1)
00081       == false) return(1);
00082 
00083   // do post-command-line configs:
00084 
00085   std::string infilename;  //Name of the input image
00086   bool hasCompIma = false;
00087   Image<PixRGB<byte> > compIma;
00088   if(manager.numExtraArgs() > 0)
00089     {
00090       infilename = manager.getExtraArgAs<std::string>(0);
00091       compIma = Raster::ReadRGB(infilename);
00092       hasCompIma = true;
00093     }
00094 
00095   uint w = 320, h = 240;
00096   std::string dims = convertToString(Dims(w, h));
00097   LDEBUG("image size: [%dx%d]", w, h);
00098   manager.setOptionValString(&OPT_InputFrameDims, dims);
00099 
00100   manager.setModelParamVal("InputFrameDims", Dims(w, h),
00101                            MC_RECURSE | MC_IGNORE_MISSING);
00102 
00103   manager.start();
00104   bool goforever = true;
00105 
00106   // input and output image
00107   Image< PixRGB<byte> > cameraImage;
00108   Image< PixRGB<byte> > outputImage;
00109 
00110   rutz::shared_ptr<BeoSubBin> binRecognizer(new BeoSubBin());
00111 
00112   // need an initial image if no comparison image is found
00113    if(!hasCompIma)
00114      compIma.resize(w,h,NO_INIT);
00115 
00116   int fNum = 0;
00117 
00118 
00119   //! window to display results
00120   rutz::shared_ptr<XWinManaged> itsWin;
00121   itsWin.reset(new XWinManaged(Dims(w*2,h*2),0, 0, "Bin Detection Output"));
00122 
00123   Point2D<int> centerOfMass;
00124   std::vector<Point2D<int> > avgCenter;
00125 
00126   while(goforever)
00127     {
00128       //      LINFO("CYCLE %d\n", ++cycle);
00129 
00130       const FrameState is = ifs->updateNext();
00131 
00132       if(is == FRAME_COMPLETE)
00133         break;
00134 
00135       GenericFrame input = ifs->readFrame();
00136       if(!input.initialized()) { Raster::waitForKey(); break; }
00137 
00138       cameraImage = rescale(input.asRgb(), w, h);
00139 
00140       Timer tim(1000000);
00141       Image<PixRGB<byte> > dispImage(w*2,h*2,NO_INIT);
00142 
00143       inplacePaste(dispImage, cameraImage, Point2D<int>(0,0));
00144 
00145 
00146 
00147 
00148       Image<PixRGB<byte> > preHough(w, h, NO_INIT);
00149       Image<PixRGB<byte> > houghImage(w,h,NO_INIT);
00150       Image<PixRGB<byte> > blobImage(w, h, NO_INIT);
00151       Image< PixRGB<byte> > intersectImage(w, h, ZEROS);
00152 
00153       std::vector<LineSegment2D> bottomLines = binRecognizer->getHoughLines(cameraImage, intersectImage, houghImage);
00154 
00155       //Point2D<int> centerOfMass;
00156       float mass = binRecognizer->getBinSceneMass(cameraImage, blobImage, centerOfMass);
00157       drawCircle(blobImage, centerOfMass, 5, PixRGB<byte>(255, 0, 0));
00158 
00159       //drawCircle(blobImage, centerOfMass, 5, PixRGB<byte>(0, 255, 0));
00160 
00161       std::vector<LineSegment2D> test;
00162 
00163       //binRecognizer->removeOrangePipe(cameraImage);
00164 
00165       //binRecognizer->pruneLines(lines, test, &preHough);
00166 
00167       // NOTE THERE IS AN ISSUE WITH THE WHITENESS OF THE IMAGE ////
00168 
00169       // for the bottom camera
00170       //std::vector<LineSegment2D> bottomLines;
00171       binRecognizer->pruneLines(bottomLines, bottomLines, &preHough);
00172       //binRecognizer->getParallelIntersections(test, frontLines, preHough);
00173       //binRecognizer->binAngles.clear();
00174       //binRecognizer->pruneAngles(frontLines, binRecognizer->binAngles, &preHough);
00175 
00176       for(uint i = 0; i < binRecognizer->binAngles.size(); i++) {
00177         //printf("x: %d, y: %d, angle: %f\n", binAngles[i].pos.i, binAngles[i].pos.j, binAngles[i].angle);
00178         drawCircle(intersectImage, binRecognizer->binAngles[i].pos, 5, PixRGB<byte>(0, 255, 0));
00179       }
00180 
00181 
00182       // if we have both mass and angles, use weighted mass
00183       if(binRecognizer->binAngles.size() > 0 && mass > 0) {
00184         binRecognizer->getWeightedBinMass(binRecognizer->binAngles, centerOfMass, false, &intersectImage);
00185         drawCircle(intersectImage, centerOfMass, 5, PixRGB<byte>(255, 0, 0));
00186         avgCenter.push_back(centerOfMass);
00187       } // if we have mass, but no angles, use mass
00188       else if(binRecognizer->binAngles.size() <= 0 && mass > 0) {
00189         drawCircle(intersectImage, centerOfMass, 5, PixRGB<byte>(255, 0, 0));
00190         avgCenter.push_back(centerOfMass);
00191       }// if we have angles, but no mass, use center of angles
00192       else if(binRecognizer->binAngles.size() > 0 && mass <= 0) {
00193         binRecognizer->getBinCenter(binRecognizer->binAngles, centerOfMass);
00194         drawCircle(intersectImage, centerOfMass, 5, PixRGB<byte>(255, 0, 0));
00195       }
00196 
00197 
00198       /*// for the top camera
00199       std::vector<LineSegment2D> frontLines;
00200       // prune off repeated lines
00201       binRecognizer->pruneLines(test, frontLines, &preHough);
00202       // prune off non-horizontal lines
00203       binRecognizer->getParallelIntersections(frontLines, frontLines, preHough);
00204       //binRecognizer->binAngles.clear();
00205       // this is a redundant function
00206       //binRecognizer->pruneAngles(frontLines, binRecognizer->binAngles, &preHough);
00207 
00208       for(uint i = 0; i < binRecognizer->binAngles.size(); i++) {
00209         //printf("x: %d, y: %d, angle: %f\n", binAngles[i].pos.i, binAngles[i].pos.j, binAngles[i].angle);
00210         drawCircle(preHough, binRecognizer->binAngles[i].pos, 5, PixRGB<byte>(0, 255, 0));
00211         } */
00212       /*if(frontLines.size() > 0 && mass > 0) {
00213         binRecognizer->getWeightedBinMass(binRecognizer->binAngles, centerOfMass, false, &preHough);
00214         drawCircle(blobImage, centerOfMass, 5, PixRGB<byte>(255, 0, 0));
00215         avgCenter.push_back(centerOfMass);
00216       }
00217       else if(frontLines.size() > 0 && binRecognizer->binAngles.size() <= 0) {
00218         drawCircle(blobImage, centerOfMass, 5, PixRGB<byte>(255, 0, 0));
00219         avgCenter.push_back(centerOfMass);
00220         } */
00221 
00222       inplacePaste(dispImage, preHough, Point2D<int>(w,0));
00223       inplacePaste(dispImage, blobImage, Point2D<int>(0, h));
00224       inplacePaste(dispImage, intersectImage, Point2D<int>(w,h));
00225 
00226 
00227       writeText( dispImage, Point2D<int>(0,0), sformat("Frame: %6d", fNum).c_str(),
00228                  PixRGB<byte>(255,0,0));
00229       writeText( dispImage, Point2D<int>(0,h), sformat("Center of Mass: %f", mass).c_str(),
00230                  PixRGB<byte>(255,0,0));
00231       writeText( dispImage, Point2D<int>(w,0), sformat("Hough Lines").c_str(),
00232                  PixRGB<byte>(255,0,0));
00233       writeText( dispImage, Point2D<int>(w,h), sformat("Center of Bin").c_str(),
00234                  PixRGB<byte>(255,0,0));
00235 
00236       itsWin->drawImage(dispImage, 0, 0);
00237       fNum++;
00238 
00239 
00240       Raster::waitForKey();
00241     }
00242 
00243   // get ready to terminate:
00244   manager.stop();
00245   return 0;
00246 }
00247 
00248 // ######################################################################
00249 /* So things look consistent in everyone's emacs... */
00250 /* Local Variables: */
00251 /* indent-tabs-mode: nil */
00252 /* End: */
Generated on Sun May 8 08:40:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3