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