Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-mb-generic-tracker-stereo-mono.cpp
1 #include <cstdlib>
3 #include <visp3/core/vpConfig.h>
4 #include <visp3/core/vpIoTools.h>
5 #include <visp3/gui/vpDisplayGDI.h>
6 #include <visp3/gui/vpDisplayOpenCV.h>
7 #include <visp3/gui/vpDisplayX.h>
8 #include <visp3/io/vpImageIo.h>
10 #include <visp3/mbt/vpMbGenericTracker.h>
12 #include <visp3/io/vpVideoReader.h>
13 
14 int main(int argc, char **argv)
15 {
16 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020300)
17  try {
18  std::string opt_videoname = "teabox.mpg";
19  int opt_tracker = vpMbGenericTracker::EDGE_TRACKER;
20 
21  for (int i = 0; i < argc; i++) {
22  if (std::string(argv[i]) == "--name" && i + 1 < argc)
23  opt_videoname = std::string(argv[i + 1]);
24  else if (std::string(argv[i]) == "--tracker" && i + 1 < argc)
25  opt_tracker = atoi(argv[i + 1]);
26  else if (std::string(argv[i]) == "--help") {
27  std::cout << "\nUsage: " << argv[0]
28  << " [--name <video name>] [--tracker "
29  "<1=egde|2=keypoint|3=hybrid>] [--help]\n"
30  << std::endl;
31  return EXIT_SUCCESS;
32  }
33  }
34 
35  if (opt_tracker < 1 || opt_tracker > 3) {
36  std::cerr << "Wrong tracker type. Correct values are: "
37  "1=egde|2=keypoint|3=hybrid."
38  << std::endl;
39  return EXIT_SUCCESS;
40  }
41 
42  std::string parentname = vpIoTools::getParent(opt_videoname);
43  std::string objectname = vpIoTools::getNameWE(opt_videoname);
44 
45  if (!parentname.empty()) {
46  objectname = parentname + "/" + objectname;
47  }
48 
49  std::cout << "Video name: " << opt_videoname << std::endl;
50  std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
51  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
52 
56 
57  vpVideoReader g;
58  g.setFileName(opt_videoname);
59  g.open(I);
60 
61 #if defined(VISP_HAVE_X11)
62  vpDisplayX display;
63 #elif defined(VISP_HAVE_GDI)
64  vpDisplayGDI display;
65 #else
66  vpDisplayOpenCV display;
67 #endif
68  display.init(I, 100, 100, "Model-based tracker");
69 
71  vpMbGenericTracker tracker(1, opt_tracker);
73 
74 #if !defined(VISP_HAVE_MODULE_KLT)
75  if (opt_tracker >= 2) {
76  std::cout << "KLT and hybrid model-based tracker are not available "
77  "since visp_klt module is missing"
78  << std::endl;
79  return EXIT_SUCCESS;
80  }
81 #endif
82 
84 #ifdef VISP_HAVE_PUGIXML
85  tracker.loadConfigFile(objectname + ".xml");
88 #else
89  if (opt_tracker == 1 || opt_tracker == 3) {
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  tracker.setMovingEdge(me);
99  }
100 
101 #ifdef VISP_HAVE_MODULE_KLT
102  if (opt_tracker == 2 || opt_tracker == 3) {
103  vpKltOpencv klt_settings;
104  tracker.setKltMaskBorder(5);
105  klt_settings.setMaxFeatures(300);
106  klt_settings.setWindowSize(5);
107  klt_settings.setQuality(0.015);
108  klt_settings.setMinDistance(8);
109  klt_settings.setHarrisFreeParameter(0.01);
110  klt_settings.setBlockSize(3);
111  klt_settings.setPyramidLevels(3);
112  tracker.setKltOpencv(klt_settings);
113  }
114 #endif
115 
116  {
118  vpCameraParameters cam;
119  cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
120  tracker.setCameraParameters(cam);
122  }
123 #endif
124 
127  tracker.loadModel(objectname + ".cao");
130  tracker.setDisplayFeatures(true);
133  tracker.initClick(I, objectname + ".init", true);
135 
136  while (!g.end()) {
137  g.acquire(I);
140  tracker.track(I);
144  tracker.getPose(cMo);
147  vpCameraParameters cam;
148  tracker.getCameraParameters(cam);
149  tracker.display(I, cMo, cam, vpColor::red, 2);
151  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
152  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
153  vpDisplay::flush(I);
154 
155  if (vpDisplay::getClick(I, false)) {
156  break;
157  }
158  }
160  } catch (const vpException &e) {
161  std::cerr << "Catch a ViSP exception: " << e.what() << std::endl;
162  }
163 
164  return EXIT_SUCCESS;
165 #else
166  (void)argc;
167  (void)argv;
168  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
169  return EXIT_SUCCESS;
170 #endif
171 }
void setMaxFeatures(int maxCount)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setHarrisFreeParameter(double harris_k)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:454
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:278
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor none
Definition: vpColor.h:191
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpMe.h:60
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1473
Real-time 6D object pose tracking using its CAD model.
static void flush(const vpImage< unsigned char > &I)
void setMu1(const double &mu_1)
Definition: vpMe.h:241
static const vpColor red
Definition: vpColor.h:179
void setQuality(double qualityLevel)
void open(vpImage< vpRGBa > &I)
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:461
void setPyramidLevels(int pyrMaxLevel)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
void setWindowSize(int winSize)
const char * what() const
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
void setMu2(const double &mu_2)
Definition: vpMe.h:248
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 std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1460
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
void setThreshold(const double &t)
Definition: vpMe.h:300
void setFileName(const std::string &filename)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setBlockSize(int blockSize)