Visual Servoing Platform  version 3.5.1 under development (2022-07-03)
tutorial-mb-tracker.cpp
1 #include <visp3/core/vpIoTools.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageIo.h>
8 #include <visp3/mbt/vpMbEdgeKltTracker.h>
9 #include <visp3/mbt/vpMbEdgeTracker.h>
11 #include <visp3/io/vpVideoReader.h>
12 
13 int main(int argc, char **argv)
14 {
15 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
16 
17  try {
18  std::string opt_videoname = "teabox.mp4";
19  std::string opt_modelname = "teabox";
20  int opt_tracker = 0;
21 
22  for (int i = 0; i < argc; i++) {
23  if (std::string(argv[i]) == "--video")
24  opt_videoname = std::string(argv[i + 1]);
25  else if (std::string(argv[i]) == "--model")
26  opt_modelname = std::string(argv[i + 1]);
27  else if (std::string(argv[i]) == "--tracker")
28  opt_tracker = atoi(argv[i + 1]);
29  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
30  std::cout << "\nUsage: " << argv[0]
31  << " [--video <video name>] [--model <model name>] "
32  "[--tracker <0=egde|1=keypoint|2=hybrid>] [--help] [-h]\n"
33  << std::endl;
34  return 0;
35  }
36  }
37  std::string parentname = vpIoTools::getParent(opt_modelname);
38  std::string objectname = vpIoTools::getNameWE(opt_modelname);
39 
40  if (!parentname.empty())
41  objectname = parentname + "/" + objectname;
42 
43  std::cout << "Video name: " << opt_videoname << std::endl;
44  std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
45  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
46 
54 
55  vpVideoReader g;
56  g.setFileName(opt_videoname);
57  g.open(I);
58 
59  vpDisplay *display = NULL;
60 #if defined(VISP_HAVE_X11)
61  display = new vpDisplayX;
62 #elif defined(VISP_HAVE_GDI)
63  display = new vpDisplayGDI;
64 #else
65  display = new vpDisplayOpenCV;
66 #endif
67  display->init(I, 100, 100, "Model-based tracker");
68 
70  vpMbTracker *tracker;
71  if (opt_tracker == 0)
72  tracker = new vpMbEdgeTracker;
73 #ifdef VISP_HAVE_MODULE_KLT
74  else if (opt_tracker == 1)
75  tracker = new vpMbKltTracker;
76  else
77  tracker = new vpMbEdgeKltTracker;
78 #else
79  else {
80  std::cout << "klt and hybrid model-based tracker are not available "
81  "since visp_klt module is missing"
82  << std::endl;
83  return 0;
84  }
85 #endif
87 
89  if (opt_tracker == 0 || opt_tracker == 2) {
90  vpMe me;
91  me.setMaskSize(5);
92  me.setMaskNumber(180);
93  me.setRange(8);
94  me.setThreshold(10000);
95  me.setMu1(0.5);
96  me.setMu2(0.5);
97  me.setSampleStep(4);
98  dynamic_cast<vpMbEdgeTracker *>(tracker)->setMovingEdge(me);
99  }
100 
101 #ifdef VISP_HAVE_MODULE_KLT
102  if (opt_tracker == 1 || opt_tracker == 2) {
103  vpKltOpencv klt_settings;
104  klt_settings.setMaxFeatures(300);
105  klt_settings.setWindowSize(5);
106  klt_settings.setQuality(0.015);
107  klt_settings.setMinDistance(8);
108  klt_settings.setHarrisFreeParameter(0.01);
109  klt_settings.setBlockSize(3);
110  klt_settings.setPyramidLevels(3);
111  dynamic_cast<vpMbKltTracker *>(tracker)->setKltOpencv(klt_settings);
112  dynamic_cast<vpMbKltTracker *>(tracker)->setKltMaskBorder(5);
113  }
114 #endif
115 
117  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
119  tracker->setCameraParameters(cam);
121 
123  tracker->loadModel(objectname + ".cao");
126  tracker->setDisplayFeatures(true);
129  tracker->initClick(I, objectname + ".init", true);
131 
132  while (!g.end()) {
133  g.acquire(I);
136  tracker->track(I);
139  tracker->getPose(cMo);
142  tracker->getCameraParameters(cam);
143  tracker->display(I, cMo, cam, vpColor::red, 2);
145  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
146  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
147  vpDisplay::flush(I);
148 
149  if (vpDisplay::getClick(I, false))
150  break;
151  }
154  delete display;
155  delete tracker;
157  } catch (const vpException &e) {
158  std::cout << "Catch a ViSP exception: " << e << std::endl;
159  }
160 #else
161  (void)argc;
162  (void)argv;
163  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
164 #endif
165 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emited by ViSP classes.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1536
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1620
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:79
void setBlockSize(int blockSize)
void setQuality(double qualityLevel)
void setHarrisFreeParameter(double harris_k)
void setMaxFeatures(int maxCount)
void setMinDistance(double minDistance)
void setWindowSize(int winSize)
void setPyramidLevels(int pyrMaxLevel)
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
Make the complete tracking of an object by using its CAD model.
Model based tracker using only KLT.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:105
virtual void track(const vpImage< unsigned char > &I)=0
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:248
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:518
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:414
virtual void setCameraParameters(const vpCameraParameters &cam)
Definition: vpMbTracker.h:488
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)=0
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
Definition: vpMe.h:61
void setMu1(const double &mu_1)
Definition: vpMe.h:241
void setSampleStep(const double &s)
Definition: vpMe.h:278
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:459
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:452
void setThreshold(const double &t)
Definition: vpMe.h:300
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)