Visual Servoing Platform  version 3.6.1 under development (2024-04-17)
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  try {
19  bool opt_init_by_click = false;
20  int opt_device = 0;
21 
22  for (int i = 0; i < argc; i++) {
23  if (std::string(argv[i]) == "--init-by-click")
24  opt_init_by_click = true;
25  else if (std::string(argv[i]) == "--device")
26  opt_device = atoi(argv[i + 1]);
27  else if (std::string(argv[i]) == "--help") {
28  std::cout << "Usage: " << argv[0] << " [--init-by-click] [--device <camera device>] [--help]" << std::endl;
29  return EXIT_SUCCESS;
30  }
31  }
32 
34 
35 #if defined(VISP_HAVE_V4L2)
36  vpV4l2Grabber g;
37  std::ostringstream device;
38  device << "/dev/video" << opt_device;
39  g.setDevice(device.str());
40  g.open(I);
41  g.acquire(I);
42 #elif defined(HAVE_OPENCV_VIDEOIO)
43  cv::VideoCapture g(opt_device);
44  if (!g.isOpened()) { // check if we succeeded
45  std::cout << "Failed to open the camera" << std::endl;
46  return EXIT_FAILURE;
47  }
48  cv::Mat frame;
49  g >> frame; // get a new frame from camera
50  vpImageConvert::convert(frame, I);
51 #endif
52 
53  cv::Mat cvI;
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) {
75  std::vector<cv::Point2f> guess;
76  vpImagePoint ip;
77  do {
78  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
79  if (vpDisplay::getClick(I, ip, button, false)) {
80  if (button == vpMouseButton::button1) {
81  guess.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
82  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
84  }
85  }
87  vpTime::wait(20);
88  } while (button != vpMouseButton::button3);
89  tracker.initTracking(cvI, guess);
90  }
91  else {
92  tracker.initTracking(cvI);
93  }
94 
95  while (1) {
96 #if defined(VISP_HAVE_V4L2)
97  g.acquire(I);
98 #elif defined(HAVE_OPENCV_VIDEOIO)
99  g >> frame;
100  vpImageConvert::convert(frame, I);
101 #endif
103 
104  vpImageConvert::convert(I, cvI);
105  tracker.track(cvI);
106 
107  tracker.display(I, vpColor::red);
108  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
109  vpDisplay::flush(I);
110  if (vpDisplay::getClick(I, false))
111  break;
112  }
113  }
114  catch (const vpException &e) {
115  std::cout << "Catch an exception: " << e << std::endl;
116  return EXIT_FAILURE;
117  }
118 #else
119  (void)argc;
120  (void)argv;
121 #endif
122  return EXIT_SUCCESS;
123 }
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
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:59
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:73
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:266
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:355
void track(const cv::Mat &I)
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:274
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:314
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
Definition: vpKltOpencv.cpp:93
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:323
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1)
void setUseHarris(int useHarrisDetector)
Definition: vpKltOpencv.h:367
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:376
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:342
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)