00001 /*!@file BeoSub/test-BeoSubCross.C find cross */ 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-BeoSubCross.C $ 00034 // $Id: test-BeoSubCross.C 9412 2008-03-10 23:10:15Z farhan $ 00035 00036 #include "Component/ModelManager.H" 00037 00038 #include "Media/FrameSeries.H" 00039 #include "Transport/FrameIstream.H" 00040 #include "Media/MediaOpts.H" 00041 #include "Raster/GenericFrame.H" 00042 00043 #include "Image/CutPaste.H" 00044 #include "Image/Image.H" 00045 #include "Image/Pixels.H" 00046 #include "Raster/Raster.H" 00047 00048 #include "GUI/XWinManaged.H" 00049 00050 #include "BeoSub/BeoSubCross.H" 00051 00052 00053 //END CAMERA STUFF 00054 //canny 00055 #define BOOSTBLURFACTOR 90.0 00056 #define FREE_ARG char* 00057 //#define PI 3.14159 00058 #define FILLBLEED 4 00059 #define INITIAL_TEMPERATURE 30.0 00060 #define FINAL_TEMPERATURE 0.5 00061 #define ALPHA 0.98 00062 #define STEPS_PER_CHANGE 1000 00063 00064 #define BIN_ANGLE 0.588001425 00065 00066 int main(int argc, char* argv[]) 00067 { 00068 00069 MYLOGVERB = LOG_INFO; 00070 00071 ModelManager manager("ComplexObject Tester"); 00072 00073 nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager)); 00074 manager.addSubComponent(ifs); 00075 00076 manager.exportOptions(MC_RECURSE); 00077 00078 // Parse command-line: 00079 if (manager.parseCommandLine(argc, argv, 00080 "[image {*.ppm}]", 00081 0, 1) 00082 == false) return(1); 00083 00084 // do post-command-line configs: 00085 00086 std::string infilename; //Name of the input image 00087 bool hasCompIma = false; 00088 Image<PixRGB<byte> > compIma; 00089 if(manager.numExtraArgs() > 0) 00090 { 00091 infilename = manager.getExtraArgAs<std::string>(0); 00092 compIma = Raster::ReadRGB(infilename); 00093 hasCompIma = true; 00094 } 00095 00096 uint w = ifs->getWidth(), h = ifs->getHeight(); 00097 //uint w = 320, h = 240; 00098 std::string dims = convertToString(Dims(w, h)); 00099 LDEBUG("image size: [%dx%d]", w, h); 00100 //manager.setOptionValString(&OPT_InputFrameDims, dims); 00101 //manager.setOptionValString(&OPT_InputFrameSource, "V4L2"); 00102 //manager.setOptionValString(&OPT_FrameGrabberMode, "YUYV"); 00103 //manager.setOptionValString(&OPT_FrameGrabberDims, "1024x576"); 00104 // mgr->setOptionValString(&OPT_FrameGrabberBswap, "no"); 00105 //manager.setOptionValString(&OPT_FrameGrabberFPS, "30"); 00106 manager.setModelParamVal("InputFrameDims", Dims(w, h), 00107 MC_RECURSE | MC_IGNORE_MISSING); 00108 00109 manager.start(); 00110 bool goforever = true; 00111 00112 // input and output image 00113 Image< PixRGB<byte> > cameraImage; 00114 Image< PixRGB<byte> > outputImage; 00115 00116 rutz::shared_ptr<BeoSubCross> crossRecognizer(new BeoSubCross()); 00117 //(infilename, showMatches, objectname); 00118 00119 // need an initial image if no comparison image is found 00120 if(!hasCompIma) 00121 { 00122 // ifs->updateNext(); ima = ifs->readRGB(); 00123 // if(!ima.initialized()) goforever = false; 00124 // else compIma = ima; 00125 compIma.resize(w,h,NO_INIT); 00126 } 00127 int fNum = 0; 00128 int linescale = 80; 00129 // int sumCrossCenterX = 0, sumCrossCenterY = 0; 00130 // uint numPoints = 0; 00131 00132 // count of the number of points that are outside 00133 // std dev to determine if current avg center point is stale 00134 uint stalePointCount = 0; 00135 00136 //! window to display results 00137 rutz::shared_ptr<XWinManaged> itsWin; 00138 itsWin.reset(new XWinManaged(Dims(w*2,h*2),w+10, 30, "Cross Detection Output")); 00139 00140 00141 while(goforever) 00142 { 00143 // LINFO("CYCLE %d\n", ++cycle); 00144 const FrameState is = ifs->updateNext(); 00145 if (is == FRAME_COMPLETE) 00146 break; 00147 00148 //grab the images 00149 GenericFrame input = ifs->readFrame(); 00150 if (!input.initialized()) 00151 break; 00152 cameraImage = rescale(input.asRgb(), w, h); 00153 00154 Timer tim(1000000); 00155 Image<PixRGB<byte> > dispImage(w*2,h*2,NO_INIT); 00156 00157 inplacePaste(dispImage, cameraImage, Point2D<int>(0,0)); 00158 00159 Image<PixRGB<byte> > houghImage(w,h,NO_INIT); 00160 00161 std::vector<LineSegment2D> lines = crossRecognizer->getHoughLines(cameraImage, houghImage); 00162 00163 inplacePaste(dispImage, houghImage, Point2D<int>(w,0)); 00164 00165 std::vector<LineSegment2D> centerPointLines; 00166 Point2D<int> crossCenterPoint = crossRecognizer->getCrossCenter(lines, centerPointLines, stalePointCount); 00167 00168 LDEBUG("Center Point X: %d, Y: %d", crossCenterPoint.i, crossCenterPoint.j); 00169 00170 00171 // bool isWithinStdDev = false; 00172 00173 float crossAngle = crossRecognizer->getCrossDir(centerPointLines); 00174 Point2D<int> p(cameraImage.getWidth()/2, cameraImage.getHeight()/2); 00175 Point2D<int> p2((int)(cameraImage.getWidth()/2+cos(crossAngle)*linescale), 00176 (int)(cameraImage.getHeight()/2+sin(crossAngle)*linescale)); 00177 00178 Image<PixRGB<byte> > crossOverlayImage = cameraImage; 00179 00180 PixRGB <byte> crossColor; 00181 00182 if(stalePointCount <= 30) 00183 { 00184 crossColor = PixRGB <byte> (0, 255,0); 00185 } 00186 else 00187 { 00188 crossColor = PixRGB <byte> (255, 0 ,0); 00189 } 00190 00191 drawCrossOR(crossOverlayImage, 00192 crossCenterPoint, 00193 crossColor, 00194 20,5, fabs(crossAngle)); 00195 00196 00197 inplacePaste(dispImage, crossOverlayImage, Point2D<int>(0,h)); 00198 00199 00200 // else foundCount = 0; 00201 00202 00203 //NEED TO FIX THIS 00204 // if(!itsSetupOrangeTracker) 00205 // { 00206 // setupOrangeTracker(w,h); 00207 // itsSetupOrangeTracker = true; 00208 // } 00209 00210 // int mass = getOrangeMass(cameraImage, display); 00211 // Image<byte> temp = quickInterpolate(segmenter->SITreturnCandidateImage(),4); 00212 // printf("mass:%d", mass); 00213 00214 // inplacePaste(dispImage, toRGB(temp), Point2D<int>(0,h*2)); 00215 00216 // IplImage* dst = cvCreateImage(cvGetSize(img2ipl(cameraImage)), 32, 1 ); 00217 // IplImage* harris_dst = cvCreateImage(cvGetSize(img2ipl(edgeImage)), 32, 1 ); 00218 00219 // cvCornerHarris(dst, harris_dst, 3); 00220 // Image< PixRGB<byte> > harris = ipl2rgb(dst); 00221 00222 // inplacePaste(dispImage, harris, Point2D<int>(0,h*2)); 00223 00224 // inplacePaste(dispImage, outputImage, Point2D<int>(w, h)); 00225 // drawLine(dispImage, Point2D<int>(0,h),Point2D<int>(w*2-1,h), 00226 // PixRGB<byte>(255,255,255),1); 00227 // drawLine(dispImage, Point2D<int>(w,0),Point2D<int>(w,h*2-1), 00228 // PixRGB<byte>(255,255,255),1); 00229 writeText( dispImage, Point2D<int>(0,0), sformat("Frame: %6d", fNum).c_str(), 00230 PixRGB<byte>(255,0,0)); 00231 // writeText( dispImage, Point2D<int>(w,0), sformat("Segment Color").c_str(), 00232 // PixRGB<byte>(255,0,0)); 00233 writeText( dispImage, Point2D<int>(w,0), sformat("Edge Detect and Hough").c_str(), 00234 PixRGB<byte>(255,0,0)); 00235 writeText( dispImage, Point2D<int>(0,h), sformat("Identify Cross").c_str(), 00236 PixRGB<byte>(255,0,0)); 00237 // writeText( dispImage, Point2D<int>(0,h*2), sformat("Harris Corner").c_str(), 00238 // PixRGB<byte>(255,0,0)); 00239 00240 // std::string saveFName = sformat("data/cross_%07d.ppm", fNum); 00241 // LINFO("saving: %s",saveFName.c_str()); 00242 // itsWin->drawImage(dispImage, 0, 0); 00243 itsWin->drawImage(dispImage, 0, 0); 00244 fNum++; 00245 00246 // decrease when: 00247 // it didn't find many lines, but there is some orange in the image 00248 00249 // increase when: 00250 // it finds too many lines 00251 00252 // increase and decrease the hough threshold 00253 // in an attempt to get some lines, but not too many. 00254 // if (orange > 0 && 00255 // totlines < 20 && 00256 // houghThreshold > minThreshold) { 00257 // houghThreshold--; //make it more relaxed 00258 // LINFO("Decreasing Hough Threshold"); 00259 // } 00260 // else if (totlines > 20 && houghThreshold < maxThreshold) { 00261 // houghThreshold++; //make it stricter 00262 // LINFO("Increasing Hough Threshold"); 00263 // } 00264 00265 // LDEBUG("Line Count: %"ZU, lines.size()); 00266 // LDEBUG("Adj Line Count: %d", totlines); 00267 // LDEBUG("Orange: %d", orange); 00268 // LDEBUG("Hough Threshold %d", houghThreshold); 00269 // LDEBUG("Found Streak: %d", foundCount); 00270 00271 itsWin->drawImage(dispImage,0,0); 00272 } 00273 00274 // get ready to terminate: 00275 manager.stop(); 00276 return 0; 00277 } 00278 00279 // ###################################################################### 00280 /* So things look consistent in everyone's emacs... */ 00281 /* Local Variables: */ 00282 /* indent-tabs-mode: nil */ 00283 /* End: */