Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
tutorial-mb-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/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.mpg";
19  std::string opt_modelname = "teabox.cao";
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") {
30  std::cout << "\nUsage: " << argv[0]
31  << " [--video <video name>] [--model <model name>] "
32  "[--tracker <0=egde|1=keypoint|2=hybrid>] [--help]\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,"
45 #ifdef VISP_HAVE_XML2
46  << "xml,"
47 #endif
48  << "cao or wrl]" << std::endl;
49  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
50 
58 
59  vpVideoReader g;
60  g.setFileName(opt_videoname);
61  g.open(I);
62 
63  vpDisplay *display = NULL;
64 #if defined(VISP_HAVE_X11)
65  display = new vpDisplayX;
66 #elif defined(VISP_HAVE_GDI)
67  display = new vpDisplayGDI;
68 #else
69  display = new vpDisplayOpenCV;
70 #endif
71  display->init(I, 100, 100, "Model-based tracker");
72 
74  vpMbTracker *tracker;
75  if (opt_tracker == 0)
76  tracker = new vpMbEdgeTracker;
77 #ifdef VISP_HAVE_MODULE_KLT
78  else if (opt_tracker == 1)
79  tracker = new vpMbKltTracker;
80  else
81  tracker = new vpMbEdgeKltTracker;
82 #else
83  else {
84  std::cout << "klt and hybrid model-based tracker are not available "
85  "since visp_klt module is missing"
86  << std::endl;
87  return 0;
88  }
89 #endif
90 
92  bool usexml = false;
94 #ifdef VISP_HAVE_XML2
95  if (vpIoTools::checkFilename(objectname + ".xml")) {
96  tracker->loadConfigFile(objectname + ".xml");
97  usexml = true;
98  }
99 #endif
100 
102  if (!usexml) {
104  if (opt_tracker == 0 || opt_tracker == 2) {
106  vpMe me;
107  me.setMaskSize(5);
108  me.setMaskNumber(180);
109  me.setRange(8);
110  me.setThreshold(10000);
111  me.setMu1(0.5);
112  me.setMu2(0.5);
113  me.setSampleStep(4);
114  dynamic_cast<vpMbEdgeTracker *>(tracker)->setMovingEdge(me);
116  }
117 
118 #ifdef VISP_HAVE_MODULE_KLT
119  if (opt_tracker == 1 || opt_tracker == 2) {
121  vpKltOpencv klt_settings;
122  klt_settings.setMaxFeatures(300);
123  klt_settings.setWindowSize(5);
124  klt_settings.setQuality(0.015);
125  klt_settings.setMinDistance(8);
126  klt_settings.setHarrisFreeParameter(0.01);
127  klt_settings.setBlockSize(3);
128  klt_settings.setPyramidLevels(3);
129  dynamic_cast<vpMbKltTracker *>(tracker)->setKltOpencv(klt_settings);
130  dynamic_cast<vpMbKltTracker *>(tracker)->setKltMaskBorder(5);
132  }
133 #endif
134 
136  cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
137  tracker->setCameraParameters(cam);
139 
141  tracker->setAngleAppear(vpMath::rad(70));
142  tracker->setAngleDisappear(vpMath::rad(80));
145  tracker->setNearClippingDistance(0.1);
146  tracker->setFarClippingDistance(100.0);
149  tracker->setClipping(tracker->getClipping() | vpMbtPolygon::FOV_CLIPPING);
152  }
155  tracker->setOgreVisibilityTest(false);
156  tracker->setOgreShowConfigDialog(false);
159  tracker->setScanLineVisibilityTest(true);
162 
164  if (vpIoTools::checkFilename(objectname + ".cao"))
165  tracker->loadModel(objectname + ".cao");
168  else if (vpIoTools::checkFilename(objectname + ".wrl"))
169  tracker->loadModel(objectname + ".wrl");
172  tracker->setDisplayFeatures(true);
175  tracker->initClick(I, objectname + ".init", true);
177 
178  while (!g.end()) {
179  g.acquire(I);
182  tracker->track(I);
185  tracker->getPose(cMo);
188  tracker->getCameraParameters(cam);
189  tracker->display(I, cMo, cam, vpColor::red, 2);
191  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
192  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
193  vpDisplay::flush(I);
194 
195  if (vpDisplay::getClick(I, false))
196  break;
197  }
200 #ifdef VISP_HAVE_XML2
202 #endif
203 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION >= 2)
204  SoDB::finish();
205 #endif
206  delete display;
207  delete tracker;
209  } catch (const vpException &e) {
210  std::cout << "Catch a ViSP exception: " << e << std::endl;
211  }
212 #ifdef VISP_HAVE_OGRE
213  catch (Ogre::Exception &e) {
214  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
215  }
216 #endif
217 #else
218  (void)argc;
219  (void)argv;
220  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
221 #endif
222 }
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:503
virtual void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:629
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)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:256
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:466
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:454
virtual void loadConfigFile(const std::string &configFile)
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
Definition: vpMe.h:60
Make the complete tracking of an object by using its CAD model.
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)
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:675
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)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:455
Model based tracker using only KLT.
void setPyramidLevels(const int pyrMaxLevel)
static double rad(double deg)
Definition: vpMath.h:102
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
static void cleanup()
Definition: vpXmlParser.h:310
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void setWindowSize(const int winSize)
virtual void setOgreVisibilityTest(const bool &v)
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())
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:587
virtual void setClipping(const unsigned int &flags)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
virtual void setFarClippingDistance(const double &dist)
virtual void setNearClippingDistance(const double &dist)