00001 /*!@file plugins/SceneUnderstanding/GTEvaluator.C */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the 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 <elazary@usc.edu> 00034 // $HeadURL: $ 00035 // $Id: $ 00036 // 00037 00038 #ifndef GTEvaluator_C_DEFINED 00039 #define GTEvaluator_C_DEFINED 00040 00041 #include "Image/OpenCVUtil.H" //Needs to be first 00042 00043 #include "plugins/SceneUnderstanding/GTEvaluator.H" 00044 00045 #include "Image/DrawOps.H" 00046 #include "Raster/Raster.H" 00047 #include "Simulation/SimEventQueue.H" 00048 #include "GUI/DebugWin.H" 00049 00050 #include "Media/MediaSimEvents.H" 00051 //#include "Neuro/NeuroSimEvents.H" 00052 #include "Transport/FrameInfo.H" 00053 #include "Transport/FrameOstream.H" 00054 00055 00056 const ModelOptionCateg MOC_GTEvaluator = { 00057 MOC_SORTPRI_3, "GTEvaluator-Related Options" }; 00058 00059 // Used by: SimulationViewerEyeMvt 00060 const ModelOptionDef OPT_GTEvaluatorShowDebug = 00061 { MODOPT_ARG(bool), "GTEvaluatorShowDebug", &MOC_GTEvaluator, OPTEXP_CORE, 00062 "Show debug img", 00063 "gtevaluator-debug", '\0', "<true|false>", "false" }; 00064 00065 //Define the inst function name 00066 SIMMODULEINSTFUNC(GTEvaluator); 00067 00068 00069 // ###################################################################### 00070 GTEvaluator::GTEvaluator(OptionManager& mgr, const std::string& descrName, 00071 const std::string& tagName) : 00072 SimModule(mgr, descrName, tagName), 00073 SIMCALLBACK_INIT(SimEventLineMatchingOutput), 00074 SIMCALLBACK_INIT(SimEventSaveOutput), 00075 itsShowDebug(&OPT_GTEvaluatorShowDebug, this) 00076 { 00077 } 00078 00079 // ###################################################################### 00080 GTEvaluator::~GTEvaluator() 00081 { 00082 } 00083 00084 00085 // ###################################################################### 00086 void GTEvaluator::onSimEventLineMatchingOutput(SimEventQueue& q, 00087 rutz::shared_ptr<SimEventLineMatchingOutput>& e) 00088 { 00089 itsShapes = e->getShapes(); 00090 00091 //if (SeC<SimEventInputFrame> ee = q.check<SimEventInputFrame>(this,SEQ_UNMARKED,0)) 00092 if (SeC<SimEventInputFrame> eframe = q.check<SimEventInputFrame>(this)) 00093 { 00094 GenericFrame frame = eframe->frame(); 00095 itsInImage = frame.asRgb(); 00096 rutz::shared_ptr<GenericFrame::MetaData> metaData = frame.getMetaData(std::string("SceneData")); 00097 if (metaData.get() != 0) 00098 itsSceneData.dyn_cast_from(metaData); 00099 } 00100 00101 evolve(q); 00102 00103 } 00104 00105 void GTEvaluator::evolve(SimEventQueue& q) 00106 { 00107 return; 00108 00109 std::vector<Rectangle> gtBB; 00110 00111 //Get the ground truth and check 00112 if (itsSceneData.get() != 0) 00113 { 00114 for(uint obj=0; obj<itsSceneData->objects.size(); obj++) 00115 { 00116 TestImages::ObjData objData = itsSceneData->objects[obj]; 00117 00118 // find the object dimention from the polygon 00119 if (objData.polygon.size() > 0) 00120 { 00121 int minX=(int)objData.polygon[0].i; 00122 int minY=(int)objData.polygon[0].j; 00123 int maxX=minX; 00124 int maxY=minY; 00125 for(uint i=1;i<objData.polygon.size();i++) 00126 { 00127 if(objData.polygon[i].i < minX) minX = (int)objData.polygon[i].i; 00128 if(objData.polygon[i].j < minY) minY = (int)objData.polygon[i].j; 00129 if(objData.polygon[i].i > maxX) maxX = (int)objData.polygon[i].i; 00130 if(objData.polygon[i].j > maxY) maxY = (int)objData.polygon[i].j; 00131 } 00132 // Uses outer coordinates 00133 if(minX==maxX || minY==maxY) 00134 LFATAL("Invalid vertices"); 00135 gtBB.push_back(Rectangle::tlbrI(minY,minX,maxY,maxX)); 00136 00137 00138 } 00139 } 00140 } 00141 00142 std::vector<bool> detected(gtBB.size(), false); 00143 00144 for(uint shape=0; shape<itsShapes.size(); shape++) 00145 { 00146 00147 //Find the max overlap for each polygon 00148 double maxOv = 0; 00149 int maxBBIdx = -1; 00150 for(uint i=0; i<gtBB.size(); i++) 00151 { 00152 drawRect(itsInImage, gtBB[i], PixRGB<byte>(0,255,0)); 00153 00154 //Find the shape with the best overlap that matches this bb 00155 Rectangle ovR = itsShapes[shape].bb.getOverlap(gtBB[i]); 00156 00157 double ov = 0; 00158 if (ovR.isValid()) 00159 { 00160 //compute overlap as area of intersection / area of union 00161 double ua = (itsShapes[shape].bb.area()+1) + (gtBB[i].area()+1) - ovR.area(); 00162 ov = (double)ovR.area()/ua; 00163 00164 if (ov > maxOv) 00165 { 00166 maxOv = ov; 00167 maxBBIdx = i; 00168 } 00169 } 00170 } 00171 00172 if (maxOv >= 0.5) 00173 { 00174 if (maxBBIdx != -1) 00175 { 00176 //We found a valid BB 00177 00178 if (!detected[maxBBIdx]) //Did we detect this shape already? 00179 { 00180 detected[maxBBIdx] = true; 00181 printf("Results: 1 %f %f\n", itsShapes[shape].score, maxOv); 00182 00183 00184 drawRect(itsInImage, gtBB[maxBBIdx], PixRGB<byte>(0,255,0)); 00185 drawRect(itsInImage, itsShapes[shape].bb, PixRGB<byte>(255,0,0)); 00186 char msg[255]; 00187 sprintf(msg, "ov: %0.2f", maxOv); 00188 writeText(itsInImage, itsShapes[shape].bb.center(), msg, PixRGB<byte>(255,255,255), PixRGB<byte>(0,0,0)); 00189 00190 00191 } else { 00192 if (itsInImage.rectangleOk(itsShapes[shape].bb)) 00193 drawRect(itsInImage, itsShapes[shape].bb, PixRGB<byte>(255,0,0)); 00194 printf("Results: 0 %f %f\n", itsShapes[shape].score, maxOv); 00195 } 00196 } 00197 } else { 00198 if (itsInImage.rectangleOk(itsShapes[shape].bb)) 00199 drawRect(itsInImage, itsShapes[shape].bb, PixRGB<byte>(255,0,0)); 00200 printf("Results: 0 %f %f\n", itsShapes[shape].score, maxOv); 00201 } 00202 } 00203 SHOWIMG(itsInImage); 00204 00205 00206 } 00207 00208 00209 // ###################################################################### 00210 void GTEvaluator::onSimEventSaveOutput(SimEventQueue& q, 00211 rutz::shared_ptr<SimEventSaveOutput>& e) 00212 { 00213 if (itsShowDebug.getVal()) 00214 { 00215 // get the OFS to save to, assuming sinfo is of type 00216 // SimModuleSaveInfo (will throw a fatal exception otherwise): 00217 nub::ref<FrameOstream> ofs = 00218 dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs; 00219 Layout<PixRGB<byte> > disp = getDebugImage(); 00220 if (disp.initialized()) 00221 ofs->writeRgbLayout(disp, "GTEvaluator", FrameInfo("GTEvaluator", SRC_POS)); 00222 } 00223 } 00224 00225 Layout<PixRGB<byte> > GTEvaluator::getDebugImage() 00226 { 00227 00228 Layout<PixRGB<byte> > disp; 00229 00230 for(uint i=0; i<itsShapes.size() && i < 4; i++) 00231 { 00232 if (itsInImage.rectangleOk(itsShapes[i].bb)) 00233 drawRect(itsInImage, itsShapes[i].bb, PixRGB<byte>(0,255,0)); 00234 } 00235 disp = itsInImage; 00236 00237 usleep(10000); 00238 return disp; 00239 } 00240 00241 #endif