Visual Servoing Platform  version 3.1.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.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_XML2
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_XML2
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 #ifdef VISP_HAVE_XML2
145 #endif
146 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 3)
147  SoDB::finish();
148 #endif
149  } catch (vpException &e) {
151  std::cout << "Catch a ViSP exception: " << e << std::endl;
152  }
153 #ifdef VISP_HAVE_OGRE
154  catch (Ogre::Exception &e) {
155  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
156  }
157 #endif
158 #else
159  (void)argc;
160  (void)argv;
161  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
162 #endif
163 }
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:223
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:470
virtual void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:575
void setMovingEdge(const vpMe &me)
virtual void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:381
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:433
Implementation of an homogeneous matrix and operations on such kind of matrices.
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:129
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
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.
virtual void setCameraParameters(const vpCameraParameters &camera)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1371
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:180
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
virtual void setNearClippingDistance(const double &dist)
virtual void setOgreVisibilityTest(const bool &v)
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:573
void open(vpImage< vpRGBa > &I)
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)
void setFileName(const char *filename)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:422
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false)
static double rad(double deg)
Definition: vpMath.h:102
static void cleanup()
Definition: vpXmlParser.h:310
void setMu2(const double &mu_2)
Definition: vpMe.h:248
virtual void loadModel(const char *modelFile, const bool verbose=false)
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:215
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:1358
void setThreshold(const double &t)
Definition: vpMe.h:300
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)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
virtual void setClipping(const unsigned int &flags)