Visual Servoing Platform  version 3.0.0
tutorial-mb-edge-tracker.cpp
1 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/io/vpImageIo.h>
6 #include <visp3/core/vpIoTools.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) || defined(VISP_HAVE_FFMPEG)
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
34  << ".[init,"
35 #ifdef VISP_HAVE_XML2
36  << "xml,"
37 #endif
38  << "cao or wrl]" << std::endl;
39  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
40 
48 
49  vpVideoReader g;
50  g.setFileName(videoname);
51  g.open(I);
52 
53 #if defined(VISP_HAVE_X11)
54  vpDisplayX display;
55 #elif defined(VISP_HAVE_GDI)
56  vpDisplayGDI display;
57 #elif defined(VISP_HAVE_OPENCV)
58  vpDisplayOpenCV display;
59 #else
60  std::cout << "No image viewer is available..." << std::endl;
61  return 0;
62 #endif
63 
64  display.init(I, 100, 100,"Model-based edge tracker");
65 
67  vpMbEdgeTracker tracker;
69  bool usexml = false;
71 #ifdef VISP_HAVE_XML2
72  if(vpIoTools::checkFilename(objectname + ".xml")) {
73  tracker.loadConfigFile(objectname + ".xml");
74  usexml = true;
75  }
76 #endif
77  if (! usexml) {
80  vpMe me;
81  me.setMaskSize(5);
82  me.setMaskNumber(180);
83  me.setRange(8);
84  me.setThreshold(10000);
85  me.setMu1(0.5);
86  me.setMu2(0.5);
87  me.setSampleStep(4);
88  tracker.setMovingEdge(me);
89  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
90  tracker.setCameraParameters(cam);
92  tracker.setAngleAppear( vpMath::rad(70) );
93  tracker.setAngleDisappear( vpMath::rad(80) );
96  tracker.setNearClippingDistance(0.1);
97  tracker.setFarClippingDistance(100.0);
103  }
105  tracker.setOgreVisibilityTest(false);
106  tracker.setOgreShowConfigDialog(false);
109  if(vpIoTools::checkFilename(objectname + ".cao"))
110  tracker.loadModel(objectname + ".cao");
113  else if(vpIoTools::checkFilename(objectname + ".wrl"))
114  tracker.loadModel(objectname + ".wrl");
117  tracker.setDisplayFeatures(true);
120  tracker.initClick(I, objectname + ".init", true);
122 
123  while(! g.end()){
124  g.acquire(I);
127  tracker.track(I);
130  tracker.getPose(cMo);
133  tracker.getCameraParameters(cam);
134  tracker.display(I, cMo, cam, vpColor::red, 2);
136  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
137  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
138  vpDisplay::flush(I);
139  if (vpDisplay::getClick(I, false))
140  break;
141  }
144 #ifdef VISP_HAVE_XML2
146 #endif
147 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 3)
148  SoDB::finish();
149 #endif
150  }
152  catch(vpException e) {
153  std::cout << "Catch a ViSP exception: " << e << std::endl;
154  }
155 #ifdef VISP_HAVE_OGRE
156  catch(Ogre::Exception e) {
157  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
158  }
159 #endif
160 #else
161  (void)argc;
162  (void)argv;
163  std::cout << "Install OpenCV or ffmpeg and rebuild ViSP to use this example." << std::endl;
164 #endif
165 }
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setMovingEdge(const vpMe &me)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:232
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:430
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:460
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:260
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
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:175
error that can be emited by ViSP classes.
Definition: vpException.h:73
Definition: vpMe.h:59
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:1240
void loadConfigFile(const std::string &configFile)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
void setMu1(const double &mu_1)
Definition: vpMe.h:160
static const vpColor red
Definition: vpColor.h:163
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)
void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:523
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:485
void open(vpImage< vpRGBa > &I)
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:468
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:356
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:225
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:419
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false)
static double rad(double deg)
Definition: vpMath.h:104
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, vpImagePoint offset=vpImagePoint(0, 0))
Definition: vpDisplay.cpp:373
static void cleanup()
Definition: vpXmlParser.h:216
void setMu2(const double &mu_2)
Definition: vpMe.h:174
virtual void loadModel(const char *modelFile, const bool verbose=false)
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
void setThreshold(const double &t)
Definition: vpMe.h:288
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)
virtual bool getClick(bool blocking=true)=0
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:459
void setRange(const unsigned int &r)
Definition: vpMe.h:218
virtual void setClipping(const unsigned int &flags)