Visual Servoing Platform  version 3.1.0
tutorial-klt-tracker.cpp
1 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/io/vpVideoReader.h>
6 #include <visp3/klt/vpKltOpencv.h>
8 
9 int main(int argc, const char *argv[])
10 {
12 #ifdef VISP_HAVE_OPENCV
13  try {
15  bool opt_init_by_click = false;
16  for (int i = 0; i < argc; i++) {
17  if (std::string(argv[i]) == "--init-by-click")
18  opt_init_by_click = true;
19  else if (std::string(argv[i]) == "--help") {
20  std::cout << "Usage: " << argv[0] << " [--init-by-click] [--help]" << std::endl;
21  return 0;
22  }
23  }
24 
26  vpVideoReader reader;
27  reader.setFileName("video-postcard.mpeg");
29 
32  reader.acquire(I);
34 
36 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
37  IplImage *cvI = NULL;
38 #else
39  cv::Mat cvI;
40 #endif
43 
45  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
49 
51  vpKltOpencv tracker;
52  tracker.setMaxFeatures(200);
53  tracker.setWindowSize(10);
55  tracker.setQuality(0.01);
57  tracker.setMinDistance(15);
58  tracker.setHarrisFreeParameter(0.04);
59  tracker.setBlockSize(9);
60  tracker.setUseHarris(1);
61  tracker.setPyramidLevels(3);
63 
64  // Initialise the tracking
65  if (opt_init_by_click) {
67 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
68  std::vector<CvPoint2D32f> feature;
69 #else
70  std::vector<cv::Point2f> feature;
71 #endif
72  vpImagePoint ip;
73  do {
74  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
75  if (vpDisplay::getClick(I, ip, button, false)) {
76  if (button == vpMouseButton::button1) {
77  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
79  }
80  }
82  vpTime::wait(20);
83  } while (button != vpMouseButton::button3);
84 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
85  tracker.initTracking(cvI, &feature[0], feature.size());
86 #else
87  tracker.initTracking(cvI, feature);
88 #endif
89  } else {
91  tracker.initTracking(cvI);
93  }
94 
96  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
98 
100  while (!reader.end()) {
101  reader.acquire(I);
103 
104  vpImageConvert::convert(I, cvI);
105 
106  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
108 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
109  std::vector<CvPoint2D32f> feature;
110 #else
111  std::vector<cv::Point2f> feature;
112 #endif
113  vpImagePoint ip;
114  do {
115  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
116  if (vpDisplay::getClick(I, ip, button, false)) {
117  if (button == vpMouseButton::button1) {
118  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
120  }
121  }
122  vpDisplay::flush(I);
123  vpTime::wait(20);
124  } while (button != vpMouseButton::button3);
125 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
126  tracker.initTracking(cvI, &feature[0], feature.size());
127 #else
128  tracker.initTracking(cvI, feature);
129 #endif
130  }
131 
132  tracker.track(cvI);
133 
134  tracker.display(I, vpColor::red);
135  vpDisplay::flush(I);
136  }
138 
142 
144 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
145  cvReleaseImage(&cvI);
146 #endif
147 
149  return 0;
150  } catch (vpException &e) {
151  std::cout << "Catch an exception: " << e << std::endl;
152  }
153 #else
154  (void)argc;
155  (void)argv;
156 #endif
157 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:120
static const vpColor green
Definition: vpColor.h:183
static void flush(const vpImage< unsigned char > &I)
long getFirstFrameIndex()
static const vpColor red
Definition: vpColor.h:180
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:263
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 acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
void setPyramidLevels(const int pyrMaxLevel)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void setWindowSize(const int winSize)
long getFrameIndex() const
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
void setBlockSize(const int blockSize)
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)
double get_v() const
Definition: vpImagePoint.h:274
void track(const cv::Mat &I)