181 double latch_t, dt0, dt1, dt2;
183 while (!isStopping())
186 ImageOf<PixelBgr> *pImgBgrIn=
inPort.read(
true);
187 if (isStopping() || (pImgBgrIn==NULL))
192 inPort.getEnvelope(stamp);
194 double t0=Time::now();
198 (pImgBgrIn->height()!=
imgMonoIn.height()))
212 nodesPrev.assign(nodesNum,Point2f(0.0f,0.0f));
213 nodesCurr.assign(nodesNum,Point2f(0.0f,0.0f));
225 cvtColor(toCvMat(*pImgBgrIn),toCvMat(
imgMonoPrev),CV_BGR2GRAY);
230 yInfo(
"Detected image of size %zdx%zd; using %dx%d=%d nodes; populated %zd nodes",
239 cvtColor(toCvMat(*pImgBgrIn),toCvMat(
imgMonoIn),CV_BGR2GRAY);
242 ImageOf<PixelBgr> imgBgrOut=*pImgBgrIn;
243 Mat imgBgrOutMat=toCvMat(imgBgrOut);
246 ImageOf<PixelMono> imgMonoOpt;
247 imgMonoOpt.resize(imgBgrOut);
249 Mat imgMonoOptMat=toCvMat(imgMonoOpt);
255 Bottle &nodesStepBottle=nodesBottle.addList();
256 nodesStepBottle.addString(
"nodesStep");
265 constexpr int maxLevel=5;
271 TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,30,0.3));
272 dt0=Time::now()-latch_t;
276 for (
size_t i=0; i<
nodesPrev.size(); i++)
278 bool persistentNode=
false;
284 circle(imgBgrOutMat,node,1,
NODE_ON,2);
285 circle(imgMonoOptMat,node,1,Scalar(255),2);
287 Bottle &nodeBottle=nodesBottle.addList();
298 circle(imgBgrOutMat,node,1,
NODE_OFF,1);
308 int cntAdjNodesOn=-1;
312 for (
int k=j-1; k<=(j+1); k++)
324 circle(imgBgrOutMat,node,1,
NODE_ON,2);
325 circle(imgMonoOptMat,node,1,Scalar(255),2);
327 Bottle &nodeBottle=nodesBottle.addList();
337 dt1=Time::now()-latch_t;
347 int blueLev=255-((100*i)%255);
348 int redLev=(100*i)%255;
352 Bottle &blobBottle=blobsBottle.addList();
353 blobBottle.addInt32(centroid.x);
354 blobBottle.addInt32(centroid.y);
355 blobBottle.addInt32(blob.
size);
357 circle(imgBgrOutMat,centroid,4,Scalar(blueLev,0,redLev),3);
359 dt2=Time::now()-latch_t;
362 if (
outPort.getOutputCount()>0)
369 if (
optPort.getOutputCount()>0)
377 if ((
nodesPort.getOutputCount()>0) && (nodesBottle.size()>1))
384 if ((
blobsPort.getOutputCount()>0) && (blobsBottle.size()>0))
391 if ((
cropPort.getOutputCount()>0) && (blobsBottle.size()>0))
393 Bottle &blob=*blobsBottle.get(0).asList();
394 int x=blob.get(0).asInt32();
395 int y=blob.get(1).asInt32();
399 Point tl=Point(std::max(
x-d2,0),std::max(
y-d2,0));
400 Point br=Point(std::min(
x+d2,(
int)pImgBgrIn->width()-1),std::min(
y+d2,(
int)pImgBgrIn->height()-1));
401 Point
cropSize=Point(br.x-tl.x,br.y-tl.y);
403 ImageOf<PixelBgr> &cropImg=
cropPort.prepare();
405 toCvMat(*pImgBgrIn)(Rect(tl.x,tl.y,
cropSize.x,
cropSize.y)).copyTo(toCvMat(cropImg));
414 double t1=Time::now();
418 yInfo(
"cycle timing [ms]: optflow(%g), colorgrid(%g), blobdetection(%g), overall(%g)",
419 1000.0*dt0,1000.0*dt1,1000.0*dt2,1000.0*(t1-t0));