Visual Servoing Platform  version 3.6.1 under development (2025-03-14)
tutorial-blob-auto-tracker.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/blob/vpDot2.h>
4 #include <visp3/gui/vpDisplayFactory.h>
5 #include <visp3/io/vpImageIo.h>
6 
7 int main()
8 {
9 #ifdef ENABLE_VISP_NAMESPACE
10  using namespace VISP_NAMESPACE_NAME;
11 #endif
12 
13 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
14  std::shared_ptr<vpDisplay> display;
15 #else
16  vpDisplay *display = nullptr;
17 #endif
18 
19  try {
20  bool learn = false;
21  vpImage<unsigned char> I; // Create a gray level image container
22 
23  vpImageIo::read(I, "./target.pgm");
24 
25 #if defined(VISP_HAVE_DISPLAY)
26 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
27  display = vpDisplayFactory::createDisplay(I, 0, 0, "Camera view");
28 #else
29  display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Camera view");
30 #endif
31 #else
32  std::cout << "No image viewer is available..." << std::endl;
33 #endif
36 
38  vpDot2 blob;
41  if (learn) {
42  // Learn the characteristics of the blob to auto detect
43  blob.setGraphics(true);
44  blob.setGraphicsThickness(1);
45  blob.initTracking(I);
46  blob.track(I);
47  std::cout << "Blob characteristics: " << std::endl;
48  std::cout << " width : " << blob.getWidth() << std::endl;
49  std::cout << " height: " << blob.getHeight() << std::endl;
50 #if VISP_VERSION_INT > VP_VERSION_INT(2, 7, 0)
51  std::cout << " area: " << blob.getArea() << std::endl;
52 #endif
53  std::cout << " gray level min: " << blob.getGrayLevelMin() << std::endl;
54  std::cout << " gray level max: " << blob.getGrayLevelMax() << std::endl;
55  std::cout << " grayLevelPrecision: " << blob.getGrayLevelPrecision() << std::endl;
56  std::cout << " sizePrecision: " << blob.getSizePrecision() << std::endl;
57  std::cout << " ellipsoidShapePrecision: " << blob.getEllipsoidShapePrecision() << std::endl;
58  }
61  else {
62  // Set blob characteristics for the auto detection
63  blob.setWidth(50);
64  blob.setHeight(50);
65 #if VISP_VERSION_INT > VP_VERSION_INT(2, 7, 0)
66  blob.setArea(1700);
67 #endif
68  blob.setGrayLevelMin(0);
69  blob.setGrayLevelMax(30);
70  blob.setGrayLevelPrecision(0.8);
71  blob.setSizePrecision(0.65);
72  blob.setEllipsoidShapePrecision(0.65);
73  }
75 
77  std::list<vpDot2> blob_list;
78  blob.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), blob_list);
80 
82  if (learn) {
83  // The blob that is tracked by initTracking() is not in the list of auto
84  // detected blobs We add it:
85  blob_list.push_back(blob);
86  }
88  std::cout << "Number of auto detected blob: " << blob_list.size() << std::endl;
89  std::cout << "A click to exit..." << std::endl;
90 
91  while (1) {
93 
95  for (std::list<vpDot2>::iterator it = blob_list.begin(); it != blob_list.end(); ++it) {
96  (*it).setGraphics(true);
97  (*it).setGraphicsThickness(3);
98  (*it).track(I);
99  }
101 
102  vpDisplay::flush(I);
103 
104  if (vpDisplay::getClick(I, false))
105  break;
106 
107  vpTime::wait(40);
108  }
109  }
110  catch (const vpException &e) {
111  std::cout << "Catch an exception: " << e << std::endl;
112  }
113 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
114  if (display != nullptr) {
115  delete display;
116  }
117 #endif
118 }
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:219
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:225
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:452
void setGraphics(bool activate)
Definition: vpDot2.h:318
void setGraphicsThickness(unsigned int thickness)
Definition: vpDot2.h:326
double getEllipsoidShapePrecision() const
Definition: vpDot2.cpp:651
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)
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:357
double getArea() const
Definition: vpDot2.cpp:628
void setSizePrecision(const double &sizePrecision)
Definition: vpDot2.cpp:756
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:726
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:338
void setHeight(const double &height)
Definition: vpDot2.cpp:695
double getSizePrecision() const
Definition: vpDot2.cpp:642
double getGrayLevelPrecision() const
Definition: vpDot2.cpp:635
void setWidth(const double &width)
Definition: vpDot2.cpp:683
double getWidth() const
Definition: vpDot2.cpp:614
void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision)
Definition: vpDot2.cpp:801
void setArea(const double &area)
Definition: vpDot2.cpp:707
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:269
double getHeight() const
Definition: vpDot2.cpp:621
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)