ViSP  2.10.0
tutorial-klt-tracker.cpp
1 #include <visp/vpImageConvert.h>
4 #include <visp/vpKltOpencv.h>
5 #include <visp/vpDisplayOpenCV.h>
6 #include <visp/vpVideoReader.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,
75  "Left click to select a point, right to start tracking",
76  vpColor::red);
77  if (vpDisplay::getClick(I, ip, button, false)) {
78  if (button == vpMouseButton::button1) {
79  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
81  }
82  }
84  vpTime::wait(20);
85  } while(button != vpMouseButton::button3);
86 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
87  tracker.initTracking(cvI, &feature[0], feature.size());
88 #else
89  tracker.initTracking(cvI, feature);
90 #endif
91  }
92  else {
94  tracker.initTracking(cvI);
96  }
97 
99  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
101 
103  while ( ! reader.end() )
104  {
105  reader.acquire(I);
107 
108  vpImageConvert::convert(I, cvI);
109 
110  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
112 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
113  std::vector<CvPoint2D32f> feature;
114 #else
115  std::vector<cv::Point2f> feature;
116 #endif
117  vpImagePoint ip;
118  do {
119  vpDisplay::displayText(I, 10, 10,
120  "Left click to select a point, right to start tracking",
121  vpColor::red);
122  if (vpDisplay::getClick(I, ip, button, false)) {
123  if (button == vpMouseButton::button1) {
124  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
126  }
127  }
128  vpDisplay::flush(I);
129  vpTime::wait(20);
130  } while(button != vpMouseButton::button3);
131 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
132  tracker.initTracking(cvI, &feature[0], feature.size());
133 #else
134  tracker.initTracking(cvI, feature);
135 #endif
136  }
137 
138  tracker.track(cvI);
139 
140  tracker.display(I, vpColor::red);
141  vpDisplay::flush(I);
142  }
144 
148 
150 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
151  cvReleaseImage(&cvI);
152 #endif
153 
155  return 0;
156  }
157  catch(vpException e) {
158  std::cout << "Catch an exception: " << e << std::endl;
159  }
160 #else
161  (void)argc;
162  (void)argv;
163 #endif
164 }
long getFrameIndex() const
double get_v() const
Definition: vpImagePoint.h:264
long getFirstFrameIndex() const
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
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: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 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)
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)
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:121
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)