Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
tutorial-klt-tracker-live-v4l2.cpp
1 
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 
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 
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
55 
56  // Display initialisation
57  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
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)
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);
85  }
86  }
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
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 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
void acquire(vpImage< unsigned char > &I)
void setMaxFeatures(int maxCount)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(vpImage< unsigned char > &I)
void setHarrisFreeParameter(double harris_k)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
void setDevice(const std::string &devname)
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1)
void setQuality(double qualityLevel)
double get_u() const
Definition: vpImagePoint.h:262
void setPyramidLevels(int pyrMaxLevel)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void setWindowSize(int winSize)
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
void setUseHarris(int useHarrisDetector)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
void setBlockSize(int blockSize)
double get_v() const
Definition: vpImagePoint.h:273
void track(const cv::Mat &I)