Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-blob-auto-tracker.cpp
1 #include <visp3/blob/vpDot2.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.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
75  // detected blobs 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  } catch (const vpException &e) {
101  std::cout << "Catch an exception: " << e << std::endl;
102  }
103 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:331
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:979
double getHeight() const
Definition: vpDot2.cpp:630
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
double getSizePrecision() const
Definition: vpDot2.cpp:651
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setGraphics(bool activate)
Definition: vpDot2.h:294
double getArea() const
Definition: vpDot2.cpp:637
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
static void flush(const vpImage< unsigned char > &I)
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:301
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:735
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:201
void setArea(const double &area)
Definition: vpDot2.cpp:716
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision)
Definition: vpDot2.cpp:806
double getGrayLevelPrecision() const
Definition: vpDot2.cpp:644
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:314
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:207
void setWidth(const double &width)
Definition: vpDot2.cpp:692
void setSizePrecision(const double &sizePrecision)
Definition: vpDot2.cpp:763
unsigned int getHeight() const
Definition: vpImage.h:186
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
double getWidth() const
Definition: vpDot2.cpp:623
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
double getEllipsoidShapePrecision() const
Definition: vpDot2.cpp:660
void setHeight(const double &height)
Definition: vpDot2.cpp:704
unsigned int getWidth() const
Definition: vpImage.h:244