Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
tutorial-detection-object-mbt.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/vpVideoReader.h>
8 #include <visp3/mbt/vpMbGenericTracker.h>
9 #include <visp3/vision/vpKeyPoint.h>
10 
11 int main(int argc, char **argv)
12 {
13 #if (VISP_HAVE_OPENCV_VERSION >= 0x020400)
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 << ".[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 
43 
44  vpVideoReader g;
45  g.setFileName(videoname);
46  g.open(I);
47 
48 #if defined(VISP_HAVE_X11)
49  vpDisplayX display;
50 #elif defined(VISP_HAVE_GDI)
51  vpDisplayGDI display;
52 #elif defined(VISP_HAVE_OPENCV)
53  vpDisplayOpenCV display;
54 #else
55  std::cout << "No image viewer is available..." << std::endl;
56  return 0;
57 #endif
58 
59  display.init(I, 100, 100, "Model-based edge tracker");
60 
62  bool usexml = false;
63 #ifdef VISP_HAVE_XML2
64  if (vpIoTools::checkFilename(objectname + ".xml")) {
65  tracker.loadConfigFile(objectname + ".xml");
66  tracker.getCameraParameters(cam);
67  usexml = true;
68  }
69 #endif
70  if (!usexml) {
71  vpMe me;
72  me.setMaskSize(5);
73  me.setMaskNumber(180);
74  me.setRange(8);
75  me.setThreshold(10000);
76  me.setMu1(0.5);
77  me.setMu2(0.5);
78  me.setSampleStep(4);
79  me.setNbTotalSample(250);
80  tracker.setMovingEdge(me);
81  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
82  tracker.setCameraParameters(cam);
83  tracker.setAngleAppear(vpMath::rad(70));
84  tracker.setAngleDisappear(vpMath::rad(80));
85  tracker.setNearClippingDistance(0.1);
86  tracker.setFarClippingDistance(100.0);
87  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
88  }
89 
90  tracker.setOgreVisibilityTest(false);
91  if (vpIoTools::checkFilename(objectname + ".cao"))
92  tracker.loadModel(objectname + ".cao");
93  else if (vpIoTools::checkFilename(objectname + ".wrl"))
94  tracker.loadModel(objectname + ".wrl");
95  tracker.setDisplayFeatures(true);
96  tracker.initClick(I, objectname + ".init", true);
97  tracker.track(I);
99 
101 #if (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D))
102  std::string detectorName = "SIFT";
103  std::string extractorName = "SIFT";
104  std::string matcherName = "BruteForce";
105  std::string configurationFile = "detection-config-SIFT.xml";
106 #else
107  std::string detectorName = "FAST";
108  std::string extractorName = "ORB";
109  std::string matcherName = "BruteForce-Hamming";
110  std::string configurationFile = "detection-config.xml";
111 #endif
112 
115  vpKeyPoint keypoint_learning;
117  if (usexml) {
119 #ifdef VISP_HAVE_XML2
120  keypoint_learning.loadConfigFile(configurationFile);
121 #endif
122  } else {
125  keypoint_learning.setDetector(detectorName);
126  keypoint_learning.setExtractor(extractorName);
127  keypoint_learning.setMatcher(matcherName);
129  }
130 
132  std::vector<cv::KeyPoint> trainKeyPoints;
133  double elapsedTime;
134  keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
136 
138  std::vector<vpPolygon> polygons;
139  std::vector<std::vector<vpPoint> > roisPt;
140  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
141  polygons = pair.first;
142  roisPt = pair.second;
143 
144  std::vector<cv::Point3f> points3f;
145  tracker.getPose(cMo);
146  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
148 
150  keypoint_learning.buildReference(I, trainKeyPoints, points3f);
152 
154  keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
156 
159  for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
160  vpDisplay::displayCross(I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
161  }
162  vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
163  vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
164  vpDisplay::flush(I);
165  vpDisplay::getClick(I, true);
167 
169  vpKeyPoint keypoint_detection;
170  if (usexml) {
171 #ifdef VISP_HAVE_XML2
172  keypoint_detection.loadConfigFile(configurationFile);
173 #endif
174  } else {
175  keypoint_detection.setDetector(detectorName);
176  keypoint_detection.setExtractor(extractorName);
177  keypoint_detection.setMatcher(matcherName);
179  keypoint_detection.setMatchingRatioThreshold(0.8);
180  keypoint_detection.setUseRansacVVS(true);
181  keypoint_detection.setUseRansacConsensusPercentage(true);
182  keypoint_detection.setRansacConsensusPercentage(20.0);
183  keypoint_detection.setRansacIteration(200);
184  keypoint_detection.setRansacThreshold(0.005);
185  }
187 
189  keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
191 
192  double error;
193  bool click_done = false;
194 
195  while (!g.end()) {
196  g.acquire(I);
198 
199  vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
200 
202  if (keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
204 
206  tracker.setPose(I, cMo);
209  tracker.display(I, cMo, cam, vpColor::red, 2);
210  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
212  }
213 
214  vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
215  vpDisplay::flush(I);
216  if (vpDisplay::getClick(I, false)) {
217  click_done = true;
218  break;
219  }
220  }
221  if (!click_done)
223 #ifdef VISP_HAVE_XML2
225 #endif
226 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION >= 2)
227  SoDB::finish();
228 #endif
229  } catch (const vpException &e) {
230  std::cout << "Catch an exception: " << e << std::endl;
231  }
232 #else
233  (void)argc;
234  (void)argv;
235  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
236 #endif
237 
238  return 0;
239 }
void setRansacIteration(const int nbIter)
Definition: vpKeyPoint.h:977
void setUseRansacVVS(const bool ransacVVS)
Definition: vpKeyPoint.h:1111
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:454
void setRansacThreshold(const double threshold)
Definition: vpKeyPoint.h:1045
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)
void setNbTotalSample(const int &nb)
Definition: vpMe.h:255
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
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1477
Real-time 6D object pose tracking using its CAD model.
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)
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:675
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:868
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:528
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 setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:734
void acquire(vpImage< vpRGBa > &I)
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:236
void setFileName(const char *filename)
static double rad(double deg)
Definition: vpMath.h:102
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(const double percentage)
Definition: vpKeyPoint.h:953
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:310
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void loadLearningData(const std::string &filename, const bool binaryMode=false, const bool append=false)
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))
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:228
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:1464
void setThreshold(const double &t)
Definition: vpMe.h:300
void setUseRansacConsensusPercentage(const bool usePercentage)
Definition: vpKeyPoint.h:1102
void loadConfigFile(const std::string &configFile)
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:889
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:792
void setMatchingRatioThreshold(const double ratio)
Definition: vpKeyPoint.h:938
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())