Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
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)
15  try {
16  std::string videoname = "teabox.mp4";
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" || std::string(argv[i]) == "-h") {
22  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
23  return EXIT_SUCCESS;
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  << "xml,"
35  << "cao or wrl]" << std::endl;
36  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
37 
41 
42  vpVideoReader g;
43  g.setFileName(videoname);
44  g.open(I);
45 
46 #if defined(VISP_HAVE_X11)
48 #elif defined(VISP_HAVE_GDI)
50 #elif defined(HAVE_OPENCV_HIGHGUI)
52 #else
53  std::cout << "No image viewer is available..." << std::endl;
54  return EXIT_FAILURE;
55 #endif
56 
57  display.init(I, 100, 100, "Model-based edge tracker");
58 
60  bool usexml = false;
61 #if defined(VISP_HAVE_PUGIXML)
62  if (vpIoTools::checkFilename(objectname + ".xml")) {
63  tracker.loadConfigFile(objectname + ".xml");
64  tracker.getCameraParameters(cam);
65  usexml = true;
66  }
67 #endif
68  if (!usexml) {
69  vpMe me;
70  me.setMaskSize(5);
71  me.setMaskNumber(180);
72  me.setRange(8);
74  me.setThreshold(20);
75  me.setMu1(0.5);
76  me.setMu2(0.5);
77  me.setSampleStep(4);
78  me.setNbTotalSample(250);
79  tracker.setMovingEdge(me);
80  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
81  tracker.setCameraParameters(cam);
82  tracker.setAngleAppear(vpMath::rad(70));
83  tracker.setAngleDisappear(vpMath::rad(80));
84  tracker.setNearClippingDistance(0.1);
85  tracker.setFarClippingDistance(100.0);
86  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
87  }
88 
89  tracker.setOgreVisibilityTest(false);
90  if (vpIoTools::checkFilename(objectname + ".cao"))
91  tracker.loadModel(objectname + ".cao");
92  else if (vpIoTools::checkFilename(objectname + ".wrl"))
93  tracker.loadModel(objectname + ".wrl");
94  tracker.setDisplayFeatures(true);
95  tracker.initClick(I, objectname + ".init", true);
96  tracker.track(I);
98 
100 #if (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)) || \
101  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
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
113 
115  vpKeyPoint keypoint_learning;
117  if (usexml) {
119  keypoint_learning.loadConfigFile(configurationFile);
121  }
122  else {
124  keypoint_learning.setDetector(detectorName);
125  keypoint_learning.setExtractor(extractorName);
126  keypoint_learning.setMatcher(matcherName);
128  }
129 
131  std::vector<cv::KeyPoint> trainKeyPoints;
132  double elapsedTime;
133  keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
135 
137  std::vector<vpPolygon> polygons;
138  std::vector<std::vector<vpPoint> > roisPt;
139  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
140  polygons = pair.first;
141  roisPt = pair.second;
142 
143  std::vector<cv::Point3f> points3f;
144  tracker.getPose(cMo);
145  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
147 
149  keypoint_learning.buildReference(I, trainKeyPoints, points3f);
151 
153  keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
155 
158  for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
159  vpDisplay::displayCross(I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
160  }
161  vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
162  vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
163  vpDisplay::flush(I);
164  vpDisplay::getClick(I, true);
166 
168  vpKeyPoint keypoint_detection;
169  if (usexml) {
170  keypoint_detection.loadConfigFile(configurationFile);
171  }
172  else {
173  keypoint_detection.setDetector(detectorName);
174  keypoint_detection.setExtractor(extractorName);
175  keypoint_detection.setMatcher(matcherName);
177  keypoint_detection.setMatchingRatioThreshold(0.8);
178  keypoint_detection.setUseRansacVVS(true);
179  keypoint_detection.setUseRansacConsensusPercentage(true);
180  keypoint_detection.setRansacConsensusPercentage(20.0);
181  keypoint_detection.setRansacIteration(200);
182  keypoint_detection.setRansacThreshold(0.005);
183  }
185 
187  keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
189 
190  double error;
191  bool click_done = false;
192 
193  while (!g.end()) {
194  g.acquire(I);
196 
197  vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
198 
200  if (keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
202 
204  tracker.setPose(I, cMo);
207  tracker.display(I, cMo, cam, vpColor::red, 2);
208  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
210  }
211 
212  vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
213  vpDisplay::flush(I);
214  if (vpDisplay::getClick(I, false)) {
215  click_done = true;
216  break;
217  }
218  }
219  if (!click_done)
221  }
222  catch (const vpException &e) {
223  std::cout << "Catch an exception: " << e << std::endl;
224  }
225 #else
226  (void)argc;
227  (void)argv;
228  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
229 #endif
230 
231  return EXIT_SUCCESS;
232 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:211
static const vpColor none
Definition: vpColor.h:223
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:1199
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1950
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:2033
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:212
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(double percentage)
Definition: vpKeyPoint.h:1795
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:1728
void setUseRansacVVS(bool ransacVVS)
Definition: vpKeyPoint.h:1947
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:1624
void loadLearningData(const std::string &filename, bool binaryMode=false, bool append=false)
void setRansacThreshold(double threshold)
Definition: vpKeyPoint.h:1882
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:970
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:460
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:1700
void saveLearningData(const std::string &filename, bool binaryMode=false, bool saveTrainingImages=true)
void setUseRansacConsensusPercentage(bool usePercentage)
Definition: vpKeyPoint.h:1938
void setMatchingRatioThreshold(double ratio)
Definition: vpKeyPoint.h:1779
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:221
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1566
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:189
void loadConfigFile(const std::string &configFile)
void setRansacIteration(int nbIter)
Definition: vpKeyPoint.h:1817
static double rad(double deg)
Definition: vpMath.h:127
Real-time 6D object pose tracking using its CAD model.
Definition: vpMe.h:124
void setMu1(const double &mu_1)
Definition: vpMe.h:399
void setRange(const unsigned int &range)
Definition: vpMe.h:429
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:519
void setNbTotalSample(const int &ntotal_sample)
Definition: vpMe.h:413
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:488
void setThreshold(const double &threshold)
Definition: vpMe.h:480
void setSampleStep(const double &sample_step)
Definition: vpMe.h:436
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:496
void setMu2(const double &mu_2)
Definition: vpMe.h:406
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:135
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)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.