Visual Servoing Platform  version 3.4.0
tutorial-mb-generic-tracker-full.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 
16  try {
17  std::string opt_videoname = "model/teabox/teabox.mp4";
18  std::string opt_modelname = "model/teabox/teabox.cao";
19  int opt_tracker = 0;
20 
21  for (int i = 0; i < argc; i++) {
22  if (std::string(argv[i]) == "--video")
23  opt_videoname = std::string(argv[i + 1]);
24  else if (std::string(argv[i]) == "--model")
25  opt_modelname = std::string(argv[i + 1]);
26  else if (std::string(argv[i]) == "--tracker")
27  opt_tracker = atoi(argv[i + 1]);
28  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
29  std::cout << "\nUsage: " << argv[0]
30  << " [--video <video name>] [--model <model name>] "
31  "[--tracker <0=egde|1=keypoint|2=hybrid>] [--help] [-h]\n"
32  << std::endl;
33  return 0;
34  }
35  }
36  std::string parentname = vpIoTools::getParent(opt_modelname);
37  std::string objectname = vpIoTools::getNameWE(opt_modelname);
38 
39  if (!parentname.empty())
40  objectname = parentname + "/" + objectname;
41 
42  std::cout << "Video name: " << opt_videoname << std::endl;
43  std::cout << "Tracker requested config files: " << objectname << ".[init,"
44  << "xml,"
45  << "cao or wrl]" << 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  vpMbGenericTracker tracker;
72  if (opt_tracker == 0)
74 #ifdef VISP_HAVE_MODULE_KLT
75  else if (opt_tracker == 1)
77  else
79 #else
80  else {
81  std::cout << "klt and hybrid model-based tracker are not available since visp_klt module is not available. "
82  "In CMakeGUI turn visp_klt module ON, configure and build ViSP again."
83  << std::endl;
84  return 0;
85  }
86 #endif
87 
89  bool usexml = false;
91  if (vpIoTools::checkFilename(objectname + ".xml")) {
92  tracker.loadConfigFile(objectname + ".xml");
93  usexml = true;
94  }
96 
97  if (!usexml) {
99  if (opt_tracker == 0 || opt_tracker == 2) {
101  vpMe me;
102  me.setMaskSize(5);
103  me.setMaskNumber(180);
104  me.setRange(8);
105  me.setThreshold(10000);
106  me.setMu1(0.5);
107  me.setMu2(0.5);
108  me.setSampleStep(4);
109  tracker.setMovingEdge(me);
111  }
112 
113 #ifdef VISP_HAVE_MODULE_KLT
114  if (opt_tracker == 1 || opt_tracker == 2) {
116  vpKltOpencv klt_settings;
117  klt_settings.setMaxFeatures(300);
118  klt_settings.setWindowSize(5);
119  klt_settings.setQuality(0.015);
120  klt_settings.setMinDistance(8);
121  klt_settings.setHarrisFreeParameter(0.01);
122  klt_settings.setBlockSize(3);
123  klt_settings.setPyramidLevels(3);
124  tracker.setKltOpencv(klt_settings);
125  tracker.setKltMaskBorder(5);
127  }
128 #endif
129 
131  cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
132  tracker.setCameraParameters(cam);
134 
136  tracker.setAngleAppear(vpMath::rad(70));
137  tracker.setAngleDisappear(vpMath::rad(80));
140  tracker.setNearClippingDistance(0.1);
141  tracker.setFarClippingDistance(100.0);
147  }
148 
151  tracker.setOgreVisibilityTest(false);
152  tracker.setOgreShowConfigDialog(false);
155  tracker.setScanLineVisibilityTest(true);
158 
160  if (vpIoTools::checkFilename(objectname + ".cao"))
161  tracker.loadModel(objectname + ".cao");
164  else if (vpIoTools::checkFilename(objectname + ".wrl"))
165  tracker.loadModel(objectname + ".wrl");
168  tracker.setDisplayFeatures(true);
171  tracker.initClick(I, objectname + ".init", true);
173 
174  while (!g.end()) {
175  g.acquire(I);
178  tracker.track(I);
181  tracker.getPose(cMo);
184  tracker.getCameraParameters(cam);
185  tracker.display(I, cMo, cam, vpColor::red, 2);
187  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
188  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
189  {
190  std::stringstream ss;
191  ss << "Features: edges " << tracker.getNbFeaturesEdge() << ", klt " << tracker.getNbFeaturesKlt();
192  vpDisplay::displayText(I, 30, 10, ss.str(), vpColor::red);
193  }
194  vpDisplay::flush(I);
195 
196  if (vpDisplay::getClick(I, false))
197  break;
198  }
201  delete display;
203  } catch (const vpException &e) {
204  std::cout << "Catch a ViSP exception: " << e << std::endl;
205  }
206 #ifdef VISP_HAVE_OGRE
207  catch (Ogre::Exception &e) {
208  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
209  }
210 #endif
211 #else
212  (void)argc;
213  (void)argv;
214  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
215 #endif
216 }
void setMaxFeatures(int maxCount)
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:177
virtual void track(const vpImage< unsigned char > &I)
virtual void setOgreShowConfigDialog(bool showConfigDialog)
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:452
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:278
virtual void setClipping(const unsigned int &flags)
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:229
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())
virtual void setAngleDisappear(const double &a)
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:1382
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:217
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:459
void setPyramidLevels(int pyrMaxLevel)
virtual void getPose(vpHomogeneousMatrix &cMo) const
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 loadConfigFile(const std::string &configFile, bool verbose=true)
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
virtual void setAngleAppear(const double &a)
void setWindowSize(int winSize)
static double rad(double deg)
Definition: vpMath.h:110
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
virtual void setNearClippingDistance(const double &dist)
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void setScanLineVisibilityTest(const bool &v)
void setMu2(const double &mu_2)
Definition: vpMe.h:248
virtual void getCameraParameters(vpCameraParameters &camera) const
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))
virtual void setOgreVisibilityTest(const bool &v)
virtual void setFarClippingDistance(const double &dist)
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1369
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)
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:640
virtual unsigned int getNbFeaturesEdge() const
void setRange(const unsigned int &r)
Definition: vpMe.h:271
virtual void setKltOpencv(const vpKltOpencv &t)
virtual unsigned int getNbFeaturesKlt() const
void setBlockSize(int blockSize)
virtual void getClipping(unsigned int &clippingFlag1, unsigned int &clippingFlag2) const