Visual Servoing Platform  version 3.6.1 under development (2024-11-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 #if defined(HAVE_OPENCV_VIDEOIO)
12 #include <opencv2/videoio.hpp>
13 #endif
14 
15 int main(int argc, const char *argv[])
16 {
17 #if (defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO) || defined(VISP_HAVE_V4L2)) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
18 #ifdef ENABLE_VISP_NAMESPACE
19  using namespace VISP_NAMESPACE_NAME;
20 #endif
21 
22  try {
23  bool opt_init_by_click = false;
24  int opt_device = 0;
25 
26  for (int i = 0; i < argc; i++) {
27  if (std::string(argv[i]) == "--init-by-click")
28  opt_init_by_click = true;
29  else if (std::string(argv[i]) == "--device")
30  opt_device = atoi(argv[i + 1]);
31  else if (std::string(argv[i]) == "--help") {
32  std::cout << "Usage: " << argv[0] << " [--init-by-click] [--device <camera device>] [--help]" << std::endl;
33  return EXIT_SUCCESS;
34  }
35  }
36 
38 
39 #if defined(VISP_HAVE_V4L2)
40  vpV4l2Grabber g;
41  std::ostringstream device;
42  device << "/dev/video" << opt_device;
43  g.setDevice(device.str());
44  g.open(I);
45  g.acquire(I);
46 #elif defined(HAVE_OPENCV_VIDEOIO)
47  cv::VideoCapture g(opt_device);
48  if (!g.isOpened()) { // check if we succeeded
49  std::cout << "Failed to open the camera" << std::endl;
50  return EXIT_FAILURE;
51  }
52  cv::Mat frame;
53  g >> frame; // get a new frame from camera
54  vpImageConvert::convert(frame, I);
55 #endif
56 
57  cv::Mat cvI;
59 
60  // Display initialisation
61  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
64 
65  vpKltOpencv tracker;
66  // Set tracker parameters
67  tracker.setMaxFeatures(200);
68  tracker.setWindowSize(10);
69  tracker.setQuality(0.01);
70  tracker.setMinDistance(15);
71  tracker.setHarrisFreeParameter(0.04);
72  tracker.setBlockSize(9);
73  tracker.setUseHarris(1);
74  tracker.setPyramidLevels(3);
75 
76  // Initialise the tracking
77  if (opt_init_by_click) {
79  std::vector<cv::Point2f> guess;
80  vpImagePoint ip;
81  do {
82  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
83  if (vpDisplay::getClick(I, ip, button, false)) {
84  if (button == vpMouseButton::button1) {
85  guess.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
86  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
88  }
89  }
91  vpTime::wait(20);
92  } while (button != vpMouseButton::button3);
93  tracker.initTracking(cvI, guess);
94  }
95  else {
96  tracker.initTracking(cvI);
97  }
98 
99  while (1) {
100 #if defined(VISP_HAVE_V4L2)
101  g.acquire(I);
102 #elif defined(HAVE_OPENCV_VIDEOIO)
103  g >> frame;
104  vpImageConvert::convert(frame, I);
105 #endif
107 
108  vpImageConvert::convert(I, cvI);
109  tracker.track(cvI);
110 
111  tracker.display(I, vpColor::red);
112  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
113  vpDisplay::flush(I);
114  if (vpDisplay::getClick(I, false))
115  break;
116  }
117  }
118  catch (const vpException &e) {
119  std::cout << "Catch an exception: " << e << std::endl;
120  return EXIT_FAILURE;
121  }
122 #else
123  (void)argc;
124  (void)argv;
125 #endif
126  return EXIT_SUCCESS;
127 }
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:74
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1) const
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:267
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:356
void track(const cv::Mat &I)
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
Definition: vpKltOpencv.cpp:94
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:324
void setUseHarris(int useHarrisDetector)
Definition: vpKltOpencv.h:368
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:377
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:343
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
VISP_EXPORT int wait(double t0, double t)