Visual Servoing Platform  version 3.0.0
tutorial-detection-object-mbt.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayX.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/mbt/vpMbEdgeTracker.h>
7 #include <visp3/io/vpVideoReader.h>
8 #include <visp3/vision/vpKeyPoint.h>
9 #include <visp3/core/vpIoTools.h>
10 
11 
12 int main(int argc, char ** argv) {
13 #if defined(VISP_HAVE_OPENCV) && ((VISP_HAVE_OPENCV_VERSION >= 0x020400) || defined(VISP_HAVE_FFMPEG))
14  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 
44 
45  vpVideoReader g;
46  g.setFileName(videoname);
47  g.open(I);
48 
49 #if defined(VISP_HAVE_X11)
50  vpDisplayX display;
51 #elif defined(VISP_HAVE_GDI)
52  vpDisplayGDI display;
53 #elif defined(VISP_HAVE_OPENCV)
54  vpDisplayOpenCV display;
55 #else
56  std::cout << "No image viewer is available..." << std::endl;
57  return 0;
58 #endif
59 
60  display.init(I, 100, 100,"Model-based edge tracker");
61 
62  vpMbEdgeTracker tracker;
63  bool usexml = false;
64 #ifdef VISP_HAVE_XML2
65  if(vpIoTools::checkFilename(objectname + ".xml")) {
66  tracker.loadConfigFile(objectname + ".xml");
67  tracker.getCameraParameters(cam);
68  usexml = true;
69  }
70 #endif
71  if (! usexml) {
72  vpMe me;
73  me.setMaskSize(5);
74  me.setMaskNumber(180);
75  me.setRange(8);
76  me.setThreshold(10000);
77  me.setMu1(0.5);
78  me.setMu2(0.5);
79  me.setSampleStep(4);
80  me.setNbTotalSample(250);
81  tracker.setMovingEdge(me);
82  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
83  tracker.setCameraParameters(cam);
84  tracker.setAngleAppear( vpMath::rad(70) );
85  tracker.setAngleDisappear( vpMath::rad(80) );
86  tracker.setNearClippingDistance(0.1);
87  tracker.setFarClippingDistance(100.0);
89  }
90 
91  tracker.setOgreVisibilityTest(false);
92  if(vpIoTools::checkFilename(objectname + ".cao"))
93  tracker.loadModel(objectname + ".cao");
94  else if(vpIoTools::checkFilename(objectname + ".wrl"))
95  tracker.loadModel(objectname + ".wrl");
96  tracker.setDisplayFeatures(true);
97  tracker.initClick(I, objectname + ".init", true);
98  tracker.track(I);
100 
102  std::string detectorName = "FAST";
103  std::string extractorName = "ORB";
104  std::string matcherName = "BruteForce-Hamming";
105  std::string configurationFile = "detection-config.xml";
106 
107 #if (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D))
108  detectorName = "SIFT";
109  extractorName = "SIFT";
110  matcherName = "BruteForce";
111  configurationFile = "detection-config-SIFT.xml";
112 #endif
113 
116  vpKeyPoint keypoint_learning;
118  if(usexml) {
120 #ifdef VISP_HAVE_XML2
121  keypoint_learning.loadConfigFile(configurationFile);
122 #endif
123  } else {
126  keypoint_learning.setDetector(detectorName);
127  keypoint_learning.setExtractor(extractorName);
128  keypoint_learning.setMatcher(matcherName);
130  }
131 
133  std::vector<cv::KeyPoint> trainKeyPoints;
134  double elapsedTime;
135  keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
137 
139  std::vector<vpPolygon> polygons;
140  std::vector<std::vector<vpPoint> > roisPt;
141  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
142  polygons = pair.first;
143  roisPt = pair.second;
144 
145  std::vector<cv::Point3f> points3f;
146  tracker.getPose(cMo);
147  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
149 
151  keypoint_learning.buildReference(I, trainKeyPoints, points3f);
153 
155  keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
157 
160  for(std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
161  vpDisplay::displayCross(I, (int) it->pt.y, (int) it->pt.x, 4, vpColor::red);
162  }
163  vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
164  vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
165  vpDisplay::flush(I);
166  vpDisplay::getClick(I, true);
168 
170  vpKeyPoint keypoint_detection;
171  if(usexml) {
172 #ifdef VISP_HAVE_XML2
173  keypoint_detection.loadConfigFile(configurationFile);
174 #endif
175  } else {
176  keypoint_detection.setDetector(detectorName);
177  keypoint_detection.setExtractor(extractorName);
178  keypoint_detection.setMatcher(matcherName);
180  keypoint_detection.setMatchingRatioThreshold(0.8);
181  keypoint_detection.setUseRansacVVS(true);
182  keypoint_detection.setUseRansacConsensusPercentage(true);
183  keypoint_detection.setRansacConsensusPercentage(20.0);
184  keypoint_detection.setRansacIteration(200);
185  keypoint_detection.setRansacThreshold(0.005);
186  }
188 
190  keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
192 
193  double error;
194  bool click_done = false;
195 
196  while(! g.end()) {
197  g.acquire(I);
199 
200  vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
201 
203  if(keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
205 
207  tracker.setPose(I, cMo);
210  tracker.display(I, cMo, cam, vpColor::red, 2);
211  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
213  }
214 
215  vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
216  vpDisplay::flush(I);
217  if (vpDisplay::getClick(I, false)) {
218  click_done = true;
219  break;
220  }
221  }
222  if (! click_done)
224 #ifdef VISP_HAVE_XML2
226 #endif
227 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 3)
228  SoDB::finish();
229 #endif
230  }
231  catch(vpException &e) {
232  std::cout << "Catch an exception: " << e << std::endl;
233  }
234 #else
235  (void)argc;
236  (void)argv;
237  std::cout << "Install OpenCV or ffmpeg and rebuild ViSP to use this example." << std::endl;
238 #endif
239 
240  return 0;
241 }
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setRansacIteration(const int nbIter)
Definition: vpKeyPoint.h:748
void setMovingEdge(const vpMe &me)
void setUseRansacVVS(const bool ransacVVS)
Definition: vpKeyPoint.h:845
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 detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, double &elapsedTime, const vpRect &rectangle=vpRect())
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:460
void setRansacThreshold(const double threshold)
Definition: vpKeyPoint.h:787
void setExtractor(const std::string &extractorName)
Definition: vpKeyPoint.h:603
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
void setNbTotalSample(const int &nb)
Definition: vpMe.h:188
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 setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
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)
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:485
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:661
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 compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidate, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=NULL)
Definition: vpKeyPoint.cpp:615
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:225
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:340
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
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(const double percentage)
Definition: vpKeyPoint.h:735
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
void loadLearningData(const std::string &filename, const bool binaryMode=false, const bool append=false)
virtual void loadModel(const char *modelFile, const bool verbose=false)
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:213
void saveLearningData(const std::string &filename, const bool binaryMode=false, const bool saveTrainingImages=true)
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)
void setDetector(const std::string &detectorName)
Definition: vpKeyPoint.h:563
void setUseRansacConsensusPercentage(const bool usePercentage)
Definition: vpKeyPoint.h:836
virtual bool getClick(bool blocking=true)=0
void loadConfigFile(const std::string &configFile)
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:459
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:677
void setRange(const unsigned int &r)
Definition: vpMe.h:218
virtual void setClipping(const unsigned int &flags)
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(const bool orderPolygons=true, const bool useVisibility=true)
void setMatchingRatioThreshold(const double ratio)
Definition: vpKeyPoint.h:722