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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "Component/ModelManager.H"
00050 #include "Media/FrameSeries.H"
00051 #include "GUI/XWindow.H"
00052 #include "Media/MediaSimEvents.H"
00053 #include "Neuro/StdBrain.H"
00054 #include "Neuro/NeuroSimEvents.H"
00055 #include "VFAT/featureClusterVision.H"
00056 #include "VFAT/segmentImageTrackMC.H"
00057 #include "Simulation/SimEventQueueConfigurator.H"
00058
00059
00060 #define H1_STD 3.5F
00061 #define H2_STD 3.5F
00062 #define S_STD 3.0F
00063 #define V_STD 2.5F
00064
00065
00066
00067 #define IMAGE_SIZE_X 640
00068 #define IMAGE_SIZE_Y 480
00069 #define TRACKER_NUMBER 3
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 int main(const int argc, const char **argv)
00080 {
00081 MYLOGVERB = LOG_INFO;
00082
00083
00084 ModelManager manager("Attention Model");
00085
00086
00087 nub::soft_ref<SimEventQueueConfigurator>
00088 seqc(new SimEventQueueConfigurator(manager));
00089 manager.addSubComponent(seqc);
00090
00091 nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00092 manager.addSubComponent(ifs);
00093
00094 nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00095 manager.addSubComponent(ofs);
00096
00097 nub::soft_ref<StdBrain> brain(new StdBrain(manager));
00098 manager.addSubComponent(brain);
00099
00100
00101 const std::string name = "featureCluster";
00102 const std::string tag = "fCV";
00103 Image< PixRGB<byte> > input;
00104 Image< PixRGB<float> > finput;
00105 std::vector<covHolder<double> > covHolder;
00106
00107
00108 if (manager.parseCommandLine(argc, argv, "<image>", 1, 1) == false)
00109 return(1);
00110
00111 nub::soft_ref<featureClusterVision<float> >
00112 fCV(new featureClusterVision<float>(manager,name,tag,brain,ifs,
00113 manager.getExtraArg(0)));
00114 manager.addSubComponent(fCV);
00115 nub::soft_ref<SimEventQueue> seq = seqc->getQ();
00116
00117
00118 manager.start();
00119 bool init = true;
00120
00121
00122
00123 Image<PixRGB<float> > fima1; Image<PixRGB<byte> > bima1;
00124 XWindow win2(Dims(IMAGE_SIZE_X, IMAGE_SIZE_Y), 0, 0, "CLASSES TEMPORAL");
00125 Image<PixRGB<float> > fima2; Image<PixRGB<byte> > bima2;
00126 XWindow win3(Dims(IMAGE_SIZE_X, IMAGE_SIZE_Y), 0, 0, "TARGETS TEMPORAL");
00127 Image<PixRGB<float> > fima3; Image<PixRGB<byte> > bima3;
00128 Image<float> fima4;
00129
00130
00131
00132
00133 XWindow wini(Dims(IMAGE_SIZE_X, IMAGE_SIZE_Y), 0, 0, "Test-input window");
00134 XWindow wino(Dims(IMAGE_SIZE_X/4, (IMAGE_SIZE_Y/4)*TRACKER_NUMBER), 0, 0, "Test-output window");
00135 XWindow winAux(Dims(500, 450), 0, 0, "Channel levels");
00136 int wi = IMAGE_SIZE_X/4;
00137 int hi = IMAGE_SIZE_Y/4;
00138
00139 Image<PixH2SV2<float> > H2SVimage;
00140
00141 Image< PixRGB<byte> > display;
00142
00143 std::vector<float> color(4,0.0F);
00144
00145 std::vector<float> std(4,0.0F);
00146
00147 std::vector<float> norm(4,0.0F);
00148 norm[0] = 1.0F; norm[1] = 1.0F; norm[2] = 1.0F; norm[3] = 1.0F;
00149
00150 std::vector<float> adapt(4,0.0F);
00151 adapt[0] = 3.5F; adapt[1] = 3.5F; adapt[2] = 3.5F; adapt[3] = 3.5F;
00152
00153 std::vector<float> upperBound(4,0.0F);
00154
00155 std::vector<float> lowerBound(4,0.0F);
00156
00157 segmentImageTrackMC<float,unsigned int,4> _segmenter(wi*hi);
00158
00159 std::vector<segmentImageTrackMC<float,unsigned int,4> >
00160 segmenters(TRACKER_NUMBER,_segmenter);
00161
00162 for(int i = 0; i < TRACKER_NUMBER; i++)
00163 {
00164 segmenters[i].SITsetFrame(&wi,&hi);
00165 segmenters[i].SITsetCircleColor(0,255,0);
00166 segmenters[i].SITsetUseSmoothing(true,10);
00167 }
00168
00169 segmenters[0].SITsetBoxColor(128,0,0 ,255,0,0);
00170 segmenters[1].SITsetBoxColor(0,128,0 ,0,255,0);
00171 segmenters[2].SITsetBoxColor(0,0,128 ,0,0,255);
00172 segmenters[3].SITsetBoxColor(128,128,0 ,255,255,0);
00173 segmenters[4].SITsetBoxColor(0,128,128 ,0,255,255);
00174 segmenters[5].SITsetBoxColor(128,0,128 ,255,0,255);
00175
00176 std::vector<bool> noTargetYet(TRACKER_NUMBER,true);
00177 std::vector<unsigned int> contTrackTime(TRACKER_NUMBER,0);
00178
00179
00180 std::ofstream outfileStart1("tracker.fzvision.log",std::ios::out);
00181 outfileStart1 << "\n*************************************************\n\n";
00182 outfileStart1.close();
00183 std::ofstream outfileStart2("blobs.fzvision.log",std::ios::out);
00184 outfileStart2 << "\n*************************************************\n\n";
00185 outfileStart2.close();
00186 while(1) {
00187
00188 bool shouldsave = false;
00189 bool shouldquit = false;
00190 bool gotcovert = false;
00191 if (seq->check<SimEventWTAwinner>(0)) gotcovert = true;
00192 const FrameState os = ofs->update(seq->now(), gotcovert);
00193
00194 if (os == FRAME_NEXT || os == FRAME_FINAL)
00195 shouldsave = true;
00196
00197 if (os == FRAME_FINAL)
00198 shouldquit = true;
00199
00200 if (shouldsave)
00201 {
00202 SimModuleSaveInfo sinfo(ofs, *seq);
00203 brain->save(sinfo);
00204 int foo = ifs->frame();
00205 std::string Myname;
00206 std::string a = manager.getExtraArg(0);
00207 std::string b = ".";
00208 char c[100];
00209 if(foo == 1)
00210 init = false;
00211 if(foo < 10)
00212 sprintf(c,"00000%d",foo);
00213 else if(foo < 100)
00214 sprintf(c,"0000%d",foo);
00215 else if(foo < 1000)
00216 sprintf(c,"000%d",foo);
00217 else if(foo < 10000)
00218 sprintf(c,"00%d",foo);
00219 else if(foo < 100000)
00220 sprintf(c,"0%d",foo);
00221 else
00222 sprintf(c,"%d",foo);
00223 Myname = a + b + c;
00224 std::cerr << "******************************************************\n";
00225 std::cerr << "******************************************************\n";
00226
00227
00228 LINFO("RUNNING FRAME %d NTARG %d",foo,0);
00229 LINFO("NAME %s",Myname.c_str());
00230
00231
00232 fCV->fCVuploadImage(input,Myname);
00233
00234 fCV->fCVclusterImage();
00235 std::cerr << "******************************************************\n";
00236 std::cerr << "******************************************************\n";
00237
00238 fCV->fCVgetClusterImages(&fima1,&fima2,&fima3,&fima4);
00239
00240 bima1 = fima1; bima2 = fima2; bima3 = fima3;
00241
00242 win2.drawImage(bima2);
00243 win3.drawImage(bima3);
00244
00245
00246 Raster::WriteRGB(bima2,sformat("classes.out.%s.ppm",c));
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 covHolder = fCV->fCVgetCovHolders();
00268
00269
00270
00271
00272
00273 std::ofstream outfile("tracker.fzvision.log",std::ios::app);
00274 outfile << ">>> FRAME " << foo;
00275 for(int i = 0; i < TRACKER_NUMBER; i++)
00276 {
00277 segmenters[i].SITsetFrameNumber(foo);
00278 }
00279
00280 for(int x = 0; x < (signed)fCV->fCVgetCovHolderSize(); x++)
00281 {
00282 covHolder[x].dumpMeToFile(a,c,init);
00283 for(int i = 0; i < TRACKER_NUMBER; i++)
00284 {
00285 if(noTargetYet[i] == true)
00286 {
00287 LINFO("%d,%f",x,covHolder[x].mean[3]);
00288 if(covHolder[x].mean[3] > 1.5F)
00289 {
00290 bool unique = true;
00291 for(int j = 0; j < TRACKER_NUMBER; j++)
00292 {
00293 if(noTargetYet[j] == false)
00294 {
00295 unsigned int umaxX,umaxY,uminX,uminY,uposX,uposY;
00296 int maxX,maxY,minX,minY;
00297 bool isSet = false;
00298 outfile << "\t\n- CHECKED " << i << " WITH " << j << " : ";
00299 if(contTrackTime[j] > 1)
00300 {
00301 segmenters[j].SITgetMinMaxBoundry(&uminX,&umaxX,&uminY,&umaxY);
00302 uposX = 0; uposY = 0;
00303 outfile << "REAL BOUNDARY ";
00304 uminX = uminX/4; uminY = uminY/4;
00305 umaxX = umaxX/4; umaxY = umaxY/4;
00306 }
00307 else
00308 {
00309 segmenters[j].SITgetExpectedTargetPosition(&uposX,&uposY,&umaxX,&umaxY,&uminX,&uminY,&isSet);
00310
00311
00312
00313
00314 outfile << "EXPECTED BOUNDARY (" << uposX << "," << uposY << ") : ";
00315 }
00316
00317 const int fudge = 15;
00318 minX = (signed)uminX; minY = (signed)uminY;
00319 maxX = (signed)umaxX; maxY = (signed)umaxY;
00320 bool Xok = false;
00321 bool Yok = false;
00322 if((((signed)covHolder[x].minX/4 < minX-fudge) && ((signed)covHolder[x].maxX/4 < minX-fudge)) ||
00323 (((signed)covHolder[x].minX/4 > maxX+fudge) && ((signed)covHolder[x].maxX/4 > maxX+fudge)))
00324 {
00325 Xok = true;
00326 }
00327 if((((signed)covHolder[x].minY/4 < minY-fudge) && ((signed)covHolder[x].maxY/4 < minY-fudge)) ||
00328 (((signed)covHolder[x].minY/4 > maxY+fudge) && ((signed)covHolder[x].maxY/4 > maxY+fudge)))
00329 {
00330 Yok = true;
00331 }
00332 if((Xok == false) && (Yok == false))
00333 {
00334 unique = false;
00335 }
00336
00337 outfile << covHolder[x].minX/4 << " < " << minX-fudge << " && "
00338 << covHolder[x].maxX/4 << " < " << minX-fudge << " || "
00339 << covHolder[x].minX/4 << " > " << maxX+fudge << " && "
00340 << covHolder[x].maxX/4 << " > " << maxX+fudge << " \n\tAND\n\t"
00341 << covHolder[x].minY/4 << " < " << minY-fudge << " && "
00342 << covHolder[x].maxY/4 << " < " << minY-fudge << " || "
00343 << covHolder[x].minY/4 << " > " << maxY+fudge << " && "
00344 << covHolder[x].maxY/4 << " > " << maxY+fudge << " : isSet = " << isSet
00345 << " : Unique = " << unique << "\n";
00346 }
00347 }
00348 if(unique == true)
00349 {
00350 PixRGB<float> pRGB;
00351 PixH2SV2<float> pH2SV2;
00352 pH2SV2.p[0] = covHolder[x].mean[6]/covHolder[x].bias[6];
00353 pH2SV2.p[1] = covHolder[x].mean[7]/covHolder[x].bias[7];
00354 pH2SV2.p[2] = covHolder[x].mean[4]/covHolder[x].bias[4];
00355 pH2SV2.p[3] = covHolder[x].mean[5]/covHolder[x].bias[5];
00356 pRGB = PixRGB<float>(pH2SV2);
00357 outfile << "\n\t- " << x
00358 << " Moving Target " << covHolder[x].mean[3] << "\t"
00359 << covHolder[x].featureName[3].c_str() << "\t"
00360 << covHolder[x].mean[3] << "\t"
00361 << covHolder[x].STD[3] << "\t"
00362 << covHolder[x].bias[3] << "\t"
00363 << covHolder[x].featureName[4].c_str() << "\t"
00364 << covHolder[x].mean[4] << "\t"
00365 << covHolder[x].STD[4] << "\t"
00366 << covHolder[x].bias[4] << "\t"
00367 << covHolder[x].featureName[5].c_str() << "\t"
00368 << covHolder[x].mean[5] << "\t"
00369 << covHolder[x].STD[5] << "\t"
00370 << covHolder[x].bias[5] << "\t"
00371 << covHolder[x].featureName[6].c_str() << "\t"
00372 << covHolder[x].mean[6] << "\t"
00373 << covHolder[x].STD[6] << "\t"
00374 << covHolder[x].bias[6] << "\t"
00375 << covHolder[x].featureName[7].c_str() << "\t"
00376 << covHolder[x].mean[7] << "\t"
00377 << covHolder[x].STD[7] << "\t"
00378 << covHolder[x].bias[6] << "\n\t- "
00379 << "RED " << pRGB.p[0] << "\t"
00380 << "GREEN " << pRGB.p[1] << "\t"
00381 << "BLUE " << pRGB.p[2] << "\n";
00382 LINFO("%s",covHolder[x].featureName[3].c_str());
00383 LINFO("%s",covHolder[x].featureName[6].c_str());
00384 LINFO("%s",covHolder[x].featureName[7].c_str());
00385 LINFO("%s",covHolder[x].featureName[4].c_str());
00386 LINFO("%s",covHolder[x].featureName[5].c_str());
00387 LINFO("..%lx",covHolder[x].samples);
00388 outfile << "\t- Samples " << covHolder[x].samples << " ... ";
00389 if(covHolder[x].samples > 25)
00390 {
00391 outfile << "OK ";
00392 outfile << " Variance " << covHolder[x].STD[6] * H1_STD
00393 << " " << covHolder[x].STD[7] * H2_STD
00394 << " ... ";
00395
00396
00397
00398
00399
00400
00401 outfile << "OK\n";
00402
00403
00404
00405 outfile << "<<<NEW TRACKING>>> " << x << "\t"
00406 << "FRAME " << foo << "\t"
00407 << "TRACKER " << i << "\t"
00408 << "LIFE SPAN " << covHolder[x].lifeSpan << "\t"
00409 << "(" << covHolder[x].posX/4
00410 << "," << covHolder[x].posY/4 << ")\t"
00411 << "Max (" << covHolder[x].maxX/4
00412 << "," << covHolder[x].maxY/4 << ")\t"
00413 << "Min (" << covHolder[x].minX/4
00414 << "," << covHolder[x].minY/4 << ")\n";
00415
00416 LINFO("....%lx",covHolder[x].lifeSpan);
00417
00418 noTargetYet[i] = false;
00419
00420 color[0] = covHolder[x].mean[6]/covHolder[x].bias[6];
00421 color[1] = covHolder[x].mean[7]/covHolder[x].bias[7];
00422 color[2] = covHolder[x].mean[4]/covHolder[x].bias[4];
00423 color[3] = covHolder[x].mean[5]/covHolder[x].bias[5];
00424
00425 std[0] = (covHolder[x].STD[6]*(1/covHolder[x].bias[6]))
00426 * H1_STD;
00427 std[1] = (covHolder[x].STD[7]*(1/covHolder[x].bias[7]))
00428 * H2_STD;
00429 std[2] = (covHolder[x].STD[4]*(1/covHolder[x].bias[4]))
00430 * S_STD;
00431 std[3] = (covHolder[x].STD[5]*(1/covHolder[x].bias[5]))
00432 * V_STD;
00433
00434 upperBound[0] = covHolder[x].mean[6]/covHolder[x].bias[6]
00435 + 0.45F;
00436 upperBound[1] = covHolder[x].mean[7]/covHolder[x].bias[7]
00437 + 0.45F;
00438 upperBound[2] = covHolder[x].mean[4]/covHolder[x].bias[4]
00439 + 0.45F;
00440 upperBound[3] = covHolder[x].mean[5]/covHolder[x].bias[5]
00441 + 0.45F;
00442
00443 lowerBound[0] = covHolder[x].mean[6]/covHolder[x].bias[6]
00444 - 0.45F;
00445 lowerBound[1] = covHolder[x].mean[7]/covHolder[x].bias[7]
00446 - 0.45F;
00447 lowerBound[2] = covHolder[x].mean[4]/covHolder[x].bias[4]
00448 - 0.45F;
00449 lowerBound[3] = covHolder[x].mean[5]/covHolder[x].bias[5]
00450 - 0.45F;
00451
00452
00453 segmenters[i].SITsetFrame(&wi,&hi);
00454
00455 segmenters[i].SITsetExpectedTargetPosition(covHolder[x].posX/4,
00456 covHolder[x].posY/4,
00457 covHolder[x].maxX/4,
00458 covHolder[x].maxY/4,
00459 covHolder[x].minX/4,
00460 covHolder[x].minY/4);
00461
00462
00463 segmenters[i].SITsetTrackColor(&color,&std,&norm,&adapt,
00464 &upperBound,&lowerBound,false,true);
00465
00466 }
00467 }
00468 }
00469 }
00470 }
00471 }
00472 outfile << "\n";
00473 outfile.close();
00474 display = input;
00475 Image<PixRGB<byte> > Aux;
00476 Aux.resize(100,450,true);
00477 Image<byte> tempPaste;
00478 tempPaste.resize(IMAGE_SIZE_X/4, (IMAGE_SIZE_Y/4)*TRACKER_NUMBER,true);
00479 for(int i = 0; i < TRACKER_NUMBER; i++)
00480 {
00481 Image<byte> temp;
00482 temp.resize(IMAGE_SIZE_X/4, IMAGE_SIZE_Y/4,true);
00483 if(noTargetYet[i] == false)
00484 {
00485 H2SVimage = input;
00486 segmenters[i].SITtrackImageAny(H2SVimage,&display,&Aux,true);
00487
00488 temp = segmenters[i].SITreturnCandidateImage();
00489
00490
00491 char foofoo[100];
00492 sprintf(foofoo,"blobs.%d.fzvision.log",i);
00493
00494 std::ofstream outfile2(foofoo,std::ios::app);
00495 outfile2 << "FRAME " << foo << "\t"
00496 << "TRACK TIME " << contTrackTime[i] << "\t"
00497 << "BLOBS " << segmenters[i].SITnumberBlobs() << "\t"
00498 << "LOT status " << segmenters[i].SITreturnLOTtypeName() << "\t"
00499 << "LOT count " << segmenters[i].SITgetLOTcount() << "\t"
00500 << "MASS " << segmenters[i].SITgetMass() << "\n";
00501 for(unsigned int ii = 0; ii < segmenters[i].SITnumberBlobs(); ii++)
00502 {
00503 outfile2 << "\tBLOB NUMBER " << ii << "\t"
00504 << segmenters[i].SITgetBlobReasonForKill(ii) << " ... \t"
00505 << segmenters[i].SITgetBlobMass(ii) << "\t"
00506 << segmenters[i].SITgetBlobPosX(ii) << "\t"
00507 << segmenters[i].SITgetBlobPosY(ii) << "\n";
00508 }
00509 outfile2 << "\n";
00510 outfile2.close();
00511
00512 if(segmenters[i].SIT_LOTandRESET == true)
00513 {
00514 noTargetYet[i] = true;
00515 contTrackTime[i] = 0;
00516 LINFO("LOSS OF TRACK RESET");
00517 std::ofstream outfile("tracker.fzvision.log",std::ios::app);
00518 outfile << ">>>LOSS OF TRACK " << i
00519 << " RESET AT FRAME " << foo << "\n";
00520 outfile.close();
00521 }
00522 else
00523 {
00524 contTrackTime[i]++;
00525 }
00526 }
00527 pasteImage(tempPaste,temp,(byte)128,Point2D<int>(0,(IMAGE_SIZE_Y/4)*i));
00528 }
00529 Raster::WriteGray(tempPaste,sformat("tracker.candidates.out.%s.pgm",c));
00530 wino.drawImage(tempPaste);
00531 Raster::WriteRGB(Aux,sformat("tracker.levels.out.%s.ppm",c));
00532 winAux.drawImage(Aux);
00533 Raster::WriteRGB(display,sformat("tracker.out.%s.ppm",c));
00534 wini.drawImage(display);
00535 }
00536
00537 if (shouldquit) break;
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 if (ifs->shouldWait() || ofs->shouldWait())
00549 Raster::waitForKey();
00550
00551
00552 const FrameState is = ifs->update(seq->now());
00553 if (is == FRAME_COMPLETE) break;
00554 if (is == FRAME_NEXT || is == FRAME_FINAL)
00555 {
00556 input = ifs->readRGB();
00557
00558
00559 if (input.initialized())
00560 {
00561 rutz::shared_ptr<SimEventInputFrame>
00562 e(new SimEventInputFrame(brain.get(), GenericFrame(input), 0));
00563 seq->post(e);
00564
00565
00566 if (MYLOGVERB >= LOG_DEBUG)
00567 SHOWMEMORY("MEMORY USAGE: frame %d t=%.1fms", ifs->frame(),
00568 seq->now().msecs());
00569 }
00570 }
00571
00572
00573 const SimStatus status = seq->evolve();
00574
00575 if (SIM_BREAK == status)
00576 break;
00577 }
00578
00579
00580
00581
00582
00583 fCV->fCVfeaturesToFile(manager.getExtraArg(0),false);
00584 fCV->fCVICAfeaturesToFile(manager.getExtraArg(0));
00585
00586
00587 manager.stop();
00588
00589
00590 return 0;
00591 }
00592
00593
00594
00595
00596
00597