#include #include #include #include #include #include #include #include #include typedef std::vector vec; GUI gui("vbox"); static inline vec getCenters(const Img8u &image, Img8u *dstRet){ static RegionDetector rd; //static int &threshold= gui.getValue("threshold-val"); rd.setRestrictions(10,10000,1,255); static Img8u dst(image.getSize(),formatGray); int w = image.getWidth(); int h = image.getHeight(); const int dim = w*h; const icl8u *r = image.getData(0); const icl8u *g = image.getData(1); const icl8u *b = image.getData(2); icl8u *d = dst.getData(0); // icl8u t = threshold; for(int i=0;i 160) && (g[i] < 80) && (b[i] < 100) ); } const std::vector &bd = rd.detect(&dst); static vec v; v.clear(); for(unsigned int i=0;i devs = DCGrabber::getDeviceList(); if(!devs.size()){ ERROR_LOG("no device found"); exit(-1); } grabber = new DCGrabber(devs[0]); grabber->setDesiredSize(Size(640,480)); //grabber->setDesiredFormat(formatGray); gui << "draw[@handle=image@minsize=32x24]" << "image[@handle=cam-image@minsize=32x24]"; gui << ( GUI("hbox[@maxsize=100x3]") << "slider(0,255,200)[@label=threshold@handle=threshold-h@out=threshold-val]" << "slider(0,100,10)[@handle=Hsl@out=Vsl@label=sleeptime]" << "togglebutton(!off,on)[@out=Vlo@label=Show labels]" ); gui.show(); dh = &gui.getValue("image"); } virtual void run(){ while(1){ static Img8u dst; //static LabelHandle &l = gui.getValue("l1"); //static FPSEstimator fps(10); //l = fps.getFpsString(); const ImgBase *image = grabber->grab(); ICLWidget *w = *gui.getValue("cam-image"); w->setImage(image); w->update(); vec v = getCenters(*(image->asImg()),&dst); datamutex.lock(); DATA = v; datamutex.unlock(); drawhandlemutex.lock(); (*dh) = &dst; // static LabelHandle &lN = gui.getValue("l3"); //lN = (int)v.size(); drawhandlemutex.unlock(); static int &sleepTime = gui.getValue("Vsl"); msleep(sleepTime); } } private: DCGrabber *grabber; }; class WorkThreadB : public Thread{ public: virtual void run(){ PositionTracker pt; while(1){ // static LabelHandle &l = gui.getValue("l2"); //static FPSEstimator fps(10); //l = fps.getFpsString(); datamutex.lock(); vec v = DATA; datamutex.unlock(); drawhandlemutex.lock(); ICLDrawWidget *w = **dh; w->lock(); w->reset(); if(v.size()){ pt.pushData(v.data(),v.size()/2); w->color(255,0,0); for(unsigned int i=0;isym(v[i],v[i+1],ICLDrawWidget::symCross); static bool &labelsOnFlag = gui.getValue("Vlo"); if(labelsOnFlag){ static char buf[100]; w->text(toStr(pt.getID(i/2),buf),v[i],v[i+1],10); } } } w->unlock(); drawhandlemutex.unlock(); w->update(); static int &sleepTime = gui.getValue("Vsl"); msleep(sleepTime); } } }; int main(int n, char **ppc){ QApplication app(n,ppc); WorkThreadA a; WorkThreadB b; a.start(); b.start(); return app.exec(); }