00001 /*!@file Media/SceneGenerator.C generate test scenes */ 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 <elazary@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Media/SceneGenerator.C $ 00035 // $Id: SceneGenerator.C 9412 2008-03-10 23:10:15Z farhan $ 00036 // 00037 00038 #include "Media/SceneGenerator.H" 00039 #include "Raster/Raster.H" 00040 #include "Image/CutPaste.H" 00041 #include "Image/ShapeOps.H" 00042 #include "Image/DrawOps.H" 00043 #include "Util/FileUtil.H" // for isDirectory() 00044 00045 #include <dirent.h> 00046 00047 #include "GUI/XWinManaged.H" 00048 #include "GUI/DebugWin.H" 00049 00050 #include <vector> 00051 00052 // ####################################################################### 00053 SceneGenerator::SceneGenerator(SceneType sceneType, int width, int height) : 00054 itsSceneType(sceneType), itsWidth(width), itsHeight(height), 00055 itsALOIImages(NULL), 00056 itsCoilImages(NULL), 00057 itsTestImages(NULL), 00058 itsTargetsLoc(0), 00059 itsObjLocation(), 00060 itsTargetObject(-1), 00061 itsObjectSize(80), 00062 itsTargetColor(PixRGB<byte>(0,0,0)), 00063 itsLum(-1), 00064 itsRot(-1), 00065 itsColor(-1), 00066 itsNoise(-1), 00067 itsBackgroundColor(PixRGB<byte>(0,0,0)) 00068 { 00069 00070 initRandomNumbers(); 00071 00072 setSceneType(sceneType); 00073 } 00074 00075 // ####################################################################### 00076 SceneGenerator::~SceneGenerator() 00077 { 00078 if (itsALOIImages != NULL) 00079 delete itsALOIImages; 00080 00081 if (itsCoilImages != NULL) 00082 delete itsCoilImages; 00083 00084 if (itsTestImages != NULL) 00085 delete itsTestImages; 00086 00087 } 00088 00089 void SceneGenerator::setSceneType(SceneType sceneId) 00090 { 00091 00092 itsSceneType = sceneId; 00093 00094 //Could remove the itsALOIImages before to save on memory 00095 switch(itsSceneType) 00096 { 00097 case SIMPLE_EDGES: 00098 itsBackgroundColor = PixRGB<byte>(0,0,0); 00099 break; 00100 case SIMPLE_SHAPES: 00101 itsBackgroundColor = PixRGB<byte>(0,0,0); 00102 itsTargetColor = PixRGB<byte>(100, 100, 100); 00103 itsColor = PixRGB<byte>(200, 200, 200); 00104 itsTargetObject = 0; 00105 itsNoise = 30; 00106 break; 00107 case ALOI_OBJECTS: 00108 if (itsALOIImages == NULL) //load the images 00109 itsALOIImages = new TestImages("/lab/ilab15/tmp/objectsDB/png/png", 00110 TestImages::ALOI); 00111 itsBackgroundColor = PixRGB<byte>(15, 15, 8); 00112 itsTargetObject = 0; 00113 break; 00114 case COIL_OBJECTS: 00115 if (itsCoilImages == NULL) //load the images 00116 itsCoilImages = new TestImages("/lab/ilab15/tmp/objectsDB/coil/coil-100", 00117 TestImages::COIL); 00118 itsBackgroundColor = PixRGB<byte>(15, 15, 8); 00119 itsTargetObject = 0; 00120 break; 00121 case SATELLITE_KATRINA: 00122 readDirScenes("/lab/lior/images/croped"); 00123 break; 00124 case MIT_LABELME: 00125 if (itsTestImages != NULL) delete itsTestImages; //delete the old images 00126 readDirScenes("/lab/ilab15/tmp/objectsDB/mit/labelMe"); 00127 break; 00128 00129 00130 } 00131 00132 00133 } 00134 00135 // ####################################################################### 00136 void SceneGenerator::readDirScenes(const char *path) 00137 { 00138 00139 DIR *dp = opendir(path); 00140 dirent *dirp; 00141 while ((dirp = readdir(dp)) != NULL ) { 00142 switch(itsSceneType) 00143 { 00144 case SATELLITE_KATRINA: 00145 if (dirp->d_name[0] != '.') 00146 sceneFilenames.push_back(std::string(path) + '/' + std::string(dirp->d_name)); 00147 break; 00148 00149 case MIT_LABELME: 00150 //only add directories 00151 if (dirp->d_name[0] != '.' && 00152 isDirectory(dirp)) 00153 sceneFilenames.push_back(std::string(path) + '/' + std::string(dirp->d_name)); 00154 break; 00155 default: 00156 break; 00157 } 00158 00159 } 00160 LINFO("%"ZU" files in the directory\n", sceneFilenames.size()); 00161 00162 } 00163 00164 // ####################################################################### 00165 void SceneGenerator::setTargetObject(int obj) 00166 { 00167 itsTargetObject = obj; 00168 } 00169 00170 // ####################################################################### 00171 Image<PixRGB<byte> > SceneGenerator::getScene(int nobj, int lum, int col, int rot) 00172 { 00173 switch(itsSceneType) 00174 { 00175 case SIMPLE_EDGES: 00176 break; 00177 case SIMPLE_SHAPES: 00178 return generateShapeScene(); 00179 break; 00180 case ALOI_OBJECTS: 00181 return generateAloiObjectsScene(nobj, lum, col, rot); 00182 break; 00183 case COIL_OBJECTS: 00184 return generateCoilObjectsScene(nobj); 00185 break; 00186 case SATELLITE_KATRINA: 00187 return getSceneFile(); 00188 case MIT_LABELME: 00189 return getLabelMeScene(); 00190 break; 00191 } 00192 00193 return Image<PixRGB<byte> >(); //return an empty scene 00194 00195 } 00196 00197 // ####################################################################### 00198 Image<PixRGB<byte> > SceneGenerator::getSceneFile() 00199 { 00200 00201 static int index = 0; 00202 00203 index++; 00204 index = index % sceneFilenames.size(); 00205 00206 return Raster::ReadRGB(sceneFilenames[index]); 00207 } 00208 00209 00210 // ####################################################################### 00211 Image<PixRGB<byte> > SceneGenerator::getLabelMeScene() 00212 { 00213 00214 static int index = 0; 00215 00216 index++; 00217 index = index % sceneFilenames.size(); 00218 00219 if (itsTestImages != NULL) delete itsTestImages; 00220 itsTestImages = new TestImages(sceneFilenames[index].c_str(), 00221 TestImages::MIT_LABELME); 00222 00223 Image<PixRGB<byte> > scene = itsTestImages->getScene(0); 00224 00225 00226 scene = rescale(scene, itsWidth, itsHeight); 00227 return scene; 00228 } 00229 00230 // ####################################################################### 00231 Image<PixRGB<byte> > SceneGenerator::generateEdgeScene() 00232 { 00233 00234 00235 return Image<PixRGB<byte> >(); //return an empty scene 00236 } 00237 00238 // ####################################################################### 00239 Image<PixRGB<byte> > SceneGenerator::generateShapeScene() 00240 { 00241 00242 Image<PixRGB<byte> > sceneImg(itsWidth, itsHeight, NO_INIT); 00243 sceneImg.clear(itsBackgroundColor); //set the background; 00244 00245 itsTargetsLoc.clear(); 00246 00247 PixRGB<byte> color; 00248 00249 int idx = 0; int targetObjPos = randomUpToIncluding(8); 00250 for (int y=itsObjectSize+10; y<sceneImg.getHeight(); y += (itsObjectSize * 2)) 00251 for(int x=itsObjectSize+10; x<sceneImg.getWidth(); x += (itsObjectSize * 2)) 00252 { 00253 int objType = randomUpToIncluding(0); 00254 /*int lum = randomUpToIncluding(11) + 1; 00255 int col = randomUpToIncluding(13) + 1; 00256 int rot = randomUpToIncluding(10) + 1;*/ 00257 00258 if (idx == targetObjPos) //place the target object 00259 { 00260 itsTargetsLoc.push_back(Point2D<int>(x,y)); 00261 objType = itsTargetObject; 00262 color = itsTargetColor; 00263 //Add some noise 00264 } else { 00265 color = itsColor; 00266 } 00267 00268 int noise = randomUpToIncluding(itsNoise*2)-itsNoise; 00269 color[0] += noise; 00270 color[1] += noise; 00271 color[2] += noise; 00272 00273 00274 switch(objType) 00275 { 00276 case 0: 00277 drawDisk(sceneImg, Point2D<int>(x, y), itsObjectSize/2, color); 00278 break; 00279 case 1: 00280 drawCross(sceneImg, Point2D<int>(x, y), color, itsObjectSize/2); 00281 break; 00282 case 2: 00283 drawRectOR(sceneImg, Rectangle::tlbrI(y-(itsObjectSize/2), 00284 x-(itsObjectSize/2), 00285 y+(itsObjectSize/2), 00286 x+(itsObjectSize/2)), 00287 color, 1, M_PI/2); 00288 break; 00289 case 3: 00290 drawRectOR(sceneImg, Rectangle::tlbrI(y-(itsObjectSize/2), 00291 x-(itsObjectSize/2), 00292 y+(itsObjectSize/2), 00293 x+(itsObjectSize/2)), 00294 color); 00295 break; 00296 case 4: 00297 drawPatch(sceneImg, Point2D<int>(x, y), itsObjectSize/2, color); 00298 break; 00299 } 00300 00301 idx++; 00302 } 00303 00304 return sceneImg; //return an empty scene 00305 } 00306 00307 // ####################################################################### 00308 Image<PixRGB<byte> > SceneGenerator::generateAloiObjectsScene(int nobj, int lum, int col, int rot) 00309 { 00310 00311 Image<PixRGB<byte> > sceneImg(itsWidth, itsHeight, NO_INIT); 00312 sceneImg.clear(itsBackgroundColor); //set the background; 00313 00314 itsTargetsLoc.clear(); 00315 itsObjLocation.clear(); 00316 00317 int idx = 0; int targetObjPos = -1; //randomUpToIncluding(24); 00318 /*int rot = -2; 00319 //int lum = -2; 00320 static int lum = 0; 00321 lum = (lum + 1)%12; 00322 00323 static int col = 0; 00324 col = (col + 1)%24; col = -2;*/ 00325 00326 std::vector<int> objInScene; 00327 00328 int objType = 0; 00329 for (int y=itsObjectSize+10; y<sceneImg.getHeight(); y += (itsObjectSize * 2)) 00330 for(int x=itsObjectSize+10; x<sceneImg.getWidth(); x += (itsObjectSize * 2)) 00331 { 00332 bool exsist = true; 00333 00334 while(exsist) 00335 { 00336 objType = randomUpToIncluding(nobj-1); 00337 exsist = false; 00338 for(uint i=0; i<objInScene.size(); i++) 00339 { 00340 if (objType == objInScene[i]) 00341 { 00342 exsist = true; 00343 } 00344 } 00345 } 00346 00347 objInScene.push_back(objType); 00348 00349 //objType = (objType+1)%nobj ; 00350 00351 // if (itsLum < 0) 00352 // lum = randomUpToIncluding(11); 00353 00354 // if (itsColor[0] == 0) 00355 // col = randomUpToIncluding(23); 00356 00357 // if(itsRot < 0) 00358 // rot = randomUpToIncluding(40); 00359 00360 Image<PixRGB<byte> > obj; 00361 if (idx == targetObjPos || objType == itsTargetObject) //place the target object 00362 { 00363 itsTargetsLoc.push_back(Point2D<int>(x,y)); 00364 itsObjLocation.push_back(ObjectInfo(Point2D<int>(x,y), itsTargetObject)); 00365 obj = itsALOIImages->getObject(itsTargetObject, lum, col, rot); 00366 } else { //the distarctors 00367 itsObjLocation.push_back(ObjectInfo(Point2D<int>(x,y), objType)); 00368 obj = itsALOIImages->getObject(objType, lum, col, rot); 00369 } 00370 00371 obj = rescale(obj, itsObjectSize, itsObjectSize); 00372 inplacePaste(sceneImg, obj, Point2D<int>(x-itsObjectSize/2, y-itsObjectSize/2)); 00373 idx++; 00374 } 00375 00376 // SHOWIMG(sceneImg); 00377 // Raster::WriteRGB(sceneImg, "out.ppm"); 00378 return sceneImg; 00379 00380 00381 } 00382 00383 // ####################################################################### 00384 Image<PixRGB<byte> > SceneGenerator::generateCoilObjectsScene(int nobj) 00385 { 00386 00387 Image<PixRGB<byte> > sceneImg(itsWidth, itsHeight, NO_INIT); 00388 sceneImg.clear(itsBackgroundColor); //set the background; 00389 00390 itsTargetsLoc.clear(); 00391 00392 int idx = 0; int targetObjPos = randomUpToIncluding(8); 00393 int lum = -2, col = -2, rot = -2; 00394 for (int y=itsObjectSize+10; y<sceneImg.getHeight(); y += (itsObjectSize * 2)) 00395 for(int x=itsObjectSize+10; x<sceneImg.getWidth(); x += (itsObjectSize * 2)) 00396 { 00397 int objType = randomUpToIncluding(98) + 1; 00398 00399 if(itsColor[1] == 0) 00400 rot = randomUpToIncluding(71) + 1; 00401 00402 Image<PixRGB<byte> > obj; 00403 if (idx == targetObjPos || objType == itsTargetObject) //place the target object 00404 { 00405 itsTargetsLoc.push_back(Point2D<int>(x,y)); 00406 obj = itsCoilImages->getObject(itsTargetObject, lum, col, rot); 00407 } else { //the distarctors 00408 obj = itsCoilImages->getObject(objType, lum, col, rot); 00409 } 00410 00411 obj = rescale(obj, itsObjectSize, itsObjectSize); 00412 inplacePaste(sceneImg, obj, Point2D<int>(x-itsObjectSize/2, y-itsObjectSize/2)); 00413 idx++; 00414 } 00415 00416 return sceneImg; 00417 00418 00419 } 00420 // ####################################################################### 00421 bool SceneGenerator::checkTargetPos(const Point2D<int> &pt, const Dims &searchWin) 00422 { 00423 int x = pt.i; 00424 int y = pt.j; 00425 bool foundTarget = false; 00426 for(uint i=0; i<itsTargetsLoc.size(); i++){ 00427 if (itsTargetsLoc[i].i > x - (searchWin.w()/2) && 00428 itsTargetsLoc[i].i < x + (searchWin.w()/2) && 00429 itsTargetsLoc[i].j > y - (searchWin.h()/2) && 00430 itsTargetsLoc[i].j < y + (searchWin.h()/2) ){ 00431 LINFO("Object found at %ix%i T%i at %ix%i", 00432 x, y, i, itsTargetsLoc[i].i, itsTargetsLoc[i].j); 00433 foundTarget = true; 00434 break; 00435 } 00436 } 00437 00438 return foundTarget; 00439 00440 } 00441 00442 // ####################################################################### 00443 int SceneGenerator::getObjFromPos(const Point2D<int> &pt, const Dims &searchWin) 00444 { 00445 int x = pt.i; 00446 int y = pt.j; 00447 int objId = -1; 00448 for(uint i=0; i<itsObjLocation.size(); i++){ 00449 if (itsObjLocation[i].loc.i > x - (searchWin.w()/2) && 00450 itsObjLocation[i].loc.i < x + (searchWin.w()/2) && 00451 itsObjLocation[i].loc.j > y - (searchWin.h()/2) && 00452 itsObjLocation[i].loc.j < y + (searchWin.h()/2) ){ 00453 objId = itsObjLocation[i].objId; 00454 break; 00455 } 00456 } 00457 00458 return objId; 00459 00460 } 00461 00462 // ####################################################################### 00463 Point2D<int> SceneGenerator::getObjLocation(uint objId) 00464 { 00465 ASSERT(objId < itsObjLocation.size()); 00466 return itsObjLocation[objId].loc; 00467 00468 } 00469 00470 // ####################################################################### 00471 int SceneGenerator::getObjId(uint objId) 00472 { 00473 ASSERT(objId < itsObjLocation.size()); 00474 return itsObjLocation[objId].objId; 00475 00476 } 00477 00478 // ####################################################################### 00479 uint SceneGenerator::getNumObj() 00480 { 00481 return itsObjLocation.size(); 00482 } 00483 00484 // ####################################################################### 00485 const Point2D<int> SceneGenerator::getTargetPos() const 00486 { 00487 00488 if (itsTargetsLoc.size() > 0) 00489 return itsTargetsLoc[0]; 00490 else 00491 return Point2D<int>(); 00492 }; 00493 00494 // ####################################################################### 00495 void SceneGenerator::setObjectSize(int size) 00496 { 00497 itsObjectSize = size; 00498 } 00499 00500 00501 // ####################################################################### 00502 void SceneGenerator::setTargetColor(PixRGB<byte> color) 00503 { 00504 itsTargetColor = color; 00505 } 00506 00507 // ####################################################################### 00508 void SceneGenerator::setLum(int lum) 00509 { 00510 itsLum = lum; 00511 } 00512 00513 // ####################################################################### 00514 void SceneGenerator::setRotation(int rot) 00515 { 00516 itsRot = rot; 00517 } 00518 00519 // ####################################################################### 00520 void SceneGenerator::setColor(PixRGB<byte> color) 00521 { 00522 itsColor = color; 00523 } 00524 00525 // ####################################################################### 00526 void SceneGenerator::setNoise(int level) 00527 { 00528 itsNoise = level; 00529 } 00530 00531 // ####################################################################### 00532 void SceneGenerator::setBackgroundColor(PixRGB<byte> color) 00533 { 00534 itsBackgroundColor = color; 00535 } 00536 00537 // ####################################################################### 00538 int SceneGenerator::getObjectSize() 00539 { 00540 return itsObjectSize; 00541 } 00542 00543 int SceneGenerator::getTargetObject() 00544 { 00545 return itsTargetObject; 00546 } 00547 00548 // ####################################################################### 00549 PixRGB<byte> SceneGenerator::getTargetColor() 00550 { 00551 return itsTargetColor; 00552 } 00553 00554 // ####################################################################### 00555 int SceneGenerator::getLum() 00556 { 00557 return itsLum; 00558 } 00559 00560 // ####################################################################### 00561 int SceneGenerator::getRotation() 00562 { 00563 return itsRot; 00564 } 00565 00566 // ####################################################################### 00567 PixRGB<byte> SceneGenerator::getColor() 00568 { 00569 return itsColor; 00570 } 00571 00572 // ####################################################################### 00573 int SceneGenerator::getNoise() 00574 { 00575 return itsNoise; 00576 } 00577 00578 // ####################################################################### 00579 PixRGB<byte> SceneGenerator::getBackgroundColor() 00580 { 00581 return itsBackgroundColor; 00582 } 00583 00584 SceneGenerator::SceneType SceneGenerator::getSceneType() 00585 { 00586 return itsSceneType; 00587 } 00588 00589 uint SceneGenerator::getNumSceneTypes() 00590 { 00591 return numSceneTypeNames; 00592 } 00593 00594 // ###################################################################### 00595 /* So things look consistent in everyone's emacs... */ 00596 /* Local Variables: */ 00597 /* indent-tabs-mode: nil */ 00598 /* End: */