ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
tutorial-blob-auto-tracker.cpp
1 
2 #include <visp/vpDisplayGDI.h>
3 #include <visp/vpDisplayX.h>
4 #include <visp/vpDot2.h>
5 #include <visp/vpImageIo.h>
6 
7 int main()
8 {
9  bool learn = false;
10  vpImage<unsigned char> I; // Create a gray level image container
11 
12  vpImageIo::read(I, "./target.pgm");
13 
14 #if defined(VISP_HAVE_X11)
15  vpDisplayX d(I, 0, 0, "Camera view");
16 #elif defined(VISP_HAVE_GDI)
17  vpDisplayGDI d(I, 0, 0, "Camera view");
18 #else
19  std::cout << "No image viewer is available..." << std::endl;
20 #endif
23 
24  vpDot2 blob;
25  if (learn) {
26  // Learn the characteristics of the blob to auto detect
27  blob.setGraphics(true);
28  blob.setGraphicsThickness(1);
29  blob.initTracking(I);
30  blob.track(I);
31  std::cout << "Blob characteristics: " << std::endl;
32  std::cout << " width : " << blob.getWidth() << std::endl;
33  std::cout << " height: " << blob.getHeight() << std::endl;
34 #if VISP_VERSION_INT > VP_VERSION_INT(2,7,0)
35  std::cout << " area: " << blob.getArea() << std::endl;
36 #endif
37  std::cout << " gray level min: " << blob.getGrayLevelMin() << std::endl;
38  std::cout << " gray level max: " << blob.getGrayLevelMax() << std::endl;
39  std::cout << " grayLevelPrecision: " << blob.getGrayLevelPrecision() << std::endl;
40  std::cout << " sizePrecision: " << blob.getSizePrecision() << std::endl;
41  std::cout << " ellipsoidShapePrecision: " << blob.getEllipsoidShapePrecision() << std::endl;
42  }
43  else {
44  // Set blob characteristics for the auto detection
45  blob.setWidth(50);
46  blob.setHeight(50);
47 #if VISP_VERSION_INT > VP_VERSION_INT(2,7,0)
48  blob.setArea(1700);
49 #endif
50  blob.setGrayLevelMin(0);
51  blob.setGrayLevelMax(30);
52  blob.setGrayLevelPrecision(0.8);
53  blob.setSizePrecision(0.65);
54  blob.setEllipsoidShapePrecision(0.65);
55  }
56 
57  std::list<vpDot2> blob_list;
58  blob.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), blob_list);
59 
60  if (learn) {
61  // The blob that is tracked by initTracking() is not in the list of auto detected blobs
62  // We add it:
63  blob_list.push_back(blob);
64  }
65  std::cout << "Number of auto detected blob: " << blob_list.size() << std::endl;
66  std::cout << "A click to exit..." << std::endl;
67 
68  while(1) {
70 
71  for(std::list<vpDot2>::iterator it=blob_list.begin(); it != blob_list.end(); ++it) {
72  (*it).setGraphics(true);
73  (*it).setGraphicsThickness(3);
74  (*it).track(I);
75  }
76 
78 
79  if (vpDisplay::getClick(I, false))
80  break;
81 
82  vpTime::wait(40);
83  }
84 }