Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-blob-auto-tracker.cpp
1 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/blob/vpDot2.h>
6 #include <visp3/io/vpImageIo.h>
7 
8 int main()
9 {
10  try {
11  bool learn = false;
12  vpImage<unsigned char> I; // Create a gray level image container
13 
14  vpImageIo::read(I, "./target.pgm");
15 
16 #if defined(VISP_HAVE_X11)
17  vpDisplayX d(I, 0, 0, "Camera view");
18 #elif defined(VISP_HAVE_GDI)
19  vpDisplayGDI d(I, 0, 0, "Camera view");
20 #elif defined(VISP_HAVE_OPENCV)
21  vpDisplayOpenCV d(I, 0, 0, "Camera view");
22 #else
23  std::cout << "No image viewer is available..." << std::endl;
24 #endif
27 
29  vpDot2 blob;
32  if (learn) {
33  // Learn the characteristics of the blob to auto detect
34  blob.setGraphics(true);
35  blob.setGraphicsThickness(1);
36  blob.initTracking(I);
37  blob.track(I);
38  std::cout << "Blob characteristics: " << std::endl;
39  std::cout << " width : " << blob.getWidth() << std::endl;
40  std::cout << " height: " << blob.getHeight() << std::endl;
41 #if VISP_VERSION_INT > VP_VERSION_INT(2,7,0)
42  std::cout << " area: " << blob.getArea() << std::endl;
43 #endif
44  std::cout << " gray level min: " << blob.getGrayLevelMin() << std::endl;
45  std::cout << " gray level max: " << blob.getGrayLevelMax() << std::endl;
46  std::cout << " grayLevelPrecision: " << blob.getGrayLevelPrecision() << std::endl;
47  std::cout << " sizePrecision: " << blob.getSizePrecision() << std::endl;
48  std::cout << " ellipsoidShapePrecision: " << blob.getEllipsoidShapePrecision() << std::endl;
49  }
52  else {
53  // Set blob characteristics for the auto detection
54  blob.setWidth(50);
55  blob.setHeight(50);
56 #if VISP_VERSION_INT > VP_VERSION_INT(2,7,0)
57  blob.setArea(1700);
58 #endif
59  blob.setGrayLevelMin(0);
60  blob.setGrayLevelMax(30);
61  blob.setGrayLevelPrecision(0.8);
62  blob.setSizePrecision(0.65);
63  blob.setEllipsoidShapePrecision(0.65);
64  }
66 
68  std::list<vpDot2> blob_list;
69  blob.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), blob_list);
71 
73  if (learn) {
74  // The blob that is tracked by initTracking() is not in the list of auto detected blobs
75  // We add it:
76  blob_list.push_back(blob);
77  }
79  std::cout << "Number of auto detected blob: " << blob_list.size() << std::endl;
80  std::cout << "A click to exit..." << std::endl;
81 
82  while(1) {
84 
86  for(std::list<vpDot2>::iterator it=blob_list.begin(); it != blob_list.end(); ++it) {
87  (*it).setGraphics(true);
88  (*it).setGraphicsThickness(3);
89  (*it).track(I);
90  }
92 
94 
95  if (vpDisplay::getClick(I, false))
96  break;
97 
98  vpTime::wait(40);
99  }
100  }
101  catch(vpException &e) {
102  std::cout << "Catch an exception: " << e << std::endl;
103  }
104 }
double getWidth() const
Definition: vpDot2.cpp:643
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:351
void searchDotsInArea(const vpImage< unsigned char > &I, int area_u, int area_v, unsigned int area_w, unsigned int area_h, std::list< vpDot2 > &niceDots)
Definition: vpDot2.cpp:1040
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:226
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
double getGrayLevelPrecision() const
Definition: vpDot2.cpp:673
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
error that can be emited by ViSP classes.
Definition: vpException.h:73
double getHeight() const
Definition: vpDot2.cpp:653
double getArea() const
Definition: vpDot2.cpp:663
double getEllipsoidShapePrecision() const
Definition: vpDot2.cpp:695
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
double getSizePrecision() const
Definition: vpDot2.cpp:683
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:322
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:783
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:210
void setArea(const double &area)
Definition: vpDot2.cpp:762
void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision)
Definition: vpDot2.cpp:858
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:335
void setWidth(const double &width)
Definition: vpDot2.cpp:733
void setSizePrecision(const double &sizePrecision)
Definition: vpDot2.cpp:813
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:262
void setHeight(const double &height)
Definition: vpDot2.cpp:748
unsigned int getHeight() const
Definition: vpImage.h:175
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:218
void setGraphics(const bool activate)
Definition: vpDot2.h:316