Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
tutorial-detection-object-mbt.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpIoTools.h>
4 #include <visp3/gui/vpDisplayFactory.h>
5 #include <visp3/io/vpVideoReader.h>
6 #include <visp3/mbt/vpMbGenericTracker.h>
7 #include <visp3/vision/vpKeyPoint.h>
8 
9 int main(int argc, char **argv)
10 {
11 #if defined(HAVE_OPENCV_IMGPROC) && defined(VISP_HAVE_DISPLAY) && \
12  ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
13  ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES))
14 
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
19 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
20  std::shared_ptr<vpDisplay> display;
21 #else
22  vpDisplay *display = nullptr;
23 #endif
24  try {
25  std::string videoname = "teabox.mp4";
26 
27  for (int i = 1; i < argc; i++) {
28  if (std::string(argv[i]) == "--name" && i + 1 < argc) {
29  videoname = std::string(argv[++i]);
30  }
31  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
32  std::cout << "\nUsage: " << argv[0]
33  << " [--name <video name>]"
34  << " [--help] [-h]\n" << std::endl;
35  return EXIT_SUCCESS;
36  }
37  }
38  std::string parentname = vpIoTools::getParent(videoname);
39  std::string objectname = vpIoTools::getNameWE(videoname);
40 
41  if (!parentname.empty())
42  objectname = parentname + "/" + objectname;
43 
44  std::cout << "Video name: " << videoname << std::endl;
45  std::cout << "Tracker requested config files: " << objectname << ".[init,"
46  << "xml,"
47  << "cao or wrl]" << std::endl;
48  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
49 
53 
54  vpVideoReader g;
55  g.setFileName(videoname);
56  g.open(I);
57 
58 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
59  display = vpDisplayFactory::createDisplay(I, 100, 100, "Model-based edge tracker");
60 #else
61  display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Model-based edge tracker");
62 #endif
63 
65  bool usexml = false;
66 #if defined(VISP_HAVE_PUGIXML)
67  if (vpIoTools::checkFilename(objectname + ".xml")) {
68  tracker.loadConfigFile(objectname + ".xml");
69  tracker.getCameraParameters(cam);
70  usexml = true;
71  }
72 #endif
73  if (!usexml) {
74  vpMe me;
75  me.setMaskSize(5);
76  me.setMaskNumber(180);
77  me.setRange(8);
79  me.setThreshold(20);
80  me.setMu1(0.5);
81  me.setMu2(0.5);
82  me.setSampleStep(4);
83  me.setNbTotalSample(250);
84  tracker.setMovingEdge(me);
85  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
86  tracker.setCameraParameters(cam);
87  tracker.setAngleAppear(vpMath::rad(70));
88  tracker.setAngleDisappear(vpMath::rad(80));
89  tracker.setNearClippingDistance(0.1);
90  tracker.setFarClippingDistance(100.0);
91  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
92  }
93 
94  tracker.setOgreVisibilityTest(false);
95  if (vpIoTools::checkFilename(objectname + ".cao"))
96  tracker.loadModel(objectname + ".cao");
97  else if (vpIoTools::checkFilename(objectname + ".wrl"))
98  tracker.loadModel(objectname + ".wrl");
99  tracker.setDisplayFeatures(true);
100  tracker.initClick(I, objectname + ".init", true);
101  tracker.track(I);
103 
105 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
106  std::string detectorName = "SIFT";
107  std::string extractorName = "SIFT";
108  std::string matcherName = "BruteForce";
109  std::string configurationFile = "detection-config-SIFT.xml";
110 #elif ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
111  std::string detectorName = "FAST";
112  std::string extractorName = "ORB";
113  std::string matcherName = "BruteForce-Hamming";
114  std::string configurationFile = "detection-config.xml";
115 #endif
117 
119  vpKeyPoint keypoint_learning;
121  if (usexml) {
123  keypoint_learning.loadConfigFile(configurationFile);
125  }
126  else {
128  keypoint_learning.setDetector(detectorName);
129  keypoint_learning.setExtractor(extractorName);
130  keypoint_learning.setMatcher(matcherName);
132  }
133 
135  std::vector<cv::KeyPoint> trainKeyPoints;
136  double elapsedTime;
137  keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
139 
141  std::vector<vpPolygon> polygons;
142  std::vector<std::vector<vpPoint> > roisPt;
143  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
144  polygons = pair.first;
145  roisPt = pair.second;
146 
147  std::vector<cv::Point3f> points3f;
148  tracker.getPose(cMo);
149  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
151 
153  keypoint_learning.buildReference(I, trainKeyPoints, points3f);
155 
157  keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
159 
162  for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
163  vpDisplay::displayCross(I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
164  }
165  vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
166  vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
167  vpDisplay::flush(I);
168  vpDisplay::getClick(I, true);
170 
172  vpKeyPoint keypoint_detection;
173  if (usexml) {
174  keypoint_detection.loadConfigFile(configurationFile);
175  }
176  else {
177  keypoint_detection.setDetector(detectorName);
178  keypoint_detection.setExtractor(extractorName);
179  keypoint_detection.setMatcher(matcherName);
181  keypoint_detection.setMatchingRatioThreshold(0.8);
182  keypoint_detection.setUseRansacVVS(true);
183  keypoint_detection.setUseRansacConsensusPercentage(true);
184  keypoint_detection.setRansacConsensusPercentage(20.0);
185  keypoint_detection.setRansacIteration(200);
186  keypoint_detection.setRansacThreshold(0.005);
187  }
189 
191  keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
193 
194  double error;
195  bool click_done = false;
196 
197  while (!g.end()) {
198  g.acquire(I);
200 
201  vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
202 
204  if (keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
206 
208  tracker.setPose(I, cMo);
211  tracker.display(I, cMo, cam, vpColor::red, 2);
212  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
214  }
215 
216  vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
217  vpDisplay::flush(I);
218  if (vpDisplay::getClick(I, false)) {
219  click_done = true;
220  break;
221  }
222  }
223  if (!click_done)
225  }
226  catch (const vpException &e) {
227  std::cout << "Catch an exception: " << e << std::endl;
228  }
229 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
230  if (display != nullptr) {
231  delete display;
232  }
233 #endif
234 #else
235  (void)argc;
236  (void)argv;
237  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
238 #endif
239 
240  return EXIT_SUCCESS;
241 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:198
static const vpColor none
Definition: vpColor.h:210
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
Class that allows keypoints 2D features detection (and descriptors extraction) and matching thanks to...
Definition: vpKeyPoint.h:267
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(double percentage)
Definition: vpKeyPoint.h:1882
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:1815
void setUseRansacVVS(bool ransacVVS)
Definition: vpKeyPoint.h:2034
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:1711
void loadLearningData(const std::string &filename, bool binaryMode=false, bool append=false)
void setRansacThreshold(double threshold)
Definition: vpKeyPoint.h:1969
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:989
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=nullptr)
Definition: vpKeyPoint.cpp:481
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:1787
void saveLearningData(const std::string &filename, bool binaryMode=false, bool saveTrainingImages=true)
void setUseRansacConsensusPercentage(bool usePercentage)
Definition: vpKeyPoint.h:2025
void setMatchingRatioThreshold(double ratio)
Definition: vpKeyPoint.h:1866
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:276
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1653
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:211
void loadConfigFile(const std::string &configFile)
void setRansacIteration(int nbIter)
Definition: vpKeyPoint.h:1904
static double rad(double deg)
Definition: vpMath.h:129
Real-time 6D object pose tracking using its CAD model.
Definition: vpMe.h:134
void setMu1(const double &mu_1)
Definition: vpMe.h:385
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setNbTotalSample(const int &ntotal_sample)
Definition: vpMe.h:399
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:552
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:560
void setMu2(const double &mu_2)
Definition: vpMe.h:392
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.