Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testKeyPoint-4.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test keypoint matching and pose estimation with mostly OpenCV functions
32  * calls to detect potential memory leaks in testKeyPoint-2.cpp.
33  */
34 
42 #include <iostream>
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D)
47 
48 #include <visp3/core/vpHomogeneousMatrix.h>
49 #include <visp3/core/vpImage.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/gui/vpDisplayGDI.h>
52 #include <visp3/gui/vpDisplayGTK.h>
53 #include <visp3/gui/vpDisplayOpenCV.h>
54 #include <visp3/gui/vpDisplayX.h>
55 #include <visp3/io/vpImageIo.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/io/vpVideoReader.h>
58 #include <visp3/mbt/vpMbEdgeTracker.h>
59 #include <visp3/vision/vpKeyPoint.h>
60 
61 // List of allowed command line options
62 #define GETOPTARGS "cdh"
63 
64 #ifdef ENABLE_VISP_NAMESPACE
65 using namespace VISP_NAMESPACE_NAME;
66 #endif
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
70 
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Test keypoints matching.\n\
83 \n\
84 SYNOPSIS\n\
85  %s [-c] [-d] [-h]\n",
86  name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: \n\
90 \n\
91  -c\n\
92  Disable the mouse click. Useful to automate the \n\
93  execution of this program without human intervention.\n\
94 \n\
95  -d \n\
96  Turn off the display.\n\
97 \n\
98  -h\n\
99  Print the help.\n");
100 
101  if (badparam)
102  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
103 }
104 
116 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
117 {
118  const char *optarg_;
119  int c;
120  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
121 
122  switch (c) {
123  case 'c':
124  click_allowed = false;
125  break;
126  case 'd':
127  display = false;
128  break;
129  case 'h':
130  usage(argv[0], nullptr);
131  return false;
132  break;
133 
134  default:
135  usage(argv[0], optarg_);
136  return false;
137  break;
138  }
139  }
140 
141  if ((c == 1) || (c == -1)) {
142  // standalone param or error
143  usage(argv[0], nullptr);
144  std::cerr << "ERROR: " << std::endl;
145  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146  return false;
147  }
148 
149  return true;
150 }
151 
152 template <typename Type>
153 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &I,
154  vpImage<Type> &Imatch, vpImage<Type> &Iref)
155 {
156 #if VISP_HAVE_DATASET_VERSION >= 0x030600
157  std::string ext("png");
158 #else
159  std::string ext("pgm");
160 #endif
161  // Set the path location of the image sequence
162  std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
163 
164  // Build the name of the image files
165  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000." + ext);
166  vpImageIo::read(I, filenameRef);
167  Iref = I;
168  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d." + ext);
169 
170 #if defined(VISP_HAVE_X11)
171  vpDisplayX display, display2;
172 #elif defined(VISP_HAVE_GTK)
173  vpDisplayGTK display, display2;
174 #elif defined(VISP_HAVE_GDI)
175  vpDisplayGDI display, display2;
176 #elif defined(HAVE_OPENCV_HIGHGUI)
177  vpDisplayOpenCV display, display2;
178 #endif
179 
180  if (opt_display) {
181  display.setDownScalingFactor(vpDisplay::SCALE_AUTO);
182  display.init(I, 0, 0, "ORB keypoints matching");
183  Imatch.resize(I.getHeight(), 2 * I.getWidth());
184  Imatch.insert(I, vpImagePoint(0, 0));
185  display2.setDownScalingFactor(vpDisplay::SCALE_AUTO);
186  display2.init(Imatch, 0, (int)I.getHeight() / vpDisplay::getDownScalingFactor(I) + 70, "ORB keypoints matching");
187  }
188 
189  vpCameraParameters cam;
190  vpMbEdgeTracker tracker;
191  // Load config for tracker
192  std::string tracker_config_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.xml");
193 
194 #if defined(VISP_HAVE_PUGIXML)
195  tracker.loadConfigFile(tracker_config_file);
196  tracker.getCameraParameters(cam);
197 #else
198  // Corresponding parameters manually set to have an example code
199  vpMe me;
200  me.setMaskSize(5);
201  me.setMaskNumber(180);
202  me.setRange(8);
204  me.setThreshold(20);
205  me.setMu1(0.5);
206  me.setMu2(0.5);
207  me.setSampleStep(4);
208  me.setNbTotalSample(250);
209  tracker.setMovingEdge(me);
210  cam.initPersProjWithoutDistortion(547.7367575, 542.0744058, 338.7036994, 234.5083345);
211  tracker.setCameraParameters(cam);
212  tracker.setNearClippingDistance(0.01);
213  tracker.setFarClippingDistance(100.0);
215 #endif
216 
217  tracker.setAngleAppear(vpMath::rad(89));
218  tracker.setAngleDisappear(vpMath::rad(89));
219 
220  // Load CAO model
221  std::string cao_model_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.cao");
222  tracker.loadModel(cao_model_file);
223 
224  // Initialize the pose
225  std::string init_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.init");
226  if (opt_display && opt_click_allowed) {
227  tracker.initClick(I, init_file);
228  }
229  else {
230  vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
231  tracker.initFromPose(I, cMoi);
232  }
233 
234  // Get the init pose
236  tracker.getPose(cMo);
237 
238  // Init keypoints
239  cv::Ptr<cv::FeatureDetector> detector;
240  cv::Ptr<cv::DescriptorExtractor> extractor;
241  cv::Ptr<cv::DescriptorMatcher> matcher;
242 
243 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
244  detector = cv::ORB::create(500, 1.2f, 1);
245  extractor = cv::ORB::create(500, 1.2f, 1);
246 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020301)
247  detector = cv::FeatureDetector::create("ORB");
248  extractor = cv::DescriptorExtractor::create("ORB");
249 #endif
250  matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
251 
252 #if (VISP_HAVE_OPENCV_VERSION >= 0x020400 && VISP_HAVE_OPENCV_VERSION < 0x030000)
253  detector->set("nLevels", 1);
254 #endif
255 
256  // Detect keypoints on the current image
257  std::vector<cv::KeyPoint> trainKeyPoints;
258  cv::Mat matImg;
259  vpImageConvert::convert(I, matImg);
260  detector->detect(matImg, trainKeyPoints);
261 
262  // Keep only keypoints on the cube
263  std::vector<vpPolygon> polygons;
264  std::vector<std::vector<vpPoint> > roisPt;
265  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
266  polygons = pair.first;
267  roisPt = pair.second;
268 
269  // Compute the 3D coordinates
270  std::vector<cv::Point3f> points3f;
271  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
272 
273  // Extract descriptors
274  cv::Mat trainDescriptors;
275  extractor->compute(matImg, trainKeyPoints, trainDescriptors);
276 
277  if (trainKeyPoints.size() != (size_t)trainDescriptors.rows || trainKeyPoints.size() != points3f.size()) {
278  throw(vpException(vpException::fatalError, "Problem with training data size !"));
279  }
280 
281  // Init reader for getting the input image sequence
282  vpVideoReader g;
283  g.setFileName(filenameCur);
284  g.open(I);
285  g.acquire(I);
286 
287  bool opt_click = false;
289  while ((opt_display && !g.end()) || (!opt_display && g.getFrameIndex() < 30)) {
290  g.acquire(I);
291 
292  vpImageConvert::convert(I, matImg);
293  std::vector<cv::KeyPoint> queryKeyPoints;
294  detector->detect(matImg, queryKeyPoints);
295 
296  cv::Mat queryDescriptors;
297  extractor->compute(matImg, queryKeyPoints, queryDescriptors);
298 
299  std::vector<std::vector<cv::DMatch> > knn_matches;
300  std::vector<cv::DMatch> matches;
301  matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
302  for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
303  ++it) {
304  if (it->size() > 1) {
305  double ratio = (*it)[0].distance / (*it)[1].distance;
306  if (ratio < 0.85) {
307  matches.push_back((*it)[0]);
308  }
309  }
310  }
311 
312  vpPose estimated_pose;
313  for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
314  vpPoint pt(points3f[(size_t)(it->trainIdx)].x, points3f[(size_t)(it->trainIdx)].y,
315  points3f[(size_t)(it->trainIdx)].z);
316 
317  double x = 0.0, y = 0.0;
318  vpPixelMeterConversion::convertPoint(cam, queryKeyPoints[(size_t)(it->queryIdx)].pt.x,
319  queryKeyPoints[(size_t)(it->queryIdx)].pt.y, x, y);
320  pt.set_x(x);
321  pt.set_y(y);
322 
323  estimated_pose.addPoint(pt);
324  }
325 
326  bool is_pose_estimated = false;
327  if (estimated_pose.npt >= 4) {
328  try {
329  unsigned int nb_inliers = (unsigned int)(0.6 * estimated_pose.npt);
330  estimated_pose.setRansacNbInliersToReachConsensus(nb_inliers);
331  estimated_pose.setRansacThreshold(0.01);
332  estimated_pose.setRansacMaxTrials(500);
333  estimated_pose.computePose(vpPose::RANSAC, cMo);
334  is_pose_estimated = true;
335  }
336  catch (...) {
337  is_pose_estimated = false;
338  }
339  }
340 
341  if (opt_display) {
343 
344  Imatch.insert(I, vpImagePoint(0, Iref.getWidth()));
345  vpDisplay::display(Imatch);
346  for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
347  vpImagePoint leftPt(trainKeyPoints[(size_t)it->trainIdx].pt.y, trainKeyPoints[(size_t)it->trainIdx].pt.x);
348  vpImagePoint rightPt(queryKeyPoints[(size_t)it->queryIdx].pt.y,
349  queryKeyPoints[(size_t)it->queryIdx].pt.x + Iref.getWidth());
350  vpDisplay::displayLine(Imatch, leftPt, rightPt, vpColor::green);
351  }
352 
353  if (is_pose_estimated) {
354  tracker.setPose(I, cMo);
355  tracker.display(I, cMo, cam, vpColor::red);
356  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none);
357  }
358 
359  vpDisplay::flush(Imatch);
360  vpDisplay::flush(I);
361  }
362 
363  // Click requested to process next image
364  if (opt_click_allowed && opt_display) {
365  if (opt_click) {
366  vpDisplay::getClick(I, button, true);
367  if (button == vpMouseButton::button3) {
368  opt_click = false;
369  }
370  }
371  else {
372  // Use right click to enable/disable step by step tracking
373  if (vpDisplay::getClick(I, button, false)) {
374  if (button == vpMouseButton::button3) {
375  opt_click = true;
376  }
377  else if (button == vpMouseButton::button1) {
378  break;
379  }
380  }
381  }
382  }
383  }
384 }
385 
386 int main(int argc, const char **argv)
387 {
388  try {
389  std::string env_ipath;
390  bool opt_click_allowed = true;
391  bool opt_display = true;
392 
393  // Read the command line options
394  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
395  return EXIT_FAILURE;
396  }
397 
398  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
399  // environment variable value
400  env_ipath = vpIoTools::getViSPImagesDataPath();
401 
402  if (env_ipath.empty()) {
403  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
404  "variable value."
405  << std::endl;
406  return EXIT_FAILURE;
407  }
408 
409  {
410  vpImage<unsigned char> I, Imatch, Iref;
411 
412  std::cout << "-- Test on gray level images" << std::endl;
413  run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
414  }
415 
416  {
417  vpImage<vpRGBa> I, Imatch, Iref;
418 
419  std::cout << "-- Test on color images" << std::endl;
420  run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
421  }
422 
423  }
424  catch (const vpException &e) {
425  std::cerr << e.what() << std::endl;
426  return EXIT_FAILURE;
427  }
428 
429  std::cout << "testKeyPoint-4 is ok !" << std::endl;
430  return EXIT_SUCCESS;
431 }
432 
433 #else
434 int main()
435 {
436  std::cerr << "You need OpenCV library." << std::endl;
437 
438  return EXIT_SUCCESS;
439 }
440 
441 #endif
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
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
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 displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
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 flush(const vpImage< unsigned char > &I)
@ SCALE_AUTO
Definition: vpDisplay.h:184
unsigned int getDownScalingFactor()
Definition: vpDisplay.h:221
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
const char * what() const
Definition: vpException.cpp:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Definition of the vpImage class member functions.
Definition: vpImage.h:131
unsigned int getWidth() const
Definition: vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:542
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:637
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
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
static double rad(double deg)
Definition: vpMath.h:129
Make the complete tracking of an object by using its CAD model.
virtual void setNearClippingDistance(const double &dist) VP_OVERRIDE
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false) VP_OVERRIDE
virtual void setFarClippingDistance(const double &dist) VP_OVERRIDE
virtual void setClipping(const unsigned int &flags) VP_OVERRIDE
void setMovingEdge(const vpMe &me)
virtual void setCameraParameters(const vpCameraParameters &cam) VP_OVERRIDE
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo) VP_OVERRIDE
virtual void loadConfigFile(const std::string &configFile, bool verbose=true) VP_OVERRIDE
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:250
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:416
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:483
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:472
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(bool orderPolygons=true, bool useVisibility=true, bool clipPolygon=false)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:258
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
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:77
void setRansacMaxTrials(const int &rM)
Definition: vpPose.h:406
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:96
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:387
@ RANSAC
Definition: vpPose.h:87
unsigned int npt
Number of point used in pose computation.
Definition: vpPose.h:113
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
Definition: vpPose.cpp:385
void setRansacThreshold(const double &t)
Definition: vpPose.h:392
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)
long getFrameIndex() const