Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
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)
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 EXIT_SUCCESS;
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)
52 #elif defined(VISP_HAVE_GDI)
54 #elif defined(HAVE_OPENCV_HIGHGUI)
56 #else
57  std::cout << "No image viewer is available..." << std::endl;
58  return EXIT_FAILURE;
59 #endif
60 
61  display.init(I, 100, 100, "Model-based edge tracker");
62 
64  vpMbEdgeTracker tracker;
66  bool usexml = false;
68 #if defined(VISP_HAVE_PUGIXML)
69  if (vpIoTools::checkFilename(objectname + ".xml")) {
70  tracker.loadConfigFile(objectname + ".xml");
71  usexml = true;
72  }
73 #endif
75  if (!usexml) {
77  vpMe me;
78  me.setMaskSize(5);
79  me.setMaskNumber(180);
80  me.setRange(8);
82  me.setThreshold(20);
83  me.setMu1(0.5);
84  me.setMu2(0.5);
85  me.setSampleStep(4);
86  tracker.setMovingEdge(me);
87  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
88  tracker.setCameraParameters(cam);
90  tracker.setAngleAppear(vpMath::rad(70));
91  tracker.setAngleDisappear(vpMath::rad(80));
94  tracker.setNearClippingDistance(0.1);
95  tracker.setFarClippingDistance(100.0);
101  }
103  tracker.setOgreVisibilityTest(false);
104  tracker.setOgreShowConfigDialog(false);
107  if (vpIoTools::checkFilename(objectname + ".cao"))
108  tracker.loadModel(objectname + ".cao");
111  else if (vpIoTools::checkFilename(objectname + ".wrl"))
112  tracker.loadModel(objectname + ".wrl");
115  tracker.setDisplayFeatures(true);
118  tracker.initClick(I, objectname + ".init", true);
120 
121  while (!g.end()) {
122  g.acquire(I);
125  tracker.track(I);
128  tracker.getPose(cMo);
131  tracker.getCameraParameters(cam);
132  tracker.display(I, cMo, cam, vpColor::red, 2);
134  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
135  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
136  vpDisplay::flush(I);
137  if (vpDisplay::getClick(I, false))
138  break;
139  }
141  }
142  catch (const vpException &e) {
143  std::cout << "Catch a ViSP exception: " << e << std::endl;
144  }
145 #ifdef VISP_HAVE_OGRE
146  catch (Ogre::Exception &e) {
147  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
148  }
149 #endif
150 #else
151  (void)argc;
152  (void)argv;
153  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
154 #endif
155 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:211
static const vpColor none
Definition: vpColor.h:223
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:1213
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:2003
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:2086
static double rad(double deg)
Definition: vpMath.h:127
Make the complete tracking of an object by using its CAD model.
virtual void setNearClippingDistance(const double &dist) vp_override
virtual void setOgreVisibilityTest(const bool &v) vp_override
virtual void setFarClippingDistance(const double &dist) vp_override
virtual void loadConfigFile(const std::string &configFile, bool verbose=true) vp_override
virtual void track(const vpImage< unsigned char > &I) vp_override
virtual void setClipping(const unsigned int &flags) vp_override
virtual void setCameraParameters(const vpCameraParameters &cam) vp_override
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false) vp_override
void setMovingEdge(const vpMe &me)
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:650
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:248
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:518
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:414
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:481
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:470
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:256
Definition: vpMe.h:124
void setMu1(const double &mu_1)
Definition: vpMe.h:399
void setRange(const unsigned int &range)
Definition: vpMe.h:429
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:519
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:488
void setThreshold(const double &threshold)
Definition: vpMe.h:480
void setSampleStep(const double &sample_step)
Definition: vpMe.h:436
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:496
void setMu2(const double &mu_2)
Definition: vpMe.h:406
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:135
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.