00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Component/ModelManager.H"
00039 #include "Image/Image.H"
00040 #include "Image/DrawOps.H"
00041 #include "GUI/DebugWin.H"
00042 #include "Robots/RobotBrain/InferotemporalCortexI.H"
00043
00044
00045 #include "Ice/IceImageUtils.H"
00046
00047
00048 InferotemporalCortexI::InferotemporalCortexI(OptionManager& mgr,
00049 const std::string& descrName, const std::string& tagName) :
00050 ModelComponent(mgr, descrName, tagName),
00051 itsObjectMessage(new RobotSimEvents::ObjectMessage),
00052 itsAttendedRegionMessage(new RobotSimEvents::AttendedRegionMessage),
00053 itsTrainingRegionMessage(new RobotSimEvents::AttendedRegionMessage),
00054 itsUseColor(false)
00055
00056 {
00057 itsObjectMessage->id = -1;
00058 itsAttendedRegionMessage->objId = -1;
00059 itsAttendedRegionMessage->name.clear();
00060 itsAttendedRegionMessage->img.width = -1;
00061 itsAttendedRegionMessage->img.height = -1;
00062
00063 itsTrainingRegionMessage->objId = -1;
00064 itsTrainingRegionMessage->name.clear();
00065 itsTrainingRegionMessage->img.width = -1;
00066 itsTrainingRegionMessage->img.height = -1;
00067
00068 itsOfs = nub::ref<OutputFrameSeries>(new OutputFrameSeries(mgr));
00069 addSubComponent(itsOfs);
00070
00071 initVDB();
00072
00073 itsTrainingMode = false;
00074
00075
00076
00077 itsCurrentCameraParam.dims = Dims(320,240);
00078 float testObjWidth = 42;
00079 float testObjDistanceFromCam = 37;
00080 itsCurrentCameraParam.focalLength = (itsCurrentCameraParam.dims.w()*testObjDistanceFromCam)/testObjWidth;
00081 itsCurrentCameraParam.yaw = -0.5*M_PI/180.0;
00082
00083 LINFO("Focal length %f",
00084 itsCurrentCameraParam.focalLength);
00085
00086
00087
00088 itsMatchThresh = 4U * 4U;
00089 }
00090
00091
00092 InferotemporalCortexI::~InferotemporalCortexI()
00093 {
00094 SimEventsUtils::unsubscribeSimEvents(itsTopicsSubscriptions, itsObjectPrx);
00095 }
00096
00097
00098 void InferotemporalCortexI::init(Ice::CommunicatorPtr ic, Ice::ObjectAdapterPtr adapter)
00099 {
00100 Ice::ObjectPtr objPtr = this;
00101 itsObjectPrx = adapter->add(objPtr,
00102 ic->stringToIdentity("InferotemporalCortex"));
00103
00104
00105 IceStorm::TopicPrx topicPrx;
00106
00107 itsTopicsSubscriptions.push_back(SimEventsUtils::TopicInfo("AttendedRegionMessageTopic", topicPrx));
00108
00109 SimEventsUtils::initSimEvents(ic, itsObjectPrx, itsTopicsSubscriptions);
00110
00111 itsEventsPub = RobotSimEvents::EventsPrx::uncheckedCast(
00112 SimEventsUtils::getPublisher(ic, "LandmarksMessageTopic")
00113 );
00114
00115 IceUtil::ThreadPtr thread = this;
00116 thread->start();
00117
00118 usleep(10000);
00119 }
00120
00121
00122 bool InferotemporalCortexI::initVDB()
00123 {
00124 itsUseColor = false;
00125 itsVDBFile = "objects.vdb";
00126 LINFO("Loading VDB File: %s", itsVDBFile.c_str());
00127 itsVDB.loadFrom(itsVDBFile);
00128 LINFO("VDB File Loaded");
00129
00130 return true;
00131 }
00132
00133
00134 void InferotemporalCortexI::run()
00135 {
00136
00137 while(1)
00138 {
00139 evolve();
00140 usleep(10000);
00141 }
00142
00143 }
00144
00145
00146
00147 void InferotemporalCortexI::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg,
00148 const Ice::Current&)
00149 {
00150
00151
00152 if(eMsg->ice_isA("::RobotSimEvents::AttendedRegionMessage"))
00153 {
00154
00155
00156 if (itsTrainingMode)
00157 return;
00158 RobotSimEvents::AttendedRegionMessagePtr arMsg = RobotSimEvents::AttendedRegionMessagePtr::dynamicCast(eMsg);
00159
00160 {
00161
00162 if (arMsg->name.size() && !itsTrainingMode)
00163 {
00164 itsTrainingMode = true;
00165 itsTrainingRegionMessage = arMsg;
00166 } else {
00167 itsAttendedRegionMessage = arMsg;
00168 }
00169
00170
00171 }
00172
00173 }
00174 }
00175
00176
00177
00178
00179 void InferotemporalCortexI::evolve()
00180 {
00181 std::vector< rutz::shared_ptr<VisualObjectMatch> > matches;
00182
00183 IceUtil::Mutex::Lock lock(itsARMutex);
00184
00185 if (itsAttendedRegionMessage->img.width == -1 ||
00186 itsAttendedRegionMessage->img.height == -1)
00187 return;
00188
00189
00190 Image<PixRGB<byte> > arImg;
00191
00192 if(itsTrainingMode)
00193 {
00194 arImg = Ice2Image<PixRGB<byte> >(itsTrainingRegionMessage->img);
00195
00196 if (!arImg.initialized())
00197 return;
00198
00199
00200 rutz::shared_ptr<VisualObject>
00201 imageVO(new VisualObject("Retina Image", "NULL", arImg,
00202 Point2D<int>(-1,-1),
00203 std::vector<double>(),
00204 std::vector< rutz::shared_ptr<Keypoint> >(),
00205 itsUseColor));
00206
00207
00208 Image<PixRGB<byte> > keyImg = imageVO->getKeypointImage(1);
00209
00210
00211
00212 std::vector< rutz::shared_ptr<Keypoint> > keypoints;
00213 std::vector< rutz::shared_ptr<Keypoint> > imgKeypoints = imageVO->getKeypoints();
00214
00215
00216 Dims attDims(
00217 itsTrainingRegionMessage->attWidth,
00218 itsTrainingRegionMessage->attHeight
00219 );
00220
00221 Point2D<int> attTopLeft(
00222 itsTrainingRegionMessage->attTopLeft.i,
00223 itsTrainingRegionMessage->attTopLeft.j
00224 );
00225
00226 LINFO("Dims: %s TL: %s", convertToString(attDims).c_str(), convertToString(attTopLeft).c_str());
00227
00228 Rectangle attRegion(attTopLeft, attDims);
00229 drawRect(keyImg, attRegion, PixRGB<byte>(255,0,255), 2);
00230
00231 keypoints.clear();
00232
00233 for(uint i=0; i<imgKeypoints.size(); i++)
00234 {
00235 Point2D<int> keyPt(imgKeypoints.at(i)->getX(), imgKeypoints.at(i)->getY());
00236
00237 if(attRegion.contains(keyPt))
00238 {
00239 drawCircle(keyImg, keyPt, 2, PixRGB<byte>(0,0,255));
00240 rutz::shared_ptr<Keypoint> k(
00241 new Keypoint(
00242 imgKeypoints.at(i)->getOriFV(),
00243 imgKeypoints.at(i)->getX() - attTopLeft.i,
00244 imgKeypoints.at(i)->getY() - attTopLeft.j,
00245 imgKeypoints.at(i)->getS(),
00246 imgKeypoints.at(i)->getO(),
00247 imgKeypoints.at(i)->getM()
00248 )
00249 );
00250 keypoints.push_back(k);
00251 }
00252 }
00253
00254 rutz::shared_ptr<VisualObject>
00255 vo(new VisualObject("Retina Image",
00256 "NULL",
00257 crop(arImg, attTopLeft, attDims),
00258 Point2D<int>(-1,-1),
00259 std::vector<double>(),
00260 keypoints,
00261 itsUseColor,
00262 false
00263 )
00264 );
00265
00266
00267 Dims objSize(
00268 (int)((float)itsTrainingRegionMessage->objWidth * itsCurrentCameraParam.focalLength / 1000),
00269 (int)((float)itsTrainingRegionMessage->objHeight * itsCurrentCameraParam.focalLength / 1000));
00270
00271
00272 learnObject(vo, itsVDB.numObjects(), itsTrainingRegionMessage->name, objSize);
00273 LINFO("Learned Object:Id = %d, Name=%s Size=%ix%i",
00274 itsVDB.numObjects(),
00275 itsTrainingRegionMessage->name.c_str(),
00276 objSize.w(), objSize.h());
00277
00278
00279 itsOfs->writeRGB(keyImg, "VObject", FrameInfo("VObject", SRC_POS));
00280
00281 itsTrainingMode = false;
00282 }
00283 else
00284 {
00285 arImg = Ice2Image<PixRGB<byte> >(itsAttendedRegionMessage->img);
00286
00287 if (!arImg.initialized())
00288 return;
00289
00290 rutz::shared_ptr<VisualObject>
00291 vo(new VisualObject("Retina Image", "NULL", arImg,
00292 Point2D<int>(-1,-1),
00293 std::vector<double>(),
00294 std::vector< rutz::shared_ptr<Keypoint> >(),
00295 itsUseColor));
00296
00297 Image<PixRGB<byte> > keyImg = vo->getKeypointImage(1);
00298
00299 RobotSimEvents::LandmarksMessagePtr landmarksMessage = new RobotSimEvents::LandmarksMessage;
00300
00301
00302
00303
00304 itsVDB.getObjectMatches(vo,matches);
00305
00306 for(uint i=0; i<matches.size(); i++)
00307 {
00308 matches.at(i)->prune();
00309
00310 if(matches.at(i)->checkSIFTaffine(3.14/4.0, 30.0, 1.0)) {
00311 std::string objectName = matches.at(i)->getVoTest()->getName();
00312
00313 Point2D<int> tl, tr, br, bl;
00314
00315
00316 matches.at(i)->getTransfTestOutline(tl,tr,br,bl);
00317
00318
00319 drawLine(keyImg, tl, tr, PixRGB<byte>(0,0,255));
00320 drawLine(keyImg, tr, br, PixRGB<byte>(0,0,255));
00321 drawLine(keyImg, br, bl, PixRGB<byte>(0,0,255));
00322 drawLine(keyImg, bl, tl, PixRGB<byte>(0,0,255));
00323
00324
00325 int landmarkX = (tl.i + tr.i + br.i + bl.i)/4;
00326 float bearing = atan( ((itsCurrentCameraParam.dims.w()/2) - landmarkX) / itsCurrentCameraParam.focalLength );
00327 bearing += itsCurrentCameraParam.yaw;
00328
00329
00330 float theta, sx, sy, str;
00331 matches.at(i)->getSIFTaffine().decompose(theta, sx, sy, str);
00332 float range = (sx + sy)/2.0;
00333
00334
00335
00336
00337 RobotSimEvents::LandmarkInfo currLandmarkInfo;
00338 currLandmarkInfo.id = -1;
00339 currLandmarkInfo.name = objectName;
00340 currLandmarkInfo.prob = -1;
00341 currLandmarkInfo.range = range;
00342 currLandmarkInfo.bearing = bearing;
00343
00344
00345 landmarksMessage->landmarks.push_back(currLandmarkInfo);
00346
00347 writeText(keyImg, tl,
00348 objectName.c_str(),
00349 PixRGB<byte>(0,0,0),
00350 PixRGB<byte>(255,255,255),
00351 SimpleFont::FIXED(6));
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 }
00363 }
00364 itsEventsPub->updateMessage(landmarksMessage);
00365
00366 writeText(keyImg, Point2D<int>(0,0),
00367 (toStr<int>(matches.size()) + " matches").c_str(),
00368 PixRGB<byte>(0,0,0),
00369 PixRGB<byte>(255,255,255),
00370 SimpleFont::FIXED(6));
00371
00372
00373 itsOfs->writeRGB(keyImg, "VObject", FrameInfo("VObject", SRC_POS));
00374
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 }
00408
00409
00410
00411 RobotSimEvents::LandmarkInfo InferotemporalCortexI::getLandmarkInfo(rutz::shared_ptr<Keypoint> keyPoint)
00412 {
00413 float x = keyPoint->getX();
00414
00415 float bearing = atan( ((itsCurrentCameraParam.dims.w()/2) - x) / itsCurrentCameraParam.focalLength );
00416
00417
00418 RobotSimEvents::LandmarkInfo landmarkInfo;
00419 float prob;
00420 landmarkInfo.id = findKeypointID(keyPoint, prob);
00421 landmarkInfo.prob = prob;
00422 landmarkInfo.range = -1;
00423 landmarkInfo.bearing = bearing;
00424
00425
00426 return landmarkInfo;
00427
00428 }
00429
00430 int InferotemporalCortexI::findKeypointID(rutz::shared_ptr<Keypoint> keyPoint, float &prob)
00431 {
00432
00433
00434 if (itsKeypointsDB.size() == 0)
00435 {
00436 itsKeypointsDB.push_back(keyPoint);
00437 return itsKeypointsDB.size()-1;
00438 }
00439
00440
00441 std::vector<rutz::shared_ptr<Keypoint> >::const_iterator
00442 keyDBIter = itsKeypointsDB.begin(),
00443 bestMatch = itsKeypointsDB.begin(),
00444 keyDBStop = itsKeypointsDB.end();
00445
00446
00447 int distsq1 = (*keyDBIter)->maxDistSquared();
00448 int distsq2 = (*keyDBIter)->maxDistSquared();
00449
00450 int id = 0;
00451 int bestID = 0;
00452 while(keyDBIter != keyDBStop)
00453 {
00454 const int distsq = (*keyDBIter)->distSquared(keyPoint);
00455
00456
00457 if (distsq < distsq1)
00458 {
00459 distsq2 = distsq1;
00460 distsq1 = distsq;
00461 bestMatch = keyDBIter;
00462 bestID = id;
00463 }
00464 else if (distsq < distsq2)
00465 distsq2 = distsq;
00466
00467 ++keyDBIter;
00468 ++id;
00469 }
00470
00471
00472
00473
00474 if ( distsq1 < 100000 )
00475 {
00476 prob = distsq1;
00477 return (int)(bestMatch - itsKeypointsDB.begin());
00478 } else {
00479
00480 itsKeypointsDB.push_back(keyPoint);
00481 prob = -1;
00482 return itsKeypointsDB.size()-1;
00483 }
00484
00485 return -1;
00486
00487
00488 }
00489
00490
00491 void InferotemporalCortexI::learnObject(rutz::shared_ptr<VisualObject> vo, const int objId, const std::string name, const Dims objSize)
00492 {
00493 vo->setName(name);
00494 vo->setImageFname("NULL");
00495 vo->setObjectSize(objSize);
00496 itsVDB.addObject(vo, false);
00497 itsVDB.saveTo(itsVDBFile);
00498 }
00499
00500
00501 void InferotemporalCortexI::findObjects(const rutz::shared_ptr<VisualObject> vo)
00502 {
00503 std::vector< rutz::shared_ptr<VisualObjectMatch> > matches;
00504
00505 Image<PixRGB<byte> > keyImg = vo->getKeypointImage(1);
00506
00507
00508 const uint nmatches = itsVDB.getObjectMatches(vo, matches, VOMA_SIMPLE,
00509 100U,
00510 0.5F,
00511 0.5F,
00512 1.0F,
00513 3U,
00514 100U,
00515 false
00516 );
00517
00518 float score = 0;
00519 float avgScore = 0, affineAvgDist = 0;
00520 int nkeyp = 0;
00521 int objId = -1;
00522 if (nmatches > 0)
00523 {
00524 rutz::shared_ptr<VisualObject> obj;
00525 rutz::shared_ptr<VisualObjectMatch> vom;
00526 for (unsigned int i = 0; i < 1; ++i)
00527 {
00528 vom = matches[i];
00529 obj = vom->getVoTest();
00530 score = vom->getScore();
00531 nkeyp = vom->size();
00532 avgScore = vom->getKeypointAvgDist();
00533 affineAvgDist = vom->getAffineAvgDist();
00534
00535 objId = atoi(obj->getName().c_str()+3);
00536
00537
00538
00539
00540
00541 double dist = 0;
00542 for (int keyp=0; keyp<nkeyp; keyp++)
00543 {
00544 const KeypointMatch kpm = vom->getKeypointMatch(keyp);
00545
00546 float refX = kpm.refkp->getX();
00547 float refY = kpm.refkp->getY();
00548
00549 LDEBUG("Point %f,%f",
00550 refX, refY);
00551 drawCircle(keyImg, Point2D<int>((int)refX, (int)refY), 3, PixRGB<byte>(0,255,0));
00552
00553 float tstX = kpm.tstkp->getX();
00554 float tstY = kpm.tstkp->getY();
00555 dist += (refX-tstX) * (refX-tstX);
00556 dist += (refY-tstY) * (refY-tstY);
00557 }
00558
00559
00560 LDEBUG("### Object match with '%s' score=%f ID:%i Dist:%f",
00561 obj->getName().c_str(), vom->getScore(), objId, dist);
00562
00563 Point2D<int> tl, tr, br, bl;
00564 vom->getTransfTestOutline(tl, tr, br, bl);
00565
00566 itsObjectMessage->id = objId;
00567 itsObjectMessage->name = obj->getName();
00568 itsObjectMessage->score = vom->getScore();
00569 itsObjectMessage->nKeyp = nkeyp;
00570 itsObjectMessage->dist = dist;
00571 itsObjectMessage->tl.i = tl.i; itsObjectMessage->tl.j = tl.j;
00572 itsObjectMessage->tr.i = tr.i; itsObjectMessage->tr.j = tr.j;
00573 itsObjectMessage->br.i = br.i; itsObjectMessage->br.j = br.j;
00574 itsObjectMessage->bl.i = bl.i; itsObjectMessage->bl.j = bl.j;
00575
00576 itsEventsPub->updateMessage(itsObjectMessage);
00577
00578
00579 drawLine(keyImg, tl, tr, PixRGB<byte>(0, 255, 0));
00580 drawLine(keyImg, tr, br, PixRGB<byte>(0, 255, 0));
00581 drawLine(keyImg, br, bl, PixRGB<byte>(0, 255, 0));
00582 drawLine(keyImg, bl, tl, PixRGB<byte>(0, 255, 0));
00583
00584
00585
00586
00587 }
00588 }
00589 itsOfs->writeRGB(keyImg, "VObject", FrameInfo("VObject", SRC_POS));
00590 }
00591
00592
00593