00001 /*!@file RCBot/trackSIFT.C track featuers */ 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: Lior Elazary <lelazary@yahoo.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/RCBot/trackSIFT.C $ 00035 // $Id: trackSIFT.C 13993 2010-09-20 04:54:23Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Component/OptionManager.H" 00040 #include "Controllers/PID.H" 00041 #include "Devices/FrameGrabberConfigurator.H" 00042 #include "Devices/sc8000.H" 00043 #include "GUI/XWinManaged.H" 00044 #include "Image/ColorOps.H" 00045 #include "Image/CutPaste.H" 00046 #include "Image/DrawOps.H" 00047 #include "Image/Image.H" 00048 #include "Image/MathOps.H" 00049 #include "Image/Pixels.H" 00050 #include "Image/ShapeOps.H" 00051 #include "Neuro/SaliencyMap.H" 00052 #include "Neuro/VisualCortex.H" 00053 #include "Neuro/WTAwinner.H" 00054 #include "Neuro/WinnerTakeAll.H" 00055 #include "RCBot/Motion/MotionEnergy.H" 00056 #include "Raster/Raster.H" 00057 #include "SIFT/Keypoint.H" 00058 #include "SIFT/ScaleSpace.H" 00059 #include "SIFT/VisualObject.H" 00060 #include "SIFT/VisualObjectDB.H" 00061 #include "Transport/FrameIstream.H" 00062 #include "Util/Timer.H" 00063 #include "Util/Types.H" 00064 #include "Util/log.H" 00065 #include <math.h> 00066 00067 00068 #define UP_KEY 98 00069 #define DOWN_KEY 104 00070 #define LEFT_KEY 100 00071 #define RIGHT_KEY 102 00072 00073 XWinManaged window(Dims(256, 256), -1, -1, "Test Output 1"); 00074 XWinManaged window1(Dims(256, 256), -1, -1, "S Map"); 00075 00076 // ###################################################################### 00077 int main(int argc, char **argv) 00078 { 00079 MYLOGVERB = LOG_INFO; 00080 00081 // instantiate a model manager: 00082 ModelManager manager("Camera capture"); 00083 00084 // Instantiate our various ModelComponents: 00085 nub::soft_ref<FrameGrabberConfigurator> 00086 gbc(new FrameGrabberConfigurator(manager)); 00087 manager.addSubComponent(gbc); 00088 00089 nub::soft_ref<VisualCortex> 00090 itsVC(new VisualCortex(manager, "Visual Cortex", "VC")); 00091 00092 /* FIXME 00093 VisualCortexWeights wts = VisualCortexWeights::zeros(); 00094 wts.chanIw = 0.1; 00095 wts.chanCw = 0.1; 00096 wts.chanOw = 1.0; 00097 itsVC->addDefaultChannels(wts); 00098 */ 00099 manager.addSubComponent(itsVC); 00100 00101 nub::soft_ref<SaliencyMap> itsSMAP(new SaliencyMapTrivial(manager)); 00102 manager.addSubComponent(itsSMAP); 00103 00104 nub::soft_ref<WinnerTakeAll> itsWTA(new WinnerTakeAllFast(manager)); 00105 manager.addSubComponent(itsWTA); 00106 00107 00108 // Parse command-line: 00109 if (manager.parseCommandLine(argc, (const char **)argv, "", 0, 0) == false) return(1); 00110 00111 // do post-command-line configs: 00112 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber(); 00113 if (gb.get() == NULL) 00114 LFATAL("You need to select a frame grabber type via the " 00115 "--fg-type=XX command-line option for this program " 00116 "to be useful -- ABORT"); 00117 // int w = gb->getWidth(), h = gb->getHeight(); 00118 00119 //manager.setModelParamVal("InputFrameDims", Dims(w, h), MC_RECURSE | MC_IGNORE_MISSING); 00120 00121 // let's get all our ModelComponent instances started: 00122 manager.start(); 00123 00124 00125 // get the frame grabber to start streaming: 00126 gb->startStream(); 00127 00128 00129 // ########## MAIN LOOP: grab, process, display: 00130 int key = 0; 00131 00132 //sc8000->move(3, -0.3); //move forward slowly 00133 00134 double time = 0; 00135 itsVC->reset(MC_RECURSE); 00136 std::vector<double> feature_vector; 00137 00138 std::vector<double> mean; 00139 00140 Image<PixRGB<byte> > track_img; 00141 Image<PixRGB<byte> > key_points; 00142 00143 VisualObjectDB vdb; 00144 //vdb.loadFrom("test.vdb"); 00145 00146 00147 bool find_obj = false; 00148 00149 while(key != 24){ 00150 // receive conspicuity maps: 00151 // grab an image: 00152 00153 Image< PixRGB<byte> > ima = gb->readRGB(); 00154 00155 Point2D<int> location = window.getLastMouseClick(); 00156 if (location.i > -1 && location.j > -1){ 00157 Dims WindowDims = window.getDims(); 00158 00159 float newi = (float)location.i * (float)ima.getWidth()/(float)WindowDims.w(); 00160 float newj = (float)location.j * (float)ima.getHeight()/(float)WindowDims.h(); 00161 location.i = (int)newi; 00162 location.j = (int)newj; 00163 00164 00165 //we got a click, show the featuers at that point 00166 Rectangle r(Point2D<int>(location.i, location.j), Dims(50, 50)); 00167 track_img = crop(ima, r); 00168 rutz::shared_ptr<VisualObject> vo(new VisualObject("Track", "TracK", track_img)); 00169 //rutz::shared_ptr<VisualObject> vo(new VisualObject("Track", "TracK", ima)); 00170 00171 vdb.addObject(vo); //add the object to the db 00172 key_points = vo->getKeypointImage(); 00173 find_obj = true; 00174 00175 } 00176 00177 Image< PixRGB<byte> > mimg; 00178 std::vector<Point2D<int> > tl, tr, br, bl; 00179 00180 if (find_obj){ 00181 std::vector< rutz::shared_ptr<VisualObjectMatch> > matches; 00182 rutz::shared_ptr<VisualObject> vo(new VisualObject("PIC", "PIC", ima)); 00183 00184 const uint nmatches = vdb.getObjectMatches(vo, matches, VOMA_KDTREEBBF); 00185 00186 printf("Found %i\n", nmatches); 00187 if (nmatches > 0 ){ 00188 for(unsigned int i=0; i< nmatches; i++){ 00189 rutz::shared_ptr<VisualObjectMatch> vom = matches[i]; 00190 rutz::shared_ptr<VisualObject> obj = vom->getVoTest(); 00191 LINFO("### Object match with '%s' score=%f", 00192 obj->getName().c_str(), vom->getScore()); 00193 00194 mimg = vom->getTransfTestImage(mimg); 00195 00196 // also keep track of the corners of the test image, for 00197 // later drawing: 00198 Point2D<int> ptl, ptr, pbr, pbl; 00199 vom->getTransfTestOutline(ptl, ptr, pbr, pbl); 00200 tl.push_back(ptl); tr.push_back(ptr); 00201 br.push_back(pbr); bl.push_back(pbl); 00202 00203 // do a final mix between given image and matches: 00204 mimg = Image<PixRGB<byte> >(mimg * 0.5F + ima * 0.5F); 00205 00206 // finally draw all the object outlines: 00207 PixRGB<byte> col(255, 255, 0); 00208 for (unsigned int i = 0; i < tl.size(); i ++) 00209 { 00210 drawLine(mimg, tl[i], tr[i], col, 1); 00211 drawLine(mimg, tr[i], br[i], col, 1); 00212 drawLine(mimg, br[i], bl[i], col, 1); 00213 drawLine(mimg, bl[i], tl[i], col, 1); 00214 } 00215 00216 window1.drawImage(rescale(mimg, 255, 255)); 00217 } 00218 } else { 00219 window1.drawImage(key_points); 00220 } 00221 00222 00223 00224 00225 00226 } 00227 00228 00229 //drawDisk(ima, newwin.p, 10, PixRGB<byte>(255, 0, 0)); 00230 00231 window.drawImage(rescale(ima, 256, 256)); 00232 00233 00234 00235 time += 0.1; 00236 } 00237 00238 00239 00240 // got interrupted; let's cleanup and exit: 00241 manager.stop(); 00242 return 0; 00243 } 00244 00245 00246 // ###################################################################### 00247 /* So things look consistent in everyone's emacs... */ 00248 /* Local Variables: */ 00249 /* indent-tabs-mode: nil */ 00250 /* End: */