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 #ifndef MOTIONSPATIOTEMPORALCHANNEL_C_DEFINED
00039 #define MOTIONSPATIOTEMPORALCHANNEL_C_DEFINED
00040
00041 #include "Channels/MotionSpatioTemporalChannel.H"
00042
00043 #include "Channels/ChannelOpts.H"
00044 #include "Channels/DirectionSpatioTemporalChannel.H"
00045 #include "Component/OptionManager.H"
00046 #include "rutz/trace.h"
00047 #include "Util/Timer.H"
00048 #include "Image/ShapeOps.H"
00049 #include "Image/FilterOps.H"
00050 #include "Image/DrawOps.H"
00051
00052 #define SELF_MOTION_WEIGHT 1.0F
00053 #define OBJECT_MOTION_WEIGHT 1.0F
00054 #define MAX_FIRING_RATE 100.0F
00055
00056
00057
00058
00059
00060
00061 MotionSpatioTemporalChannel::MotionSpatioTemporalChannel(OptionManager& mgr) :
00062 ComplexChannel(mgr,
00063 "MotionSpatioTemporal",
00064 "motionSpatioTemporal",
00065 MOTIONSPATIOTEMPORAL),
00066 itsPyrType("MotionChannelPyramidType", this, Gaussian5),
00067 itsNumDirs(&OPT_NumSpatioTemporalDirections, this),
00068 itsNumSpeeds(&OPT_NumSpatioTemporalSpeeds, this),
00069 itsFoeDetector(new FoeDetector(mgr))
00070 {
00071 GVX_TRACE(__PRETTY_FUNCTION__);
00072
00073
00074
00075 buildSubChans();
00076
00077 itsMT.reset(new MiddleTemporal());
00078 addSubComponent(itsFoeDetector);
00079
00080 itsCurrentFoeMapIndex = -1;
00081 itsWin.reset();
00082 }
00083
00084
00085 MotionSpatioTemporalChannel::~MotionSpatioTemporalChannel()
00086 {
00087 GVX_TRACE(__PRETTY_FUNCTION__);
00088 }
00089
00090
00091 DirectionSpatioTemporalChannel& MotionSpatioTemporalChannel::dirChan
00092 (const uint idx) const
00093 {
00094 GVX_TRACE(__PRETTY_FUNCTION__);
00095 return *(dynCast<DirectionSpatioTemporalChannel>(subChan(idx)));
00096 }
00097
00098
00099 void MotionSpatioTemporalChannel::buildSubChans()
00100 {
00101 GVX_TRACE(__PRETTY_FUNCTION__);
00102
00103 this->removeAllSubChans();
00104
00105
00106
00107
00108
00109 LINFO("Using %d directions spanning [0..360]deg", itsNumDirs.getVal());
00110
00111
00112 itsDirectionSpatioTemporalChannels.clear();
00113 itsDirectionSpatioTemporalChannels.resize(itsNumDirs.getVal());
00114
00115
00116 for (uint i = 0; i < itsNumDirs.getVal(); i++)
00117 {
00118 for (uint j = 0; j < itsNumSpeeds.getVal(); j++)
00119 {
00120 float speed = pow(2.0, j);
00121
00122 nub::ref<DirectionSpatioTemporalChannel> chan =
00123 makeSharedComp(new DirectionSpatioTemporalChannel
00124 (getManager(), i, j,
00125 360.0 * double(i) /
00126 double(itsNumDirs.getVal()),
00127 speed,
00128 itsPyrType.getVal()));
00129
00130 itsDirectionSpatioTemporalChannels[i].push_back(chan);
00131
00132 this->addSubChan(chan);
00133
00134 chan->exportOptions(MC_RECURSE);
00135 }
00136 }
00137
00138
00139 itsRawSpatioTemporalEnergy.clear();
00140 itsRawSpatioTemporalEnergy.resize(itsNumDirs.getVal());
00141 for(uint i = 0; i < itsNumDirs.getVal(); i++)
00142 itsRawSpatioTemporalEnergy[i].resize(itsNumSpeeds.getVal());
00143 }
00144
00145
00146 void MotionSpatioTemporalChannel::paramChanged(ModelParamBase* const param,
00147 const bool valueChanged,
00148 ParamClient::ChangeStatus* status)
00149 {
00150 GVX_TRACE(__PRETTY_FUNCTION__);
00151 ComplexChannel::paramChanged(param, valueChanged, status);
00152
00153
00154
00155 if (param == &itsNumDirs &&
00156 numChans() != itsNumDirs.getVal())
00157 buildSubChans();
00158 }
00159
00160
00161 void MotionSpatioTemporalChannel::doInput(const InputFrame& inframe)
00162 {
00163 GVX_TRACE(__PRETTY_FUNCTION__);
00164 ASSERT(inframe.grayFloat().initialized());
00165
00166 Timer tim1(1000000);
00167
00168 Image<byte> image(inframe.grayFloat());
00169 itsCurrentImage = image;
00170
00171
00172
00173 for (uint i = 0; i < numChans(); i++)
00174 {
00175 subChan(i)->input(inframe);
00176 LINFO("Motion pyramid (%d/%d) ok.", i+1, numChans());
00177 }
00178
00179
00180 uint index = 0;
00181 for (uint i = 0; i < itsNumDirs.getVal(); i++)
00182 {
00183 for (uint j = 0; j < itsNumSpeeds.getVal(); j++)
00184 {
00185 itsRawSpatioTemporalEnergy[i][j] =
00186 itsDirectionSpatioTemporalChannels[i][j]->getSpatioTemporalEnergy();
00187
00188
00189
00190
00191 index++;
00192 }
00193 }
00194
00195 LINFO(" time: %f \n", tim1.get()/1000.0);
00196
00197
00198 if(itsRawSpatioTemporalEnergy[0][0].size() != 0)
00199 {
00200 Timer tim2(1000000);
00201
00202
00203 itsMT->computeMTfeatures(itsRawSpatioTemporalEnergy);
00204 std::vector<Image<float> > mtFeatures = itsMT->getMTfeatures();
00205 for (uint i = 0; i < itsNumDirs.getVal(); i++)
00206 for (uint j = 0; j < itsNumSpeeds.getVal(); j++)
00207 itsDirectionSpatioTemporalChannels[i][j]->setMTfeatureMap(mtFeatures[i]);
00208
00209 LINFO("\n computeMTfeatures time: %f \n", tim2.get()/1000.0);
00210
00211
00212 computeConspicuityMap();
00213
00214 LINFO("\n computeConspicuityMap time: %f \n", tim2.get()/1000.0);
00215 }
00216 }
00217
00218
00219 void MotionSpatioTemporalChannel::computeConspicuityMap()
00220 {
00221 GVX_TRACE(__PRETTY_FUNCTION__);
00222
00223
00224 std::vector<Image<float> > mtFeatures = itsMT->getMTfeatures();
00225 uint mtWidth = mtFeatures[0].getWidth();
00226 uint mtHeight = mtFeatures[0].getHeight();
00227 if(mtWidth == 0 || mtHeight == 0) return;
00228
00229 uint cmWidth = subChan(0)->getMapDims().w();
00230 uint cmHeight = subChan(0)->getMapDims().h();
00231
00232
00233
00234
00235 Image<float> result = getMTObjectMotionMap();
00236
00237 Image<float> tsubmap = maxNormalize(result, MAXNORMMIN, MAXNORMMAX,
00238 itsNormType.getVal());
00239 result = tsubmap * numChans();
00240 Image<float> tres = result;
00241 result = rescale(tres, Dims(mtWidth, mtHeight));
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 LINFO("TOOK OUT THE FOE MAP\n\n\n");
00279
00280
00281
00282
00283
00284 float mn, mx;
00285 getMinMax(result,mn,mx);
00286 if (mtWidth > cmWidth)
00287 result = downSize(result, Dims(cmWidth, cmHeight));
00288 else if (mtWidth < cmWidth)
00289 result = rescale(result, Dims(cmWidth, cmHeight));
00290 inplaceNormalize(result,0.0F,mx);
00291
00292 itsConspicuityMap = result;
00293 }
00294
00295
00296 Image<float> MotionSpatioTemporalChannel::getV1ObjectMotionMap()
00297 {
00298 uint cmWidth = subChan(0)->getMapDims().w();
00299 uint cmHeight = subChan(0)->getMapDims().h();
00300 Image<float> result(cmWidth, cmHeight, ZEROS);
00301
00302 for (uint i = 0; i < itsNumDirs.getVal(); i++)
00303 {
00304 for (uint j = 0; j < itsNumSpeeds.getVal(); j++)
00305 {
00306 for (uint k = 0; k < itsRawSpatioTemporalEnergy[i][j].size(); k++)
00307 {
00308 Image<float> tmap = itsRawSpatioTemporalEnergy[i][j][k];
00309 Image<float> submap = downSizeClean(tmap, Dims(cmWidth, cmHeight));
00310
00311 Image<float> psubmap;
00312 if (itsUseOlderVersion.getVal())
00313 {
00314 LDEBUG("%s[%d]: applying %s(%f .. %f)",
00315 tagName().c_str(), i,
00316 maxNormTypeName(itsNormType.getVal()), MAXNORMMIN, MAXNORMMAX);
00317 psubmap = maxNormalize(submap, MAXNORMMIN, MAXNORMMAX,
00318 itsNormType.getVal());
00319 }
00320 else
00321 {
00322 LDEBUG("%s[%d]: applying %s(0.0 .. 0.0)", tagName().c_str(), i,
00323 maxNormTypeName(itsNormType.getVal()));
00324 psubmap = maxNormalize(submap, 0.0f, 0.0f, itsNormType.getVal());
00325 }
00326
00327 result += psubmap;
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 }
00368 }
00369 }
00370
00371 return result;
00372 }
00373
00374
00375 Image<float> MotionSpatioTemporalChannel::getMTObjectMotionMap()
00376 {
00377 std::vector<Image<float> > mtFeatures = itsMT->getMTfeatures();
00378 uint mtWidth = mtFeatures[0].getWidth();
00379 uint mtHeight = mtFeatures[0].getHeight();
00380 if(mtWidth == 0 || mtHeight == 0) return Image<float>();
00381
00382 uint cmWidth = subChan(0)->getMapDims().w();
00383 uint cmHeight = subChan(0)->getMapDims().h();
00384
00385 Image<float> result(mtWidth, mtHeight, ZEROS);
00386
00387 LINFO("MT: %d %d CM: %d %d", mtWidth,mtHeight, cmWidth, cmHeight);
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 for (uint i = 0; i < mtFeatures.size(); i++)
00402 {
00403 Image<float> tmap = mtFeatures[i];
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 Image<float> submap = tmap;
00423
00424 Image<float> psubmap;
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 psubmap = submap;
00440
00441 result += psubmap;
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 }
00473
00474
00475 result = downSizeClean(result, Dims(cmWidth, cmHeight));
00476 return result;
00477 }
00478
00479
00480 Image<float> MotionSpatioTemporalChannel::getOutput()
00481 {
00482 GVX_TRACE(__PRETTY_FUNCTION__);
00483 return itsConspicuityMap;
00484 }
00485
00486
00487 Image<float> MotionSpatioTemporalChannel::
00488 downSizeMax(Image<float> img, uint scale)
00489 {
00490 img = lowPassX(9,img);
00491 img = lowPassY(9,img);
00492
00493 uint width = img.getWidth();
00494 uint height = img.getHeight();
00495
00496 uint oWidth = width/scale;
00497 uint oHeight = height/scale;
00498
00499 Image<float> result(oWidth, oHeight, NO_INIT);
00500 for(uint i = 0; i < oWidth; i++)
00501 for(uint j = 0; j < oHeight; j++)
00502 {
00503 float max = 0.0;
00504 for(uint di = 0; di < scale; di++)
00505 for(uint dj = 0; dj < scale; dj++)
00506 {
00507 uint ci = i*scale + di;
00508 uint cj = j*scale + dj;
00509 float val = img.getVal(ci,cj);
00510 if(val > max) max = val;
00511 }
00512 result.setVal(i,j,max);
00513 }
00514
00515 return result;
00516 }
00517
00518
00519
00520
00521
00522
00523
00524 #endif // MOTION_SPATIOTEMPORALENERGY_CHANNEL_C_DEFINED