00001 /*! @file ObjRec/test-handRec.C test for robot arm hand rec */ 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: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ArmControl/test-handRec.C $ 00035 // $Id: test-handRec.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 00039 #include "Component/ModelManager.H" 00040 #include "Image/Image.H" 00041 #include "Image/ImageSet.H" 00042 #include "Image/ShapeOps.H" 00043 #include "Image/CutPaste.H" 00044 #include "Image/DrawOps.H" 00045 #include "Image/FilterOps.H" 00046 #include "Image/ColorOps.H" 00047 #include "Image/Transforms.H" 00048 #include "Image/MathOps.H" 00049 #include "Image/Kernels.H" 00050 #include "Image/fancynorm.H" 00051 #include "Image/Layout.H" 00052 #include "Transport/FrameInfo.H" 00053 #include "Raster/Raster.H" 00054 #include "Raster/GenericFrame.H" 00055 #include "GUI/DebugWin.H" 00056 #include "Neuro/EnvVisualCortex.H" 00057 #include "Neuro/NeoBrain.H" 00058 #include "Media/FrameSeries.H" 00059 #include "Util/Timer.H" 00060 #include "Image/OpenCVUtil.H" 00061 #include "Devices/Scorbot.H" 00062 #include "GUI/XWinManaged.H" 00063 #include "RCBot/Motion/MotionEnergy.H" 00064 00065 #define UP 98 00066 #define DOWN 104 00067 #define LEFT 100 00068 #define RIGHT 102 00069 00070 00071 float w (float *p, int k) 00072 { 00073 int i; 00074 float x=0.0; 00075 00076 for (i=1; i<=k; i++) x += p[i]; 00077 return x; 00078 } 00079 00080 float u (float *p, int k) 00081 { 00082 int i; 00083 float x=0.0; 00084 00085 for (i=1; i<=k; i++) x += (float)i*p[i]; 00086 return x; 00087 } 00088 00089 float nu (float *p, int k, float ut, float vt) 00090 { 00091 float x, y; 00092 00093 y = w(p,k); 00094 x = ut*y - u(p,k); 00095 x = x*x; 00096 y = y*(1.0-y); 00097 if (y>0) x = x/y; 00098 else x = 0.0; 00099 return x/vt; 00100 } 00101 00102 00103 Image<float> segmentProb(const Image<float> &img) 00104 { 00105 Image<float> retImg = img; 00106 int hist[260]; 00107 float p[260]; 00108 00109 inplaceNormalize(retImg, 0.0F, 255.0F); 00110 00111 for(int i=0; i<260; i++) 00112 { 00113 hist[i] = 0; //set histogram to 0 00114 p[i] = 0; //set prob to 0 00115 } 00116 00117 for (int y=0; y<retImg.getHeight(); y++) 00118 for(int x=0; x<retImg.getWidth(); x++) 00119 { 00120 int val = (int)retImg.getVal(x,y); 00121 hist[val+1]++; 00122 } 00123 00124 //nomalize into a distribution 00125 for (int i=1; i<=256; i++) 00126 p[i] = (float)hist[i]/(float)retImg.getSize(); 00127 00128 //find the global mean 00129 float ut = 0.0; 00130 for(int i=1; i<=256; i++) 00131 ut += (float)i*p[i]; 00132 00133 //find the global variance 00134 float vt = 0.0; 00135 for(int i=1; i<=256; i++) 00136 vt += (i-ut)*(i-ut)*p[i]; 00137 00138 int j=-1, k=-1; 00139 for(int i=1; i<=256; i++) 00140 { 00141 if ((j<0) && (p[i] > 0.0)) j = i; //first index 00142 if (p[i] > 0.0) k = i; //last index 00143 } 00144 00145 float z = -1.0; 00146 int m = -1; 00147 for (int i=j; i<=k; i++) 00148 { 00149 float y = nu(p,i,ut,vt); //compute nu 00150 00151 if (y>=z) 00152 { 00153 z = y; 00154 m = i; 00155 } 00156 } 00157 00158 int t = m; 00159 00160 if (t < 0) 00161 LINFO("ERROR"); 00162 00163 //threshold 00164 for (int y=0; y<retImg.getHeight(); y++) 00165 for(int x=0; x<retImg.getWidth(); x++) 00166 { 00167 int val = (int)retImg.getVal(x,y); 00168 if (val < t) 00169 retImg.setVal(x,y,0); 00170 else 00171 retImg.setVal(x,y,255.0); 00172 } 00173 00174 00175 return retImg; 00176 } 00177 00178 Image<float> integralImage(const Image<float> &img) 00179 { 00180 00181 Image<float> integImg(img.getDims(), ZEROS); 00182 00183 int xDim = integImg.getWidth(); 00184 int yDim = integImg.getHeight(); 00185 00186 00187 float s[xDim]; 00188 for (int i=0; i<xDim; i++) s[i] = 0; 00189 00190 for(int y=0; y<yDim; y++) 00191 for(int x=0; x<xDim; x++) 00192 { 00193 float ii = x > 0 ? integImg.getVal(x-1, y) : 0; 00194 s[x] += img.getVal(x,y); 00195 integImg.setVal(x,y, ii+s[x]); 00196 } 00197 00198 return integImg; 00199 00200 00201 } 00202 00203 Image<float> getHaarFeature(Image<float> &integImg, int i) 00204 { 00205 00206 Image<float> fImg(integImg.getDims(), ZEROS); 00207 00208 /*int w = 2, h = 4; 00209 00210 for(int y=0; y<fImg.getHeight()-2*w; y++) 00211 for(int x=0; x<fImg.getWidth()-h; x++) 00212 { 00213 float left = integImg.getVal(x+w,y+h) + integImg.getVal(x,y) - 00214 (integImg.getVal(x+w,y) + integImg.getVal(x,y+h)); 00215 float right = integImg.getVal(x+(2*w),y+h) + integImg.getVal(x+w,y) - 00216 (integImg.getVal(x+(2*w),y) + integImg.getVal(x+w,y+h)); 00217 00218 float top = integImg.getVal(x,y) + integImg.getVal(x+h,y+w) - 00219 (integImg.getVal(x+h,y) + integImg.getVal(x,y+w)); 00220 float bottom = integImg.getVal(x,y+w) + integImg.getVal(x+h,y+(2*w)) - 00221 (integImg.getVal(x+h,y+w) + integImg.getVal(x,y+(2*w))); 00222 00223 00224 fImg.setVal(x,y, fabs(left-right) + fabs(top-bottom)); 00225 }*/ 00226 00227 int c = 6+i, s = 8+i; 00228 00229 int x = 320/2, y=240/2; 00230 00231 Rectangle rect(Point2D<int>(x,y),Dims(s,s)); 00232 drawRect(fImg, rect, float(255.0)); 00233 //for(int y=0; y<fImg.getHeight()-s; y++) 00234 // for(int x=0; x<fImg.getWidth()-s; x++) 00235 { 00236 int d = (s-c)/2; 00237 float center = integImg.getVal(x+d,y+d) + integImg.getVal(x+d+c,y+d+c) - 00238 (integImg.getVal(x+d,y+d+c) + integImg.getVal(x+d+c,y+d)); 00239 float surround = integImg.getVal(x,y) + integImg.getVal(x+s,y+s) - 00240 (integImg.getVal(x+s,y) + integImg.getVal(x,y+s)); 00241 00242 center /= c*2; 00243 surround /= s*2; 00244 //fImg.setVal(x,y, center-surround); 00245 //printf("%i %f\n", i, center-surround); 00246 } 00247 00248 00249 return fImg; 00250 00251 } 00252 00253 Image<float> getObj(Image<float> &integImg) 00254 { 00255 00256 //Image<float> fImg(integImg.getDims(), ZEROS); 00257 Image<float> objImg(integImg.getDims(), ZEROS); 00258 00259 /*int w = 2, h = 4; 00260 00261 for(int y=0; y<fImg.getHeight()-2*w; y++) 00262 for(int x=0; x<fImg.getWidth()-h; x++) 00263 { 00264 float left = integImg.getVal(x+w,y+h) + integImg.getVal(x,y) - 00265 (integImg.getVal(x+w,y) + integImg.getVal(x,y+h)); 00266 float right = integImg.getVal(x+(2*w),y+h) + integImg.getVal(x+w,y) - 00267 (integImg.getVal(x+(2*w),y) + integImg.getVal(x+w,y+h)); 00268 00269 float top = integImg.getVal(x,y) + integImg.getVal(x+h,y+w) - 00270 (integImg.getVal(x+h,y) + integImg.getVal(x,y+w)); 00271 float bottom = integImg.getVal(x,y+w) + integImg.getVal(x+h,y+(2*w)) - 00272 (integImg.getVal(x+h,y+w) + integImg.getVal(x,y+(2*w))); 00273 00274 00275 fImg.setVal(x,y, fabs(left-right) + fabs(top-bottom)); 00276 }*/ 00277 00278 for(int y=6; y<objImg.getHeight()-60; y++) 00279 for(int x=6; x<objImg.getWidth()-60; x++) 00280 { 00281 int c, s; 00282 float center, surround; 00283 for (int i=0; i<30; i++) 00284 { 00285 c = 3+(i/2); 00286 s = 6+i; 00287 00288 int d = (s-c)/2; 00289 center = integImg.getVal(x+d,y+d) + integImg.getVal(x+d+c,y+d+c) - 00290 (integImg.getVal(x+d,y+d+c) + integImg.getVal(x+d+c,y+d)); 00291 surround = integImg.getVal(x,y) + integImg.getVal(x+s,y+s) - 00292 (integImg.getVal(x+s,y) + integImg.getVal(x,y+s)) - center; 00293 00294 center /= (c*c); 00295 surround /= ((s*s) - (c*c)); 00296 if (fabs(surround-center) > 1) break; 00297 } 00298 00299 Rectangle rectC(Point2D<int>(x+((s-c)/2),y+((s-c)/2)),Dims(c,c)); 00300 Rectangle rectS(Point2D<int>(x,y),Dims(s,s)); 00301 00302 drawFilledRect(objImg, rectC, center); 00303 } 00304 00305 00306 return objImg; 00307 00308 } 00309 00310 float centerSurround(Image<byte> &img, Point2D<int> &p) 00311 { 00312 int s = 10; 00313 int c = 5; 00314 00315 float sum = 0; 00316 for(int y=p.j-s; y<p.j+s; y++) 00317 for(int x=p.i-s; x<p.i+s; x++) 00318 { 00319 if (p.i-c < x && x > p.i+c && 00320 p.j-c < y && y > p.j+c ) 00321 { 00322 sum += img.getVal(x,y); 00323 img.setVal(x,y,0); 00324 } 00325 } 00326 00327 00328 00329 return sum; 00330 } 00331 00332 00333 int main(const int argc, const char **argv) 00334 { 00335 00336 MYLOGVERB = LOG_INFO; 00337 ModelManager *mgr = new ModelManager("Test Brain"); 00338 00339 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr)); 00340 mgr->addSubComponent(ifs); 00341 00342 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr)); 00343 mgr->addSubComponent(ofs); 00344 00345 nub::soft_ref<Scorbot> scorbot(new Scorbot(*mgr)); 00346 mgr->addSubComponent(scorbot); 00347 00348 nub::soft_ref<NeoBrain> neoBrain(new NeoBrain(*mgr)); 00349 mgr->addSubComponent(neoBrain); 00350 00351 mgr->exportOptions(MC_RECURSE); 00352 00353 if (mgr->parseCommandLine( 00354 (const int)argc, (const char**)argv, "", 0, 0) == false) 00355 return 1; 00356 00357 mgr->start(); 00358 00359 Dims imageDims = ifs->peekDims(); 00360 neoBrain->init(imageDims, 50, 30); 00361 00362 XWinManaged* xwin = new XWinManaged(Dims(512,512), -1, -1, "Bot Control"); 00363 00364 // do post-command-line configs: 00365 00366 //start streaming 00367 //ifs->startStream(); 00368 00369 MotionEnergyPyrBuilder<byte> motionPyr(Gaussian5, 0.0f, 10.0f, 5, 00370 1500.0f); 00371 00372 Image< PixRGB<byte> > inputImg; 00373 FrameState is = ifs->updateNext(); 00374 //grab the images 00375 GenericFrame input = ifs->readFrame(); 00376 inputImg = input.asRgb(); 00377 00378 Image<byte> lum = luminance(inputImg); 00379 00380 while(1) 00381 { 00382 00383 00384 00385 00386 Point2D<int> clickLoc = xwin->getLastMouseClick(); 00387 int key = xwin->getLastKeyPress(); 00388 00389 if (clickLoc.isValid()) 00390 { 00391 float val = centerSurround(lum, clickLoc); 00392 printf("Val %f %i\n", val, lum.getVal(clickLoc)); 00393 00394 00395 } 00396 switch (key) 00397 { 00398 case UP: 00399 is = ifs->updateNext(); 00400 if (is == FRAME_COMPLETE) 00401 break; 00402 00403 //grab the images 00404 input = ifs->readFrame(); 00405 if (!input.initialized()) 00406 break; 00407 inputImg = input.asRgb(); 00408 lum = luminance(inputImg); 00409 break; 00410 case DOWN: 00411 break; 00412 case LEFT: 00413 break; 00414 case RIGHT: 00415 break; 00416 case 65: // space 00417 break; 00418 case 38: // a 00419 break; 00420 case 52: //z 00421 break; 00422 case 39: //s 00423 break; 00424 case 53: //x 00425 break; 00426 case 40: //d 00427 break; 00428 case 54: //c 00429 break; 00430 case 41: //f 00431 break; 00432 case 55: //v 00433 break; 00434 00435 default: 00436 if (key != -1) 00437 printf("Unknown Key %i\n", key); 00438 break; 00439 00440 } 00441 00442 00443 ofs->writeRGB(inputImg, "input", FrameInfo("input", SRC_POS)); 00444 00445 //mag = segmentProb(mag); 00446 00447 xwin->drawImage(lum); 00448 00449 } 00450 00451 00452 // stop all our ModelComponents 00453 mgr->stop(); 00454 00455 return 0; 00456 00457 }