1 //! \example tutorial-blob-auto-tracker.cpp
2 #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 
main()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
25     vpDisplay::display(I);
26     vpDisplay::flush(I);
27 
28     //! [Construction]
29     vpDot2 blob;
30     //! [Construction]
31     //! [Learn]
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     }
50     //! [Learn]
51     //! [Setting]
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     }
65     //! [Setting]
66 
67     //! [Search]
68     std::list<vpDot2> blob_list;
69     blob.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), blob_list);
70     //! [Search]
71 
72     //! [Add learned dot]
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     }
78     //! [Add learned dot]
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) {
83       vpDisplay::display(I);
84 
85       //! [Display]
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       }
91       //! [Display]
92 
93       vpDisplay::flush(I);
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 }
104