Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-mb-generic-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/vpMbGenericTracker.h>
10 #include <visp3/io/vpVideoReader.h>
11 
12 int main(int argc, char **argv)
13 {
14 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
15  try {
16  std::string opt_videoname = "model/teabox/teabox.mpg";
17  std::string opt_modelname = "model/teabox/teabox.cao";
18  int opt_tracker = 1;
19 
20  for (int i = 0; i < argc; i++) {
21  if (std::string(argv[i]) == "--video")
22  opt_videoname = std::string(argv[i + 1]);
23  else if (std::string(argv[i]) == "--model")
24  opt_modelname = std::string(argv[i + 1]);
25  else if (std::string(argv[i]) == "--tracker")
26  opt_tracker = atoi(argv[i + 1]);
27  else if (std::string(argv[i]) == "--help") {
28  std::cout << "\nUsage: " << argv[0]
29  << " [--video <video name>] [--model <model name>]"
30  " [--tracker <0=egde|1=keypoint|2=hybrid>] [--help]\n"
31  << std::endl;
32  return 0;
33  }
34  }
35  std::string parentname = vpIoTools::getParent(opt_modelname);
36  std::string objectname = vpIoTools::getNameWE(opt_modelname);
37 
38  if (!parentname.empty())
39  objectname = parentname + "/" + objectname;
40 
41  std::cout << "Video name: " << opt_videoname << std::endl;
42  std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
43  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
44 
52 
53  vpVideoReader g;
54  g.setFileName(opt_videoname);
55  g.open(I);
56 
57  vpDisplay *display = NULL;
58 #if defined(VISP_HAVE_X11)
59  display = new vpDisplayX;
60 #elif defined(VISP_HAVE_GDI)
61  display = new vpDisplayGDI;
62 #else
63  display = new vpDisplayOpenCV;
64 #endif
65  display->init(I, 100, 100, "Model-based tracker");
66 
68  vpMbGenericTracker tracker;
69  if (opt_tracker == 0)
71 #ifdef VISP_HAVE_MODULE_KLT
72  else if (opt_tracker == 1)
74  else
76 #else
77  else {
78  std::cout << "klt and hybrid model-based tracker are not available since visp_klt module is not available. "
79  "In CMakeGUI turn visp_klt module ON, configure and build ViSP again."
80  << std::endl;
81  return 0;
82  }
83 #endif
84 
87  if (opt_tracker == 0 || opt_tracker == 2) {
88  vpMe me;
89  me.setMaskSize(5);
90  me.setMaskNumber(180);
91  me.setRange(8);
92  me.setThreshold(10000);
93  me.setMu1(0.5);
94  me.setMu2(0.5);
95  me.setSampleStep(4);
96  tracker.setMovingEdge(me);
97  }
98 
99 #ifdef VISP_HAVE_MODULE_KLT
100  if (opt_tracker == 1 || opt_tracker == 2) {
101  vpKltOpencv klt_settings;
102  klt_settings.setMaxFeatures(300);
103  klt_settings.setWindowSize(5);
104  klt_settings.setQuality(0.015);
105  klt_settings.setMinDistance(8);
106  klt_settings.setHarrisFreeParameter(0.01);
107  klt_settings.setBlockSize(3);
108  klt_settings.setPyramidLevels(3);
109  tracker.setKltOpencv(klt_settings);
110  tracker.setKltMaskBorder(5);
111  }
112 #endif
113 
115  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
117  tracker.setCameraParameters(cam);
119 
121  tracker.loadModel(objectname + ".cao");
124  tracker.setDisplayFeatures(true);
127  tracker.initClick(I, objectname + ".init", true);
129 
130  while (!g.end()) {
131  g.acquire(I);
134  tracker.track(I);
137  tracker.getPose(cMo);
140  tracker.getCameraParameters(cam);
141  tracker.display(I, cMo, cam, vpColor::red, 2);
143  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
144  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
145  vpDisplay::flush(I);
146 
147  if (vpDisplay::getClick(I, false))
148  break;
149  }
152  delete display;
154  } catch (const vpException &e) {
155  std::cout << "Catch a ViSP exception: " << e << std::endl;
156  }
157 #else
158  (void)argc;
159  (void)argv;
160  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
161 #endif
162 }
void setMaxFeatures(int maxCount)
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:171
virtual void track(const vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
void setHarrisFreeParameter(double harris_k)
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual void setTrackerType(int type)
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)
virtual void setDisplayFeatures(bool displayF)
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)
virtual void initClick(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, const std::string &initFile1, const std::string &initFile2, bool displayHelp=false, const vpHomogeneousMatrix &T1=vpHomogeneousMatrix(), const vpHomogeneousMatrix &T2=vpHomogeneousMatrix())
error that can be emited by ViSP classes.
Definition: vpException.h:71
Definition: vpMe.h:60
virtual void setMovingEdge(const vpMe &me)
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)
virtual void setKltMaskBorder(const unsigned int &e)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
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)
virtual void getCameraParameters(vpCameraParameters &camera) const
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
virtual void setCameraParameters(const vpCameraParameters &camera)
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
virtual void setKltOpencv(const vpKltOpencv &t)
void setBlockSize(int blockSize)
virtual void getPose(vpHomogeneousMatrix &cMo) const