Visual Servoing Platform  version 3.4.0
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.mp4";
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" || std::string(argv[i]) == "-h") {
22  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\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  << "xml,"
35  << "cao or wrl]" << std::endl;
36  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
37 
45 
46  vpVideoReader g;
47  g.setFileName(videoname);
48  g.open(I);
49 
50 #if defined(VISP_HAVE_X11)
51  vpDisplayX display;
52 #elif defined(VISP_HAVE_GDI)
53  vpDisplayGDI display;
54 #elif defined(VISP_HAVE_OPENCV)
55  vpDisplayOpenCV display;
56 #else
57  std::cout << "No image viewer is available..." << std::endl;
58  return 0;
59 #endif
60 
61  display.init(I, 100, 100, "Model-based edge tracker");
62 
64  vpMbEdgeTracker tracker;
66  bool usexml = false;
68  if (vpIoTools::checkFilename(objectname + ".xml")) {
69  tracker.loadConfigFile(objectname + ".xml");
70  usexml = true;
71  }
73  if (!usexml) {
75  vpMe me;
76  me.setMaskSize(5);
77  me.setMaskNumber(180);
78  me.setRange(8);
79  me.setThreshold(10000);
80  me.setMu1(0.5);
81  me.setMu2(0.5);
82  me.setSampleStep(4);
83  tracker.setMovingEdge(me);
84  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
85  tracker.setCameraParameters(cam);
87  tracker.setAngleAppear(vpMath::rad(70));
88  tracker.setAngleDisappear(vpMath::rad(80));
91  tracker.setNearClippingDistance(0.1);
92  tracker.setFarClippingDistance(100.0);
98  }
100  tracker.setOgreVisibilityTest(false);
101  tracker.setOgreShowConfigDialog(false);
104  if (vpIoTools::checkFilename(objectname + ".cao"))
105  tracker.loadModel(objectname + ".cao");
108  else if (vpIoTools::checkFilename(objectname + ".wrl"))
109  tracker.loadModel(objectname + ".wrl");
112  tracker.setDisplayFeatures(true);
115  tracker.initClick(I, objectname + ".init", true);
117 
118  while (!g.end()) {
119  g.acquire(I);
122  tracker.track(I);
125  tracker.getPose(cMo);
128  tracker.getCameraParameters(cam);
129  tracker.display(I, cMo, cam, vpColor::red, 2);
131  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
132  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
133  vpDisplay::flush(I);
134  if (vpDisplay::getClick(I, false))
135  break;
136  }
138  } catch (const vpException &e) {
139  std::cout << "Catch a ViSP exception: " << e << std::endl;
140  }
141 #ifdef VISP_HAVE_OGRE
142  catch (Ogre::Exception &e) {
143  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
144  }
145 #endif
146 #else
147  (void)argc;
148  (void)argv;
149  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
150 #endif
151 }
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 unsigned int getClipping() const
Definition: vpMbTracker.h:256
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:480
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: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
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:229
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:643
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:1382
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
virtual void setNearClippingDistance(const double &dist)
virtual void setOgreVisibilityTest(const bool &v)
void open(vpImage< vpRGBa > &I)
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:459
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:469
static double rad(double deg)
Definition: vpMath.h:110
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:248
void setMu2(const double &mu_2)
Definition: vpMe.h:248
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:414
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:1369
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:640
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:517