Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-mb-edge-tracker.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpIoTools.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpImageIo.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)
16 #ifdef ENABLE_VISP_NAMESPACE
17  using namespace VISP_NAMESPACE_NAME;
18 #endif
19 
20  try {
21  std::string videoname = "teabox.mp4";
22 
23  for (int i = 0; i < argc; i++) {
24  if (std::string(argv[i]) == "--name")
25  videoname = std::string(argv[i + 1]);
26  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
27  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
28  return EXIT_SUCCESS;
29  }
30  }
31  std::string parentname = vpIoTools::getParent(videoname);
32  std::string objectname = vpIoTools::getNameWE(videoname);
33 
34  if (!parentname.empty())
35  objectname = parentname + "/" + objectname;
36 
37  std::cout << "Video name: " << videoname << std::endl;
38  std::cout << "Tracker requested config files: " << objectname << ".[init,"
39  << "xml,"
40  << "cao or wrl]" << std::endl;
41  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
42 
50 
51  vpVideoReader g;
52  g.setFileName(videoname);
53  g.open(I);
54 
55 #if defined(VISP_HAVE_X11)
56  vpDisplayX display;
57 #elif defined(VISP_HAVE_GDI)
58  vpDisplayGDI display;
59 #elif defined(HAVE_OPENCV_HIGHGUI)
60  vpDisplayOpenCV display;
61 #else
62  std::cout << "No image viewer is available..." << std::endl;
63  return EXIT_FAILURE;
64 #endif
65 
66  display.init(I, 100, 100, "Model-based edge tracker");
67 
69  vpMbEdgeTracker tracker;
71  bool usexml = false;
73 #if defined(VISP_HAVE_PUGIXML)
74  if (vpIoTools::checkFilename(objectname + ".xml")) {
75  tracker.loadConfigFile(objectname + ".xml");
76  usexml = true;
77  }
78 #endif
80  if (!usexml) {
82  vpMe me;
83  me.setMaskSize(5);
84  me.setMaskNumber(180);
85  me.setRange(8);
87  me.setThreshold(20);
88  me.setMu1(0.5);
89  me.setMu2(0.5);
90  me.setSampleStep(4);
91  tracker.setMovingEdge(me);
92  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
93  tracker.setCameraParameters(cam);
95  tracker.setAngleAppear(vpMath::rad(70));
96  tracker.setAngleDisappear(vpMath::rad(80));
99  tracker.setNearClippingDistance(0.1);
100  tracker.setFarClippingDistance(100.0);
106  }
108  tracker.setOgreVisibilityTest(false);
109  tracker.setOgreShowConfigDialog(false);
112  if (vpIoTools::checkFilename(objectname + ".cao"))
113  tracker.loadModel(objectname + ".cao");
116  else if (vpIoTools::checkFilename(objectname + ".wrl"))
117  tracker.loadModel(objectname + ".wrl");
120  tracker.setDisplayFeatures(true);
123  tracker.initClick(I, objectname + ".init", true);
125 
126  while (!g.end()) {
127  g.acquire(I);
130  tracker.track(I);
133  tracker.getPose(cMo);
136  tracker.getCameraParameters(cam);
137  tracker.display(I, cMo, cam, vpColor::red, 2);
139  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
140  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
141  vpDisplay::flush(I);
142  if (vpDisplay::getClick(I, false))
143  break;
144  }
146  }
147  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 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
static double rad(double deg)
Definition: vpMath.h:129
Make the complete tracking of an object by using its CAD model.
virtual void setNearClippingDistance(const double &dist) VP_OVERRIDE
virtual void track(const vpImage< unsigned char > &I) 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
virtual void setFarClippingDistance(const double &dist) VP_OVERRIDE
virtual void setOgreVisibilityTest(const bool &v) VP_OVERRIDE
virtual void setClipping(const unsigned int &flags) VP_OVERRIDE
void setMovingEdge(const vpMe &me)
virtual void setCameraParameters(const vpCameraParameters &cam) VP_OVERRIDE
virtual void loadConfigFile(const std::string &configFile, bool verbose=true) VP_OVERRIDE
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:652
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:250
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:520
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:416
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:483
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:472
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:258
Definition: vpMe.h:134
void setMu1(const double &mu_1)
Definition: vpMe.h:385
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:552
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:560
void setMu2(const double &mu_2)
Definition: vpMe.h:392
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
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)