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 Regions_C_DEFINED
00039 #define Regions_C_DEFINED
00040
00041 #include "Image/DrawOps.H"
00042 #include "Image/MathOps.H"
00043
00044 #include "Image/Kernels.H"
00045 #include "Image/FilterOps.H"
00046 #include "Image/Convolutions.H"
00047 #include "Image/fancynorm.H"
00048 #include "Image/Point3D.H"
00049 #include "Simulation/SimEventQueue.H"
00050 #include "plugins/SceneUnderstanding/Regions.H"
00051 #include "Neuro/EnvVisualCortex.H"
00052 #include "GUI/DebugWin.H"
00053 #include "Util/MathFunctions.H"
00054 #include <math.h>
00055 #include <fcntl.h>
00056 #include <limits>
00057 #include <string>
00058
00059 const ModelOptionCateg MOC_Regions = {
00060 MOC_SORTPRI_3, "Regions-Related Options" };
00061
00062
00063 const ModelOptionDef OPT_RegionsShowDebug =
00064 { MODOPT_ARG(bool), "RegionsShowDebug", &MOC_Regions, OPTEXP_CORE,
00065 "Show debug img",
00066 "regions-debug", '\0', "<true|false>", "false" };
00067
00068
00069
00070 Regions::Regions(OptionManager& mgr, const std::string& descrName,
00071 const std::string& tagName) :
00072 SimModule(mgr, descrName, tagName),
00073 SIMCALLBACK_INIT(SimEventLGNOutput),
00074 SIMCALLBACK_INIT(SimEventSMapOutput),
00075 SIMCALLBACK_INIT(SimEventSaveOutput),
00076 SIMCALLBACK_INIT(SimEventUserInput),
00077 SIMCALLBACK_INIT(SimEventRegionsPrior),
00078 itsShowDebug(&OPT_RegionsShowDebug, this)
00079
00080 {
00081
00082 itsTempColor = 104;
00083
00084
00085 itsRegionsState.clear();
00086 }
00087
00088
00089 Regions::~Regions()
00090 {
00091
00092 }
00093
00094
00095 void Regions::onSimEventLGNOutput(SimEventQueue& q,
00096 rutz::shared_ptr<SimEventLGNOutput>& e)
00097 {
00098 itsRegionsCellsInput = e->getCells();
00099 inplaceNormalize(itsRegionsCellsInput[0], 0.0F, 255.0F);
00100 evolve();
00101
00102 q.post(rutz::make_shared(new SimEventRegionsOutput(this, itsRegionsState)));
00103
00104 }
00105
00106
00107 void Regions::onSimEventRegionsPrior(SimEventQueue& q,
00108 rutz::shared_ptr<SimEventRegionsPrior>& e)
00109 {
00110 itsRegionsPrior = e->getRegions();
00111 evolve();
00112 }
00113
00114
00115
00116 void Regions::onSimEventSMapOutput(SimEventQueue& q,
00117 rutz::shared_ptr<SimEventSMapOutput>& e)
00118 {
00119 itsSMapInput = e->getCells();
00120
00121
00122
00123 Image<float> tmp(itsRegionsCellsInput[0].getDims(), ZEROS);
00124 if (itsSMapInput.size() > 0)
00125 {
00126 SMap::SMapState sms = itsSMapInput[0];
00127
00128 itsBackgroundRegion.mu = sms.mu;
00129 itsBackgroundRegion.sigma = sms.sigma;
00130
00131 for(uint pix=0; pix<sms.region.size(); pix++)
00132 {
00133
00134
00135
00136 {
00137 itsBackgroundRegion.region.push_back(sms.region[pix]);
00138 tmp.setVal(sms.region[pix], 255);
00139 }
00140 }
00141
00142 calcRegionLikelihood(itsBackgroundRegion);
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 }
00206
00207
00208 void Regions::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00209 {
00210 if (itsShowDebug.getVal())
00211 {
00212
00213
00214 nub::ref<FrameOstream> ofs =
00215 dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00216 Layout<PixRGB<byte> > disp = getDebugImage();
00217 ofs->writeRgbLayout(disp, "Regions", FrameInfo("Regions", SRC_POS));
00218 }
00219 }
00220
00221
00222
00223 void Regions::onSimEventUserInput(SimEventQueue& q, rutz::shared_ptr<SimEventUserInput>& e)
00224 {
00225
00226
00227 {
00228 LINFO("Got event %s %ix%i key=%i",
00229 e->getWinName(),
00230 e->getMouseClick().i,
00231 e->getMouseClick().j,
00232 e->getKey());
00233
00234 switch(e->getKey())
00235 {
00236 case 54:
00237 itsUserPolygon.clear();
00238 break;
00239 case 36:
00240 break;
00241 case 98:
00242 itsTempColor += 1;
00243 break;
00244 case 104:
00245 itsTempColor -= 1;
00246 break;
00247 }
00248 if (e->getMouseClick().isValid())
00249 {
00250 itsUserPolygon.push_back(e->getMouseClick());
00251 LINFO("Color is %f", itsRegionsCellsInput[0].getVal(e->getMouseClick()));
00252 }
00253 evolve();
00254
00255 }
00256
00257 }
00258
00259
00260
00261 void Regions::calcRegionLikelihood(RegionState& rs)
00262 {
00263
00264 double minProb = 1.0e-5;
00265
00266 rs.prob = log(minProb)*512*512;
00267
00268 if (itsRegionsCellsInput.size() > 0)
00269 {
00270 for(uint i=0; i<rs.region.size(); i++)
00271 rs.prob += log(gauss<double>((double)itsRegionsCellsInput[0].getVal(rs.region[i]), rs.mu, rs.sigma));
00272
00273
00274 rs.prob -= log(minProb) * rs.region.size();
00275 }
00276
00277 }
00278
00279
00280 double Regions::calcRegionLikelihood(Image<float>& mu)
00281 {
00282
00283 double prob = 0;
00284 Image<float> in = itsRegionsCellsInput[0];
00285
00286 for(uint i=0; i<mu.size(); i++)
00287 {
00288 prob += log(gauss<double>((double)in[i], (double)mu[i], 5));
00289 }
00290
00291 return prob;
00292 }
00293
00294
00295
00296 void Regions::evolve()
00297 {
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 itsRegionsState = itsRegionsPrior;
00323
00324
00325
00326
00327
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 Layout<PixRGB<byte> > Regions::getDebugImage()
00353 {
00354 Layout<PixRGB<byte> > outDisp;
00355
00356 for(int i=0; i<1; i++)
00357 {
00358 Image<float> in = itsRegionsCellsInput[i];
00359
00360
00361
00362 Image<PixRGB<byte> > perc = in;
00363
00364
00365
00366
00367 for(uint pix=0; pix<itsBackgroundRegion.region.size(); pix++)
00368 {
00369 perc.setVal(itsBackgroundRegion.region[pix], PixRGB<byte>(0,0,0));
00370 }
00371
00372 for(uint i=0; i<itsRegionsState.size(); i++)
00373 {
00374 RegionState rs = itsRegionsState[i];
00375
00376
00377 for(uint pix=0; pix<rs.region.size(); pix++)
00378 {
00379 perc.setVal(rs.region[pix], PixRGB<byte>(255,0,0));
00380 }
00381 }
00382
00383
00384
00385
00386 for(uint i=0; i<itsUserPolygon.size(); i++)
00387 {
00388 drawCircle(in, itsUserPolygon[i], 3, 255.0F);
00389 }
00390
00391
00392 outDisp = hcat(toRGB(Image<byte>(in)), perc);
00393 }
00394
00395 return outDisp;
00396
00397 }
00398
00399
00400
00401
00402
00403
00404
00405 #endif
00406