Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-mb-edge-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/vpMbEdgeTracker.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 videoname = "teabox.mpg";
17 
18  for (int i = 0; i < argc; i++) {
19  if (std::string(argv[i]) == "--name")
20  videoname = std::string(argv[i + 1]);
21  else if (std::string(argv[i]) == "--help") {
22  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\n" << std::endl;
23  return 0;
24  }
25  }
26  std::string parentname = vpIoTools::getParent(videoname);
27  std::string objectname = vpIoTools::getNameWE(videoname);
28 
29  if (!parentname.empty())
30  objectname = parentname + "/" + objectname;
31 
32  std::cout << "Video name: " << videoname << std::endl;
33  std::cout << "Tracker requested config files: " << objectname << ".[init,"
34 #ifdef VISP_HAVE_PUGIXML
35  << "xml,"
36 #endif
37  << "cao or wrl]" << std::endl;
38  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
39 
47 
48  vpVideoReader g;
49  g.setFileName(videoname);
50  g.open(I);
51 
52 #if defined(VISP_HAVE_X11)
53  vpDisplayX display;
54 #elif defined(VISP_HAVE_GDI)
55  vpDisplayGDI display;
56 #elif defined(VISP_HAVE_OPENCV)
57  vpDisplayOpenCV display;
58 #else
59  std::cout << "No image viewer is available..." << std::endl;
60  return 0;
61 #endif
62 
63  display.init(I, 100, 100, "Model-based edge tracker");
64 
66  vpMbEdgeTracker tracker;
68  bool usexml = false;
70 #ifdef VISP_HAVE_PUGIXML
71  if (vpIoTools::checkFilename(objectname + ".xml")) {
72  tracker.loadConfigFile(objectname + ".xml");
73  usexml = true;
74  }
75 #endif
76  if (!usexml) {
79  vpMe me;
80  me.setMaskSize(5);
81  me.setMaskNumber(180);
82  me.setRange(8);
83  me.setThreshold(10000);
84  me.setMu1(0.5);
85  me.setMu2(0.5);
86  me.setSampleStep(4);
87  tracker.setMovingEdge(me);
88  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
89  tracker.setCameraParameters(cam);
91  tracker.setAngleAppear(vpMath::rad(70));
92  tracker.setAngleDisappear(vpMath::rad(80));
95  tracker.setNearClippingDistance(0.1);
96  tracker.setFarClippingDistance(100.0);
102  }
104  tracker.setOgreVisibilityTest(false);
105  tracker.setOgreShowConfigDialog(false);
108  if (vpIoTools::checkFilename(objectname + ".cao"))
109  tracker.loadModel(objectname + ".cao");
112  else if (vpIoTools::checkFilename(objectname + ".wrl"))
113  tracker.loadModel(objectname + ".wrl");
116  tracker.setDisplayFeatures(true);
119  tracker.initClick(I, objectname + ".init", true);
121 
122  while (!g.end()) {
123  g.acquire(I);
126  tracker.track(I);
129  tracker.getPose(cMo);
132  tracker.getCameraParameters(cam);
133  tracker.display(I, cMo, cam, vpColor::red, 2);
135  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
136  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
137  vpDisplay::flush(I);
138  if (vpDisplay::getClick(I, false))
139  break;
140  }
143 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION >= 2)
144  SoDB::finish();
145 #endif
146  } catch (const vpException &e) {
148  std::cout << "Catch a ViSP exception: " << e << std::endl;
149  }
150 #ifdef VISP_HAVE_OGRE
151  catch (Ogre::Exception &e) {
152  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
153  }
154 #endif
155 #else
156  (void)argc;
157  (void)argv;
158  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
159 #endif
160 }
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:252
void setMovingEdge(const vpMe &me)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:476
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual void track(const vpImage< unsigned char > &I)
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 initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
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
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:639
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
Make the complete tracking of an object by using its CAD model.
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1473
void loadConfigFile(const std::string &configFile)
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
virtual void setNearClippingDistance(const double &dist)
virtual void setOgreVisibilityTest(const bool &v)
void open(vpImage< vpRGBa > &I)
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:410
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...
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
virtual void setFarClippingDistance(const double &dist)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:465
static double rad(double deg)
Definition: vpMath.h:108
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
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 setThreshold(const double &t)
Definition: vpMe.h:300
void setFileName(const std::string &filename)
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:730
virtual void setCameraParameters(const vpCameraParameters &cam)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
virtual void setClipping(const unsigned int &flags)
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:513
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:244