Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
tutorial-mb-tracker-stereo-mono.cpp
1 #include <iostream>
3 #include <visp3/core/vpConfig.h>
4 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
5 
6 #include <visp3/core/vpIoTools.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
12 #include <visp3/mbt/vpMbEdgeKltMultiTracker.h>
13 #include <visp3/mbt/vpMbEdgeMultiTracker.h>
15 #include <visp3/io/vpVideoReader.h>
16 
17 int main(int argc, char **argv)
18 {
19 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
20  try {
21  std::string opt_videoname = "teabox.mpg";
22  int opt_tracker = 0;
23 
24  for (int i = 0; i < argc; i++) {
25  if (std::string(argv[i]) == "--name" && i + 1 < argc)
26  opt_videoname = std::string(argv[i + 1]);
27  else if (std::string(argv[i]) == "--tracker" && i + 1 < argc)
28  opt_tracker = atoi(argv[i + 1]);
29  else if (std::string(argv[i]) == "--help") {
30  std::cout << "\nUsage: " << argv[0]
31  << " [--name <video name>] [--tracker "
32  "<0=egde|1=keypoint|2=hybrid>] [--help]\n"
33  << std::endl;
34  return 0;
35  }
36  }
37  std::string parentname = vpIoTools::getParent(opt_videoname);
38  std::string objectname = vpIoTools::getNameWE(opt_videoname);
39 
40  if (!parentname.empty()) {
41  objectname = parentname + "/" + objectname;
42  }
43 
44  std::cout << "Video name: " << opt_videoname << std::endl;
45  std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
46  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
47 
55 
56  vpVideoReader g;
57  g.setFileName(opt_videoname);
58  g.open(I);
59 
60  vpDisplay *display = NULL;
61 #if defined(VISP_HAVE_X11)
62  display = new vpDisplayX;
63 #elif defined(VISP_HAVE_GDI)
64  display = new vpDisplayGDI;
65 #else
66  display = new vpDisplayOpenCV;
67 #endif
68  display->init(I, 100, 100, "Model-based tracker");
69 
71  vpMbTracker *tracker;
72  if (opt_tracker == 0)
73  tracker = new vpMbEdgeMultiTracker;
74 #ifdef VISP_HAVE_MODULE_KLT
75  else if (opt_tracker == 1)
76  tracker = new vpMbKltMultiTracker;
77  else
78  tracker = new vpMbEdgeKltMultiTracker;
79 #else
80  else {
81  std::cout << "klt and hybrid model-based tracker are not available "
82  "since visp_klt module is missing"
83  << std::endl;
84  return 0;
85  }
86 #endif
87 
90  if (opt_tracker == 0 || opt_tracker == 2) {
91  vpMe me;
92  me.setMaskSize(5);
93  me.setMaskNumber(180);
94  me.setRange(8);
95  me.setThreshold(10000);
96  me.setMu1(0.5);
97  me.setMu2(0.5);
98  me.setSampleStep(4);
99  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->setMovingEdge(me);
100  }
101 
102 #ifdef VISP_HAVE_MODULE_KLT
103  if (opt_tracker == 1 || opt_tracker == 2) {
104  vpKltOpencv klt_settings;
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  dynamic_cast<vpMbKltMultiTracker *>(tracker)->setKltOpencv(klt_settings);
113  dynamic_cast<vpMbKltMultiTracker *>(tracker)->setKltMaskBorder(5);
114  }
115 #endif
116 
118  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
120  tracker->setCameraParameters(cam);
122 
124  tracker->loadModel(objectname + ".cao");
127  tracker->setDisplayFeatures(true);
130  tracker->initClick(I, objectname + ".init", true);
132 
133  while (!g.end()) {
134  g.acquire(I);
137  tracker->track(I);
140  tracker->getPose(cMo);
143  tracker->getCameraParameters(cam);
144  tracker->display(I, cMo, cam, vpColor::red, 2, true);
146  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
147  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
148  vpDisplay::flush(I);
149 
150  if (vpDisplay::getClick(I, false)) {
151  break;
152  }
153  }
156  delete display;
157  delete tracker;
159  } catch (const vpException &e) {
160  std::cerr << "Catch a ViSP exception: " << e << std::endl;
161  }
162 #else
163  (void)argc;
164  (void)argv;
165  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
166 #endif
167 }
168 #else
169 int main()
170 {
171  std::cout << "Nothing to run, deprecated tutorial." << std::endl;
172  return 0;
173 }
174 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:503
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:171
virtual void track(const vpImage< unsigned char > &I)=0
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:129
virtual void loadModel(const std::string &modelFile, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
void setMaxFeatures(const int maxCount)
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:151
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:192
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
Hybrid stereo (or more) tracker based on moving-edges and keypoints tracked using KLT tracker...
Definition: vpMe.h:60
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1477
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:180
void setQuality(double qualityLevel)
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void open(vpImage< vpRGBa > &I)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)=0
virtual void setCameraParameters(const vpCameraParameters &camera)
Definition: vpMbTracker.h:473
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:461
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:248
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:110
void acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
void setPyramidLevels(const int pyrMaxLevel)
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void setWindowSize(const int winSize)
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:1464
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
virtual void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:414
void setBlockSize(const int blockSize)
void setThreshold(const double &t)
Definition: vpMe.h:300
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
void setRange(const unsigned int &r)
Definition: vpMe.h:271
Make the complete stereo (or more) tracking of an object by using its CAD model.
Model based stereo (or more) tracker using only KLT.