Visual Servoing Platform  version 3.0.0
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/klt/vpKltOpencv.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/io/vpVideoReader.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  }
93  else {
94  tracker.initTracking(cvI);
95  }
96 
97  while ( 1 ) {
98 #if defined(VISP_HAVE_V4L2)
99  g.acquire(I);
100 #elif defined(VISP_HAVE_OPENCV)
101  g >> frame;
102  vpImageConvert::convert(frame, I);
103 #endif
105 
106  vpImageConvert::convert(I, cvI);
107  tracker.track(cvI);
108 
109  tracker.display(I, vpColor::red);
110  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
111  vpDisplay::flush(I);
112  if (vpDisplay::getClick(I, false))
113  break;
114  }
115 
116 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
117  cvReleaseImage(&cvI);
118 #endif
119 
120  return 0;
121  }
122  catch(vpException e) {
123  std::cout << "Catch an exception: " << e << std::endl;
124  }
125 #else
126  (void)argc;
127  (void)argv;
128 #endif
129 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void acquire(vpImage< unsigned char > &I)
double get_v() const
Definition: vpImagePoint.h:259
void open(vpImage< unsigned char > &I)
void setHarrisFreeParameter(double harris_k)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void setMaxFeatures(const int maxCount)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
double get_u() const
Definition: vpImagePoint.h:248
void setDevice(const std::string &devname)
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:73
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1)
void setQuality(double qualityLevel)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
void setPyramidLevels(const int pyrMaxLevel)
Class for the Video4Linux2 video device.
void setWindowSize(const int winSize)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
Definition: vpKltOpencv.h:75
void setBlockSize(const int blockSize)
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setUseHarris(const int useHarrisDetector)
void track(const cv::Mat &I)