Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 }
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:411
void setRansacIteration(const int nbIter)
Definition: vpKeyPoint.h:909
void setMovingEdge(const vpMe &me)
void setUseRansacVVS(const bool ransacVVS)
Definition: vpKeyPoint.h:1006
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:208
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:382
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:488
void setRansacThreshold(const double threshold)
Definition: vpKeyPoint.h:948
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:263
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
void setNbTotalSample(const int &nb)
Definition: vpMe.h:240
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
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
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
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:1306
void loadConfigFile(const std::string &configFile)
static void flush(const vpImage< unsigned char > &I)
void setMu1(const double &mu_1)
Definition: vpMe.h:226
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:508
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:822
void open(vpImage< vpRGBa > &I)
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidates, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=NULL)
Definition: vpKeyPoint.cpp:765
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:496
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:201
Generic class defining intrinsic camera parameters.
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:700
void acquire(vpImage< vpRGBa > &I)
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:490
virtual void setFarClippingDistance(const double &dist)
void setFileName(const char *filename)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:371
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:896
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))
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void cleanup()
Definition: vpXmlParser.h:308
void setMu2(const double &mu_2)
Definition: vpMe.h:233
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:217
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:1293
virtual void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:333
void setThreshold(const double &t)
Definition: vpMe.h:284
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 setUseRansacConsensusPercentage(const bool usePercentage)
Definition: vpKeyPoint.h:997
void loadConfigFile(const std::string &configFile)
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:838
void setRange(const unsigned int &r)
Definition: vpMe.h:256
virtual void setClipping(const unsigned int &flags)
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:752
void setMatchingRatioThreshold(const double ratio)
Definition: vpKeyPoint.h:883
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(const bool orderPolygons=true, const bool useVisibility=true, const bool clipPolygon=false)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())