1 /*! \example tutorial-klt-tracker-live-v4l2.cpp */
2 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_SENSOR
4 #include <visp3/sensor/vpV4l2Grabber.h>
5 #endif
6 #include <visp3/core/vpImageConvert.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/io/vpVideoReader.h>
9 #include <visp3/klt/vpKltOpencv.h>
10 
main(int argc,const char * argv[])11 int main(int argc, const char *argv[])
12 {
13 #if defined(VISP_HAVE_OPENCV) && (defined(VISP_HAVE_V4L2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100))
14   try {
15     bool opt_init_by_click = false;
16     int opt_device = 0;
17 
18     for (int i = 0; i < argc; i++) {
19       if (std::string(argv[i]) == "--init-by-click")
20         opt_init_by_click = true;
21       else if (std::string(argv[i]) == "--device")
22         opt_device = atoi(argv[i + 1]);
23       else if (std::string(argv[i]) == "--help") {
24         std::cout << "Usage: " << argv[0] << " [--init-by-click] [--device <camera device>] [--help]" << std::endl;
25         return 0;
26       }
27     }
28 
29     vpImage<unsigned char> I;
30 
31 #if defined(VISP_HAVE_V4L2)
32     vpV4l2Grabber g;
33     std::ostringstream device;
34     device << "/dev/video" << opt_device;
35     g.setDevice(device.str());
36     g.open(I);
37     g.acquire(I);
38 #elif defined(VISP_HAVE_OPENCV)
39     cv::VideoCapture g(opt_device);
40     if (!g.isOpened()) { // check if we succeeded
41       std::cout << "Failed to open the camera" << std::endl;
42       return -1;
43     }
44     cv::Mat frame;
45     g >> frame; // get a new frame from camera
46     vpImageConvert::convert(frame, I);
47 #endif
48 
49 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
50     IplImage *cvI = NULL;
51 #else
52     cv::Mat cvI;
53 #endif
54     vpImageConvert::convert(I, cvI);
55 
56     // Display initialisation
57     vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
58     vpDisplay::display(I);
59     vpDisplay::flush(I);
60 
61     vpKltOpencv tracker;
62     // Set tracker parameters
63     tracker.setMaxFeatures(200);
64     tracker.setWindowSize(10);
65     tracker.setQuality(0.01);
66     tracker.setMinDistance(15);
67     tracker.setHarrisFreeParameter(0.04);
68     tracker.setBlockSize(9);
69     tracker.setUseHarris(1);
70     tracker.setPyramidLevels(3);
71 
72     // Initialise the tracking
73     if (opt_init_by_click) {
74 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
75       vpMouseButton::vpMouseButtonType button;
76       std::vector<cv::Point2f> guess;
77       vpImagePoint ip;
78       do {
79         vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
80         if (vpDisplay::getClick(I, ip, button, false)) {
81           if (button == vpMouseButton::button1) {
82             guess.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
83             vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
84             vpDisplay::displayCross(I, ip, 12, vpColor::green);
85           }
86         }
87         vpDisplay::flush(I);
88         vpTime::wait(20);
89       } while (button != vpMouseButton::button3);
90       tracker.initTracking(cvI, guess);
91 #endif
92     } else {
93       tracker.initTracking(cvI);
94     }
95 
96     while (1) {
97 #if defined(VISP_HAVE_V4L2)
98       g.acquire(I);
99 #elif defined(VISP_HAVE_OPENCV)
100       g >> frame;
101       vpImageConvert::convert(frame, I);
102 #endif
103       vpDisplay::display(I);
104 
105       vpImageConvert::convert(I, cvI);
106       tracker.track(cvI);
107 
108       tracker.display(I, vpColor::red);
109       vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
110       vpDisplay::flush(I);
111       if (vpDisplay::getClick(I, false))
112         break;
113     }
114 
115 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
116     cvReleaseImage(&cvI);
117 #endif
118 
119     return 0;
120   } catch (const vpException &e) {
121     std::cout << "Catch an exception: " << e << std::endl;
122   }
123 #else
124   (void)argc;
125   (void)argv;
126 #endif
127 }
128