Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D)
14 #ifdef ENABLE_VISP_NAMESPACE
15  using namespace VISP_NAMESPACE_NAME;
16 #endif
18  try {
19  std::string videoname = "teabox.mp4";
20 
21  for (int i = 0; i < argc; i++) {
22  if (std::string(argv[i]) == "--name")
23  videoname = std::string(argv[i + 1]);
24  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
25  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
26  return EXIT_SUCCESS;
27  }
28  }
29  std::string parentname = vpIoTools::getParent(videoname);
30  std::string objectname = vpIoTools::getNameWE(videoname);
31 
32  if (!parentname.empty())
33  objectname = parentname + "/" + objectname;
34 
35  std::cout << "Video name: " << videoname << std::endl;
36  std::cout << "Tracker requested config files: " << objectname << ".[init,"
37  << "xml,"
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(HAVE_OPENCV_HIGHGUI)
54  vpDisplayOpenCV display;
55 #else
56  std::cout << "No image viewer is available..." << std::endl;
57  return EXIT_FAILURE;
58 #endif
59 
60  display.init(I, 100, 100, "Model-based edge tracker");
61 
63  bool usexml = false;
64 #if defined(VISP_HAVE_PUGIXML)
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);
77  me.setThreshold(20);
78  me.setMu1(0.5);
79  me.setMu2(0.5);
80  me.setSampleStep(4);
81  me.setNbTotalSample(250);
82  tracker.setMovingEdge(me);
83  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
84  tracker.setCameraParameters(cam);
85  tracker.setAngleAppear(vpMath::rad(70));
86  tracker.setAngleDisappear(vpMath::rad(80));
87  tracker.setNearClippingDistance(0.1);
88  tracker.setFarClippingDistance(100.0);
89  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
90  }
91 
92  tracker.setOgreVisibilityTest(false);
93  if (vpIoTools::checkFilename(objectname + ".cao"))
94  tracker.loadModel(objectname + ".cao");
95  else if (vpIoTools::checkFilename(objectname + ".wrl"))
96  tracker.loadModel(objectname + ".wrl");
97  tracker.setDisplayFeatures(true);
98  tracker.initClick(I, objectname + ".init", true);
99  tracker.track(I);
101 
103 #if (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)) || \
104  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
105  std::string detectorName = "SIFT";
106  std::string extractorName = "SIFT";
107  std::string matcherName = "BruteForce";
108  std::string configurationFile = "detection-config-SIFT.xml";
109 #else
110  std::string detectorName = "FAST";
111  std::string extractorName = "ORB";
112  std::string matcherName = "BruteForce-Hamming";
113  std::string configurationFile = "detection-config.xml";
114 #endif
116 
118  vpKeyPoint keypoint_learning;
120  if (usexml) {
122  keypoint_learning.loadConfigFile(configurationFile);
124  }
125  else {
127  keypoint_learning.setDetector(detectorName);
128  keypoint_learning.setExtractor(extractorName);
129  keypoint_learning.setMatcher(matcherName);
131  }
132 
134  std::vector<cv::KeyPoint> trainKeyPoints;
135  double elapsedTime;
136  keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
138 
140  std::vector<vpPolygon> polygons;
141  std::vector<std::vector<vpPoint> > roisPt;
142  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
143  polygons = pair.first;
144  roisPt = pair.second;
145 
146  std::vector<cv::Point3f> points3f;
147  tracker.getPose(cMo);
148  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
150 
152  keypoint_learning.buildReference(I, trainKeyPoints, points3f);
154 
156  keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
158 
161  for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
162  vpDisplay::displayCross(I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
163  }
164  vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
165  vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
166  vpDisplay::flush(I);
167  vpDisplay::getClick(I, true);
169 
171  vpKeyPoint keypoint_detection;
172  if (usexml) {
173  keypoint_detection.loadConfigFile(configurationFile);
174  }
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  }
225  catch (const vpException &e) {
226  std::cout << "Catch an exception: " << e << std::endl;
227  }
228 #else
229  (void)argc;
230  (void)argv;
231  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
232 #endif
233 
234  return EXIT_SUCCESS;
235 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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 detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:221
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(double percentage)
Definition: vpKeyPoint.h:1804
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:1737
void setUseRansacVVS(bool ransacVVS)
Definition: vpKeyPoint.h:1956
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:1633
void loadLearningData(const std::string &filename, bool binaryMode=false, bool append=false)
void setRansacThreshold(double threshold)
Definition: vpKeyPoint.h:1891
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:975
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:465
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:1709
void saveLearningData(const std::string &filename, bool binaryMode=false, bool saveTrainingImages=true)
void setUseRansacConsensusPercentage(bool usePercentage)
Definition: vpKeyPoint.h:1947
void setMatchingRatioThreshold(double ratio)
Definition: vpKeyPoint.h:1788
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:230
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1575
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:194
void loadConfigFile(const std::string &configFile)
void setRansacIteration(int nbIter)
Definition: vpKeyPoint.h:1826
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)