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 #include "Neuro/ShapeEstimator.H"
00038
00039 #include "Component/ModelOptionDef.H"
00040 #include "Component/GlobalOpts.H"
00041 #include "Channels/ChannelMaps.H"
00042 #include "Component/OptionManager.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/FilterOps.H"
00045 #include "Image/ImageSet.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/MorphOps.H"
00048 #include "Image/Pixels.H"
00049 #include "Image/Point2D.H"
00050 #include "Image/Transforms.H"
00051 #include "Neuro/NeuroOpts.H"
00052 #include "Neuro/NeuroSimEvents.H"
00053 #include "Neuro/VisualCortex.H"
00054 #include "Neuro/SpatialMetrics.H"
00055 #include "Simulation/SimEventQueue.H"
00056 #include "Transport/FrameInfo.H"
00057 #include "Transport/FrameOstream.H"
00058 #include "Util/Assert.H"
00059 #include "Util/log.H"
00060 #include "Util/sformat.H"
00061 #include "Util/TextLog.H"
00062
00063 #include "Raster/Raster.H"
00064 #include "Image/ShapeOps.H"
00065 #include "Image/CutPaste.H"
00066 #include "Util/Timer.H"
00067
00068 #define SIGMA 20
00069 #define CLAMP 0.4f
00070
00071 const ModelOptionDef OPT_ShapeEstimatorUseLargeNeigh =
00072 { MODOPT_FLAG, "ShapeEstimatorUseLargeNeigh", &MOC_BRAIN, OPTEXP_CORE,
00073 "Use a larger 3x3 neighborhood to track down a local maximum across "
00074 "scales when true, otherwise use a 2x2 neighborhood.",
00075 "shape-estim-largeneigh", '\0', "", "true" };
00076
00077
00078 ShapeEstimator::ShapeEstimator(OptionManager& mgr,
00079 const std::string& descrName,
00080 const std::string& tagName,
00081 const nub::soft_ref<VisualCortex> vcx) :
00082 SimModule(mgr, descrName, tagName),
00083 SIMCALLBACK_INIT(SimEventWTAwinner),
00084 SIMCALLBACK_INIT(SimEventSaveOutput),
00085 itsMetrics(new SpatialMetrics(mgr)),
00086 itsLogFile(&OPT_TextLogFile, this),
00087 itsMode(&OPT_ShapeEstimatorMode, this),
00088 itsSmMethod(&OPT_ShapeEstimatorSmoothMethod, this),
00089 itsSaveObjMask(&OPT_BrainSaveObjMask, this),
00090 itsUseLargeNeigh(&OPT_ShapeEstimatorUseLargeNeigh, this),
00091 structEl(), itsSmoothMask(), itsCumMask(),
00092 itsSalientRegionSegmenter(new SalientRegionSegmenter())
00093 {
00094 this->addSubComponent(itsMetrics);
00095 }
00096
00097
00098
00099 void ShapeEstimator::reset1()
00100 {
00101
00102 itsSmoothMask.freeMem();
00103 itsCumMask.freeMem();
00104
00105
00106 ModelComponent::reset1();
00107 }
00108
00109
00110 void ShapeEstimator::onSimEventWTAwinner
00111 (SimEventQueue& q, rutz::shared_ptr<SimEventWTAwinner>& e)
00112 {
00113
00114 if (itsMode.getVal() == SEMnone) return;
00115
00116
00117 const Point2D<int> winner = e->winner().p;
00118
00119
00120
00121
00122 NamedImage<float> winmap; Point2D<int> winloc(-1, -1);
00123
00124
00125 Dims indims;
00126 if (SeC<SimEventRetinaImage> ee = q.check<SimEventRetinaImage>(this, SEQ_ANY))
00127 indims = ee->frame().getDims();
00128 else
00129 LFATAL("Oooops, we have a WTA winner "
00130 "but no retina image in the SimEventQueue");
00131
00132
00133 itsSmoothMask.freeMem();
00134
00135
00136 rutz::shared_ptr<SimReqVCXmaps> vcxm(new SimReqVCXmaps(this));
00137 q.request(vcxm);
00138 rutz::shared_ptr<ChannelMaps> chm = vcxm->channelmaps();
00139
00140
00141
00142 if(itsMode.getVal() == SEMMTfeatureMap)
00143 {
00144 getMotionShapeEstimatorMap(q,e);
00145 return;
00146 }
00147
00148 getShapeEstimatorMap(q,e);
00149
00150
00151 if (itsMode.getVal() == SEMsaliencyMap)
00152 {
00153 winmap = chm->getMap();
00154 locateLocalMax(winmap, winner, indims, winloc);
00155 }
00156 else
00157 {
00158
00159 float mx = 0.0F; rutz::shared_ptr<ChannelMaps> chan;
00160
00161 for (uint i = 0; i < chm->numSubchans(); ++i)
00162 {
00163 rutz::shared_ptr<ChannelMaps> ch = chm->subChanMaps(i);
00164 NamedImage<float> output = ch->getMap();
00165 if (output.initialized())
00166 {
00167 Point2D<int> w;
00168 const float val = locateLocalMax(output, winner, indims, w);
00169 LINFO("Examining: %s, val = %g", output.name().c_str(), val);
00170
00171
00172 if (val > mx) { winmap = output; mx = val; chan = ch; winloc = w; }
00173 }
00174 }
00175
00176
00177 if (mx == 0.0F)
00178 {
00179 winmap = chm->getMap();
00180 locateLocalMax(winmap, winner, indims, winloc);
00181 }
00182 else
00183 {
00184
00185
00186
00187
00188
00189
00190
00191
00192 if (itsMode.getVal() != SEMconspicuityMap)
00193 {
00194
00195 mx = 0.0F; Point2D<int> winloc2(0, 0); NamedImage<float> winmap2;
00196 for (uint i = 0; i < chan->numSubmaps(); ++i)
00197 {
00198 NamedImage<float> submap = chan->getSubmap(i);
00199 if (submap.initialized())
00200 {
00201 Point2D<int> w;
00202 const float val = locateLocalMax(submap, winloc, winmap.getDims(), w);
00203 LINFO("Examining: %s, val = %g", submap.name().c_str(), val);
00204
00205
00206 if (val > mx) { winmap2 = submap; mx = val; winloc2 = w; }
00207 }
00208 }
00209
00210
00211
00212
00213
00214 if (mx > 0.0F) { winmap = winmap2; winloc = winloc2; }
00215 }
00216 }
00217 }
00218 postShapeEstimatorMask(winmap, winloc, winner, indims, q);
00219 }
00220
00221
00222
00223 void ShapeEstimator::postShapeEstimatorMask
00224 (NamedImage<float> winmap, Point2D<int> winloc,
00225 Point2D<int> winner, Dims indims, SimEventQueue& q)
00226 {
00227
00228
00229
00230 ASSERT(winmap.initialized());
00231
00232
00233 Image<byte> objmask;
00234 Image<float> winmapnormalized = winmap;
00235 inplaceNormalize(winmapnormalized, 0.0F, 1.0F);
00236
00237
00238
00239 const bool goodseed = (winmap.getVal(winloc) > 0.0F);
00240 if (goodseed)
00241 {
00242 LDEBUG("Segmenting object around (%d, %d) in %s.",
00243 winner.i, winner.j, winmap.name().c_str());
00244 objmask = segmentObjectClean(winmapnormalized, winloc);
00245 }
00246 else
00247 {
00248 LDEBUG("Drawing disk object around (%d, %d) in %s.",
00249 winner.i, winner.j, winmap.name().c_str());
00250 objmask.resize(winmap.getDims(), true);
00251 drawDisk(objmask, winloc,
00252 (itsMetrics->getFOAradius() * objmask.getWidth()) / indims.w(),
00253 byte(255));
00254 }
00255
00256
00257 switch(itsSmMethod.getVal())
00258 {
00259 case SESMgaussian:
00260 {
00261 Image<float> temp = scaleBlock(objmask, indims);
00262 itsSmoothMask = convGauss<float>(temp, SIGMA, SIGMA, 5);
00263 inplaceNormalize(itsSmoothMask, 0.0F, 3.0F);
00264 inplaceClamp(itsSmoothMask, 0.0F, 1.0F);
00265 break;
00266 }
00267
00268 case SESMchamfer:
00269 {
00270 if (structEl.initialized() == false)
00271 {
00272
00273 const int ss = 8;
00274 structEl = Image<byte>(ss+ss,ss+ss, ZEROS);
00275 drawDisk(structEl, Point2D<int>(ss,ss), ss, byte(1));
00276 }
00277
00278 const byte cutoff = 100;
00279 Image<byte> temp = scaleBlock(objmask, indims);
00280 temp = chamfer34(openImg(temp, structEl), cutoff);
00281 itsSmoothMask = binaryReverse(Image<float>(temp), 255.0F);
00282 inplaceNormalize(itsSmoothMask, 0.0F, 1.0F);
00283 break;
00284 }
00285
00286 case SESMnone:
00287 {
00288 itsSmoothMask = scaleBlock(objmask, indims) / 255.0F;
00289 break;
00290 }
00291
00292 default: LFATAL("Unknown Smoothing Method");
00293 }
00294
00295
00296 if (itsCumMask.initialized()) itsCumMask = takeMax(itsCumMask, itsSmoothMask);
00297 else itsCumMask = itsSmoothMask;
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 Image<byte> objmask2(objmask);
00311 objmask2.setVal((winner.i * objmask.getWidth()) / indims.w(),
00312 (winner.j * objmask.getHeight()) / indims.h(),
00313 byte(255));
00314 Image<byte> iormask = lowPass3(objmask2) * (winmapnormalized + 0.25F);
00315
00316
00317 rutz::shared_ptr<SimEventShapeEstimatorOutput>
00318 e(new SimEventShapeEstimatorOutput(this, winmap, objmask, iormask, itsSmoothMask, itsCumMask,
00319 winmap.name(), goodseed));
00320
00321
00322
00323
00324 Raster::waitForKey();
00325 uint width = itsSmoothMask.getWidth();
00326 uint height = itsSmoothMask.getHeight();
00327 uint scale = 4;
00328 if(itsWin.is_invalid())
00329 itsWin.reset(new XWinManaged(Dims(width*3,height*2), 0, 0, "ShapeEst"));
00330 itsWin->setDims(Dims(width*3, height*2));
00331
00332 LINFO("%d %d", winmap.getWidth(), winmap.getHeight());
00333 LINFO("%d %d", objmask.getWidth(), objmask.getHeight());
00334 LINFO("%d %d", iormask.getWidth(), iormask.getHeight());
00335 LINFO("%d %d", itsSmoothMask.getWidth(), itsSmoothMask.getHeight());
00336 LINFO("%d %d", itsCumMask.getWidth(), itsCumMask.getHeight());
00337
00338 Image<float> disp(width*3, height*2, NO_INIT);
00339
00340 Image<float> dwinmap = zoomXY(winmap,scale);
00341 inplaceNormalize(dwinmap, 0.0F, 1.0F);
00342 inplacePaste(disp, dwinmap, Point2D<int>(0,0));
00343
00344 Image<float> dobjmask = zoomXY(objmask,scale);
00345 inplaceNormalize(dobjmask, 0.0F, 1.0F);
00346 inplacePaste(disp, dobjmask, Point2D<int>(width,0));
00347
00348 Image<float> diormask = zoomXY(iormask,scale);
00349 inplaceNormalize(diormask, 0.0F, 1.0F);
00350 inplacePaste(disp, diormask, Point2D<int>(2*width,0));
00351
00352 Image<float> dsmmask = itsSmoothMask;
00353 inplaceNormalize(dsmmask, 0.0F, 1.0F);
00354 inplacePaste(disp, dsmmask, Point2D<int>(0,height));
00355
00356 Image<float> dcummask = itsCumMask;
00357 inplaceNormalize(dcummask, 0.0F, 1.0F);
00358 inplacePaste(disp, dcummask, Point2D<int>(width, height));
00359
00360 itsWin->drawImage(disp,0,0);
00361
00362 Raster::waitForKey();
00363
00364
00365
00366
00367 q.post(e);
00368
00369
00370 const std::string msg =
00371 sformat("(%d,%d) %s %s",
00372 winner.i, winner.j, winmap.name().c_str(),
00373 goodseed ? "[Shape]" : "[Disk]");
00374
00375 textLog(itsLogFile.getVal(), "ShapeEstimator", msg);
00376 LINFO("Shape estimated %s", msg.c_str());
00377 }
00378
00379
00380 float ShapeEstimator::locateLocalMax(const Image<float>& submap,
00381 const Point2D<int>& winner,
00382 const Dims& fulldims,
00383 Point2D<int>& winloc,
00384 int mini, int maxi)
00385 {
00386
00387
00388 winloc.i = (winner.i * submap.getWidth()) / fulldims.w();
00389 winloc.j = (winner.j * submap.getHeight()) / fulldims.h();
00390
00391
00392
00393 float maxval = 0.0F; std::vector<Point2D<int> > surr;
00394
00395 if (itsUseLargeNeigh.getVal()) mini = -1;
00396
00397 for (int j = mini; j <= maxi; ++j)
00398 for (int i = mini; i <= maxi; ++i)
00399 surr.push_back(winloc + Point2D<int>(i, j));
00400
00401 for (uint i = 0; i < surr.size(); ++i)
00402 {
00403 surr[i].clampToDims(submap.getDims());
00404 const float val = submap.getVal(surr[i]);
00405 if (val > maxval) { winloc = surr[i]; maxval = val; }
00406 }
00407 return maxval;
00408 }
00409
00410
00411 void ShapeEstimator::
00412 onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00413 {
00414 if (itsSaveObjMask.getVal())
00415 {
00416
00417
00418 nub::ref<FrameOstream> ofs = dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00419
00420 if (itsSmoothMask.initialized())
00421 {
00422 const Image<byte> om = itsSmoothMask * 255.0F;
00423 ofs->writeGray(om, "OBJ", FrameInfo("ShapeEstimator object mask", SRC_POS));
00424 }
00425 else
00426 {
00427 if (SeC<SimEventRetinaImage> ee = q.check<SimEventRetinaImage>(this, SEQ_ANY))
00428 ofs->writeGray(Image<byte>(ee->frame().getDims(), ZEROS), "OBJ",
00429 FrameInfo("ShapeEstimator object mask", SRC_POS));
00430 }
00431 }
00432 }
00433
00434
00435 void ShapeEstimator::getMotionShapeEstimatorMap
00436 (SimEventQueue& q, rutz::shared_ptr<SimEventWTAwinner>& e)
00437 {
00438
00439 const Point2D<int> winner = e->winner().p;
00440 LINFO("SEMMTfeatureMap: winner: %d %d", winner.i, winner.j);
00441
00442
00443
00444
00445 NamedImage<float> winmap; Point2D<int> winloc(-1, -1);
00446
00447
00448 Dims indims;
00449 if (SeC<SimEventRetinaImage> ee = q.check<SimEventRetinaImage>(this, SEQ_ANY))
00450 indims = ee->frame().getDims();
00451 else LFATAL("Oooops, we have a WTA winner but no retina image in the SimEventQueue");
00452
00453 SeC<SimEventRetinaImage> ee = q.check<SimEventRetinaImage>(this, SEQ_ANY);
00454 Image<PixRGB<byte> > image = ee->frame().colorByte();
00455
00456
00457 uint width = image.getWidth();
00458 uint height = image.getHeight();
00459
00460 if(itsWin.is_invalid())
00461 itsWin.reset(new XWinManaged(Dims(width*3,height*2), 0, 0, "ShapeEstObj"));
00462 itsWin->setDims(Dims(width*3, height*2));
00463
00464 Image<PixRGB<byte> > disp = image;
00465 drawCross(disp, winner, PixRGB<byte>(255,0,0), 10, 2);
00466 itsWin->drawImage(disp,0,0);
00467 LINFO("Input Image: %d %d", image.getWidth(), image.getHeight());
00468 Raster::waitForKey();
00469
00470
00471 itsSmoothMask.freeMem();
00472
00473
00474 rutz::shared_ptr<SimReqVCXmaps> vcxm(new SimReqVCXmaps(this));
00475 q.request(vcxm);
00476 rutz::shared_ptr<ChannelMaps> chm = vcxm->channelmaps();
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 float mx = 0.0F; rutz::shared_ptr<ChannelMaps> chan;
00498 uint jmax = 0;
00499
00500
00501 for (uint i = 0; i < chm->numSubchans(); ++i)
00502 {
00503 rutz::shared_ptr<ChannelMaps> ch = chm->subChanMaps(i);
00504 NamedImage<float> output = ch->getMap();
00505
00506
00507
00508
00509
00510
00511
00512 if(output.name().find(std::string("motionSpatioTemporal")) !=
00513 std::string::npos)
00514 {
00515
00516 uint nSubchans = ch->numSubchans();
00517 LINFO("num subchans: %d", nSubchans);
00518
00519 int cDir = -1;
00520 for(uint j = 0; j < nSubchans; j++)
00521 {
00522 rutz::shared_ptr<ChannelMaps> sch = ch->subChanMaps(j);
00523 NamedImage<float> rawcsmap = sch->getRawCSmap(0);
00524
00525 std::string rcsname = rawcsmap.name();
00526 int fupos = rcsname.find_first_of('_') +1;
00527 std::string rcsname2 = rcsname.substr(fupos);
00528 int supos = rcsname2.find_first_of('_')+ fupos;
00529 std::string number = rcsname.substr(fupos,supos-fupos).c_str();
00530
00531 int dir = atoi(number.c_str());
00532
00533 if(cDir < dir)
00534 {
00535 cDir = dir;
00536
00537 Image<float> drawcsmap = zoomXY(rawcsmap, 4);
00538 inplaceNormalize(drawcsmap, 0.0F, 1.0F);
00539 itsWin->drawImage(drawcsmap, 0,0);
00540 LINFO("RCSMAP[%3d][%3d]:[%d %d]: %s",
00541 i,j, rawcsmap.getWidth(), rawcsmap.getHeight(),
00542 rawcsmap.name().c_str());
00543 Raster::waitForKey();
00544
00545
00546 if (output.initialized())
00547 {
00548 Point2D<int> w;
00549 const float val = locateLocalMax(rawcsmap, winner, indims, w, -1, 1);
00550 LINFO("Examining: %s, val = %g", rawcsmap.name().c_str(), val);
00551
00552
00553 if (val > mx)
00554 {
00555 LINFO("j: %d", j); jmax = j;
00556 winmap = rawcsmap; mx = val; chan = ch; winloc = w;
00557 }
00558 }
00559 }
00560 }
00561 }
00562 }
00563
00564
00565 if (mx == 0.0F)
00566 {
00567 winmap = chm->getMap();
00568 locateLocalMax(winmap, winner, indims, winloc);
00569 LINFO("did not find a max in the map");
00570 }
00571
00572 LINFO("jmax: %d mx: %f", jmax, mx);
00573 postShapeEstimatorMask(winmap, winloc, winner, indims, q);
00574
00575 Raster::waitForKey();
00576 }
00577
00578
00579 void ShapeEstimator::getShapeEstimatorMap
00580 (SimEventQueue& q, rutz::shared_ptr<SimEventWTAwinner>& e)
00581 {
00582 Timer t(1000000); t.reset();
00583
00584
00585 const Point2D<int> winner = e->winner().p;
00586
00587
00588
00589
00590 NamedImage<float> winmap; Point2D<int> winloc(-1, -1);
00591
00592
00593 Dims indims;
00594 if (SeC<SimEventRetinaImage> ee = q.check<SimEventRetinaImage>(this, SEQ_ANY))
00595 indims = ee->frame().getDims();
00596 else LFATAL("Oooops, we have a WTA winner but no retina image in the SimEventQueue");
00597
00598 SeC<SimEventRetinaImage> ee = q.check<SimEventRetinaImage>(this, SEQ_ANY);
00599 Image<PixRGB<byte> > image = ee->frame().colorByte();
00600
00601 uint width = image.getWidth();
00602 uint height = image.getHeight();
00603 uint scale = 16;
00604 if(itsWin.is_invalid())
00605 itsWin.reset(new XWinManaged(Dims(width*3,height*2), 0, 0, "ShapeEstObj"));
00606 itsWin->setDims(Dims(width*3, height*2));
00607
00608 Image<PixRGB<byte> > disp = image;
00609 drawCross(disp, winner, PixRGB<byte>(255,0,0), 10, 1);
00610 drawRect(disp,
00611 Rectangle(Point2D<int>(winner.i-8, winner.j-8), Dims(16, 16)),
00612 PixRGB<byte>(255,255,0));
00613
00614 itsWin->drawImage(disp,0,0);
00615 LINFO("winner: %d %d in [%3d %3d]",
00616 winner.i, winner.j, image.getWidth(), image.getHeight());
00617
00618
00619 itsSmoothMask.freeMem();
00620
00621
00622 rutz::shared_ptr<SimReqVCXmaps> vcxm(new SimReqVCXmaps(this));
00623 q.request(vcxm);
00624 rutz::shared_ptr<ChannelMaps> chm = vcxm->channelmaps();
00625
00626 winmap = chm->getMap();
00627 locateLocalMax(winmap, winner, indims, winloc);
00628
00629 Image<float> dwinmap = zoomXY(winmap,scale);
00630 inplaceNormalize(dwinmap, 0.0F, 1.0F);
00631 itsWin->drawImage(dwinmap,0,0);
00632
00633
00634
00635 rutz::shared_ptr<ChannelMaps> chan;
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718 ASSERT(winmap.initialized());
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 if(!itsSalientRegionSegmenter->getImage().initialized())
00755 itsSalientRegionSegmenter->setImage(image);
00756
00757 Image<float> segObjImage =
00758 itsSalientRegionSegmenter->getSalientRegion(winner);
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828 uint64 t2 = t.get();
00829 LINFO("time: %f", t2/1000.0);
00830 }
00831
00832
00833
00834
00835
00836