00001 /*!@file AppMedia/test-viewport.C test the opengl viewport */ 00002 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00004 // by the 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 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00032 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ObjRec/test-vobj.C $ 00033 // $Id: test-vobj.C 10794 2009-02-08 06:21:09Z itti $ 00034 00035 00036 #include "Component/ModelManager.H" 00037 #include "Media/FrameSeries.H" 00038 #include "Transport/FrameInfo.H" 00039 #include "Image/Image.H" 00040 #include "Image/ImageSet.H" 00041 #include "Image/ShapeOps.H" 00042 #include "Image/CutPaste.H" 00043 #include "Image/DrawOps.H" 00044 #include "Image/FilterOps.H" 00045 #include "Image/ColorOps.H" 00046 #include "Image/Transforms.H" 00047 #include "Image/MathOps.H" 00048 #include "Image/MatrixOps.H" 00049 #include "Image/Kernels.H" 00050 #include "Image/fancynorm.H" 00051 #include "Image/Layout.H" 00052 #include "Util/log.H" 00053 #include <math.h> 00054 #include <stdlib.h> 00055 00056 struct ObjState 00057 { 00058 float pos[3]; 00059 float side[3]; 00060 float ori[3]; 00061 float color[3]; 00062 float lik; 00063 }; 00064 00065 Image<float> thresh(const Image<float>& src) 00066 { 00067 00068 Image<float> result = src; 00069 Image<float>::const_iterator sptr = src.begin(); 00070 Image<float>::iterator dptr = result.beginw(); 00071 int size = src.getSize(); 00072 00073 float min, max; 00074 getMinMax(src, min, max); 00075 00076 //treshold 00077 for (int i = 0; i < size; i ++) 00078 { 00079 if (*sptr/max > 0.10) 00080 *dptr++ = 1.0; 00081 else 00082 *dptr++ = 0; 00083 *sptr++; 00084 } 00085 00086 00087 return result; 00088 } 00089 00090 Image<float> edgeDist(const Image<float>& priorMag, const Image<float>& priorOri, 00091 const Image<float>& dataMag, const Image<float>& dataOri) 00092 { 00093 00094 Image<float> dataChamfer = chamfer34(thresh(dataMag)); 00095 Image<float> priorThresh = thresh(priorMag); 00096 00097 return dataChamfer*priorThresh; 00098 } 00099 00100 00101 00102 Image<PixRGB<byte> > drawObject(ObjState &objState) 00103 { 00104 Image<PixRGB<byte> > objImg(256,256,ZEROS); 00105 drawPatch(objImg, Point2D<int>((int)objState.pos[0], (int)objState.pos[1]), 40, PixRGB<byte>(255,0,0)); 00106 00107 return objImg; 00108 } 00109 00110 Image<PixRGB<byte> > drawWorld() 00111 { 00112 Image<PixRGB<byte> > objImg(256,256,ZEROS); 00113 00114 int x = randomUpToIncluding(256); 00115 int y = randomUpToIncluding(256); 00116 LINFO("Object at %ix%i", x, y); 00117 int size = 40; 00118 PixRGB<byte> color = PixRGB<byte>(255,0,0); 00119 00120 drawPatch(objImg, Point2D<int>(x,y), size, color); 00121 00122 //x = randomUpToIncluding(256); 00123 //y = randomUpToIncluding(256); 00124 //LINFO("Object at %ix%i", x, y); 00125 00126 //drawPatch(objImg, Point2D<int>(x,y), size, color); 00127 00128 return objImg; 00129 } 00130 00131 Image<float> likelyhood(const Image<PixRGB<byte> > &world, const Image<PixRGB<byte> > &model) 00132 { 00133 00134 Image<float> worldMag, worldOri; 00135 Image<byte> worldLum = luminance(world); 00136 gradientSobel(worldLum, worldMag, worldOri, 3); 00137 00138 Image<float> modelMag, modelOri; 00139 Image<byte> modelLum = luminance(model); 00140 gradientSobel(modelLum, modelMag, modelOri, 3); 00141 00142 //Image<float> diffImg = edgeDist(worldMag, worldOri, modelMag, modelOri); 00143 Image<float> diffImg = edgeDist(modelMag, modelOri, worldMag, worldOri); 00144 00145 return diffImg; 00146 00147 } 00148 00149 void normalize(std::vector<ObjState> &particles) 00150 { 00151 00152 float sum = 0; 00153 for (uint i=0; i<particles.size(); i++) 00154 sum += particles[i].lik; 00155 00156 for (uint i=0; i<particles.size(); i++) 00157 particles[i].lik /= sum; 00158 } 00159 00160 int resample(std::vector<ObjState> &particles) 00161 { 00162 00163 float max = particles[0].lik; 00164 int maxi = 0; 00165 for (uint i=1; i<particles.size(); i++) 00166 { 00167 if (particles[i].lik > max) 00168 { 00169 maxi = i; 00170 max = particles[i].lik; 00171 } 00172 00173 } 00174 00175 return maxi; 00176 00177 } 00178 00179 Image<float> getLikImg(std::vector<ObjState> &particles) 00180 { 00181 00182 Image<float> retImg(256,256,ZEROS); 00183 00184 for (uint i=0; i<particles.size(); i++) 00185 retImg.setVal((int)particles[i].pos[0], (int)particles[i].pos[1],particles[i].lik); 00186 00187 inplaceNormalize(retImg, 0.0F, 255.0F); 00188 00189 return retImg; 00190 } 00191 00192 00193 int main(const int argc, const char **argv) 00194 { 00195 MYLOGVERB = LOG_INFO; 00196 ModelManager *mgr = new ModelManager("Test ObjRec"); 00197 00198 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr)); 00199 mgr->addSubComponent(ofs); 00200 00201 mgr->exportOptions(MC_RECURSE); 00202 00203 if (mgr->parseCommandLine( 00204 (const int)argc, (const char**)argv, "", 0, 0) == false) 00205 return 1; 00206 mgr->start(); 00207 00208 00209 int run = 1; 00210 Image<PixRGB<byte> > worldImg = drawWorld(); 00211 00212 //generate particles as uniform dist 00213 std::vector<ObjState> particles(100); 00214 for(uint i=0; i<particles.size(); i++) 00215 { 00216 particles[i].pos[0] = randomUpToIncluding(255); 00217 particles[i].pos[1] = randomUpToIncluding(255); 00218 particles[i].lik = 1/(float)particles.size(); 00219 } 00220 while(run) 00221 { 00222 00223 LINFO("Move particles"); 00224 for(uint i=0; i<particles.size(); i++) 00225 { 00226 particles[i].pos[0] += randomUpToIncluding(6)-3; 00227 particles[i].pos[1] += randomUpToIncluding(6)-3; 00228 00229 //apply prior 00230 if (particles[i].pos[0] < 0) particles[i].pos[0] = 0; 00231 if (particles[i].pos[0] > 255) particles[i].pos[0] = 255; 00232 00233 if (particles[i].pos[1] < 0) particles[i].pos[1] = 0; 00234 if (particles[i].pos[1] > 255) particles[i].pos[1] = 255; 00235 } 00236 00237 00238 LINFO("Evaluate importance weight"); 00239 //Evaluate each particle 00240 for(uint i=0; i<particles.size(); i++) 00241 { 00242 Image<PixRGB<byte> > modelImg = drawObject(particles[i]); 00243 Image<float> diffImg = likelyhood(worldImg, modelImg); 00244 float diff = sum(diffImg); 00245 float lik = 1/diff; 00246 particles[i].lik *= lik; 00247 } 00248 00249 //normalize the weight 00250 normalize(particles); 00251 00252 00253 float sum = 0; 00254 for(uint i=0; i<particles.size(); i++) 00255 sum += (particles[i].lik)*(particles[i].lik); 00256 00257 LINFO("Effective N: %f ", 1/sum); 00258 for(uint i=0; i<particles.size(); i++) 00259 printf("%0.2f ", particles[i].lik); 00260 printf("\n"); 00261 00262 //selection step 00263 LINFO("resample particles"); 00264 int maxParticle = resample(particles); 00265 00266 LINFO("Show results"); 00267 Image<float> likImg = getLikImg(particles); 00268 00269 //Show the most probable object 00270 00271 Image<PixRGB<byte> > probImg = drawObject(particles[maxParticle]); 00272 00273 Layout<PixRGB<byte> > outDisp; 00274 outDisp = vcat(outDisp, hcat(worldImg, toRGB(Image<byte>(likImg)))); 00275 outDisp = vcat(outDisp, hcat(worldImg, probImg)); 00276 00277 ofs->writeRgbLayout(outDisp, "Result", FrameInfo("Result", SRC_POS)); 00278 getchar(); 00279 } 00280 00281 //while(run){ 00282 00283 00284 // Layout<PixRGB<byte> > outDisp; 00285 // XEvent event = vp.initFrame(); 00286 // handleEvents(event, objState); 00287 00288 // drawObject(vp, objState); 00289 00290 // run = vp.updateFrame(); 00291 // Image<PixRGB<byte> > worldImg = flipVertic(vp.getFrame()); 00292 // inputImg = rescale(input.asRgb(), worldImg.getDims()); 00293 00294 // Image<float> distmap; 00295 // Image<byte> foamask = Image<byte>(); 00296 00297 // Point2D<int> foa(145,65); 00298 // Rectangle foaRec = segmentColor(inputImg, foa, 00299 // 0, 0.1, 10, 200, distmap, foamask); 00300 // drawRect(inputImg, foaRec, PixRGB<byte>(255,0,0)); 00301 00302 00303 // //World 00304 // Image<float> priorMag, priorOri; 00305 // Image<byte> lum = luminance(worldImg); 00306 // gradientSobel(lum, priorMag, priorOri, 3); 00307 00308 // outDisp = vcat(outDisp, hcat(worldImg, toRGB(Image<byte>(priorMag)))); 00309 00310 // //Real 00311 // lum = luminance(inputImg); 00312 // Image<float> dataMag, dataOri; 00313 // gradientSobel(lum, dataMag, dataOri, 3); 00314 // dataMag = dataMag * foamask; 00315 00316 // outDisp = vcat(outDisp, hcat(inputImg, toRGB(Image<byte>(dataMag)))); 00317 00318 // Image<float> comb = priorMag + priorMag; 00319 00320 // Image<float> diffImg = edgeDist(dataMag, dataOri, priorMag, priorOri); 00321 // //dataMag, dataOri); 00322 // LINFO("Diff %f \n", sum(diffImg)); 00323 00324 00325 // outDisp = vcat(outDisp, hcat(toRGB(Image<byte>(comb)), toRGB(Image<byte>(diffImg)))); 00326 00327 // ofs->writeRgbLayout(outDisp, "Edges", FrameInfo("Edges", SRC_POS)); 00328 // usleep(1000); 00329 //} 00330 00331 exit(0); 00332 00333 } 00334 00335