Visual Servoing Platform  version 3.0.0
testKeyPoint-2.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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.
32  *
33  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
37 
38 #include <iostream>
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
43 
44 #include <visp3/vision/vpKeyPoint.h>
45 #include <visp3/core/vpImage.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/gui/vpDisplayX.h>
48 #include <visp3/gui/vpDisplayGTK.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayOpenCV.h>
51 #include <visp3/io/vpVideoReader.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/mbt/vpMbEdgeTracker.h>
54 #include <visp3/io/vpParseArgv.h>
55 
56 // List of allowed command line options
57 #define GETOPTARGS "cdh"
58 
59 void usage(const char *name, const char *badparam);
60 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
61 
70 void usage(const char *name, const char *badparam)
71 {
72  fprintf(stdout, "\n\
73 Test keypoints matching.\n\
74 \n\
75 SYNOPSIS\n\
76  %s [-c] [-d] [-h]\n", name);
77 
78  fprintf(stdout, "\n\
79 OPTIONS: \n\
80 \n\
81  -c\n\
82  Disable the mouse click. Useful to automaze the \n\
83  execution of this program without humain intervention.\n\
84 \n\
85  -d \n\
86  Turn off the display.\n\
87 \n\
88  -h\n\
89  Print the help.\n");
90 
91  if (badparam)
92  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
93 }
94 
106 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'c': click_allowed = false; break;
114  case 'd': display = false; break;
115  case 'h': usage(argv[0], NULL); return false; break;
116 
117  default:
118  usage(argv[0], optarg_);
119  return false; break;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
139 int main(int argc, const char ** argv) {
140  try {
141  std::string env_ipath;
142  bool opt_click_allowed = true;
143  bool opt_display = true;
144 
145  // Read the command line options
146  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
147  exit (-1);
148  }
149 
150  //Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
151  env_ipath = vpIoTools::getViSPImagesDataPath();
152 
153  if(env_ipath.empty()) {
154  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment variable value." << std::endl;
155  return -1;
156  }
157 
159 
160  //Set the path location of the image sequence
161  std::string dirname = vpIoTools::createFilePath(env_ipath, "ViSP-images/mbt/cube");
162 
163  //Build the name of the image files
164  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000.pgm");
165  vpImageIo::read(I, filenameRef);
166  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
167 
168 #if defined VISP_HAVE_X11
169  vpDisplayX display;
170 #elif defined VISP_HAVE_GTK
171  vpDisplayGTK display;
172 #elif defined VISP_HAVE_GDI
173  vpDisplayGDI display;
174 #else
175  vpDisplayOpenCV display;
176 #endif
177 
178  if (opt_display) {
179  display.init(I, 0, 0, "ORB keypoints matching and pose estimation");
180  }
181 
182  vpCameraParameters cam;
183  vpMbEdgeTracker tracker;
184  //Load config for tracker
185  std::string tracker_config_file = vpIoTools::createFilePath(env_ipath, "ViSP-images/mbt/cube.xml");
186 
187  bool usexml = false;
188 #ifdef VISP_HAVE_XML2
189  tracker.loadConfigFile(tracker_config_file);
190  tracker.getCameraParameters(cam);
191 
192  usexml = true;
193 #endif
194  if (! usexml) {
195  vpMe me;
196  me.setMaskSize(5);
197  me.setMaskNumber(180);
198  me.setRange(8);
199  me.setThreshold(10000);
200  me.setMu1(0.5);
201  me.setMu2(0.5);
202  me.setSampleStep(4);
203  me.setNbTotalSample(250);
204  tracker.setMovingEdge(me);
205  cam.initPersProjWithoutDistortion(547.7367575, 542.0744058, 338.7036994, 234.5083345);
206  tracker.setCameraParameters(cam);
207  tracker.setNearClippingDistance(0.01);
208  tracker.setFarClippingDistance(100.0);
210  }
211 
212  tracker.setAngleAppear(vpMath::rad(89));
213  tracker.setAngleDisappear(vpMath::rad(89));
214 
215  //Load CAO model
216  std::string cao_model_file = vpIoTools::createFilePath(env_ipath, "ViSP-images/mbt/cube.cao");
217  tracker.loadModel(cao_model_file);
218 
219  //Initialize the pose
220  std::string init_file = vpIoTools::createFilePath(env_ipath, "ViSP-images/mbt/cube.init");
221  if (opt_display && opt_click_allowed) {
222  tracker.initClick(I, init_file);
223  }
224  else
225  {
226  vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
227  tracker.initFromPose(I, cMoi);
228  }
229 
230  //Get the init pose
232  tracker.getPose(cMo);
233 
234  //Init keypoints
235  vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
236 #if (VISP_HAVE_OPENCV_VERSION >= 0x020400)
237  //Bug when using LSH index with FLANN and OpenCV 2.3.1.
238  //see http://code.opencv.org/issues/1741 (Bug #1741)
239  keypoints.setMatcher("FlannBased");
240 #if (VISP_HAVE_OPENCV_VERSION < 0x030000)
241  keypoints.setDetectorParameter("ORB", "nLevels", 1);
242 #else
243  cv::Ptr<cv::ORB> orb_detector = keypoints.getDetector("ORB").dynamicCast<cv::ORB>();
244  if(orb_detector != NULL) {
245  orb_detector->setNLevels(1);
246  }
247 #endif
248 #endif
249 
250  //Detect keypoints on the current image
251  std::vector<cv::KeyPoint> trainKeyPoints;
252  double elapsedTime;
253  keypoints.detect(I, trainKeyPoints, elapsedTime);
254 
255  //Keep only keypoints on the cube
256  std::vector<vpPolygon> polygons;
257  std::vector<std::vector<vpPoint> > roisPt;
258  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
259  polygons = pair.first;
260  roisPt = pair.second;
261 
262  //Compute the 3D coordinates
263  std::vector<cv::Point3f> points3f;
264  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
265 
266  //Build the reference keypoints
267  keypoints.buildReference(I, trainKeyPoints, points3f, false, 1);
268 
269 
270  //Read image 150
271  filenameRef = vpIoTools::createFilePath(dirname, "image0150.pgm");
272  vpImageIo::read(I, filenameRef);
273 
274  //Init pose at image 150
275  cMo.buildFrom(0.02651282185, -0.03713587374, 0.6873765919, 2.314744454, 0.3492296488, -0.1226054828);
276  tracker.initFromPose(I, cMo);
277 
278  //Detect keypoints on the image 150
279  keypoints.detect(I, trainKeyPoints, elapsedTime);
280 
281  //Keep only keypoints on the cube
282  pair = tracker.getPolygonFaces(false);
283  polygons = pair.first;
284  roisPt = pair.second;
285 
286  //Compute the 3D coordinates
287  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
288 
289  //Build the reference keypoints
290  keypoints.buildReference(I, trainKeyPoints, points3f, true, 2);
291 
292 
293  //Read image 200
294  filenameRef = vpIoTools::createFilePath(dirname, "image0200.pgm");
295  vpImageIo::read(I, filenameRef);
296 
297  //Init pose at image 200
298  cMo.buildFrom(0.02965448956, -0.07283091786, 0.7253526051, 2.300529617, -0.4286674806, 0.1788761025);
299  tracker.initFromPose(I, cMo);
300 
301  //Detect keypoints on the image 200
302  keypoints.detect(I, trainKeyPoints, elapsedTime);
303 
304  //Keep only keypoints on the cube
305  pair = tracker.getPolygonFaces(false);
306  polygons = pair.first;
307  roisPt = pair.second;
308 
309  //Compute the 3D coordinates
310  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
311 
312  //Build the reference keypoints
313  keypoints.buildReference(I, trainKeyPoints, points3f, true, 3);
314 
315 
316  //Init reader for getting the input image sequence
317  vpVideoReader g;
318  g.setFileName(filenameCur);
319  g.open(I);
320  g.acquire(I);
321 
322 #if defined VISP_HAVE_X11
323  vpDisplayX display2;
324 #elif defined VISP_HAVE_GTK
325  vpDisplayGTK display2;
326 #elif defined VISP_HAVE_GDI
327  vpDisplayGDI display2;
328 #else
329  vpDisplayOpenCV display2;
330 #endif
331 
332  vpImage<unsigned char> IMatching;
333 
334  keypoints.createImageMatching(I, IMatching);
335 
336  if (opt_display) {
337  display2.init(IMatching, 0, (int)I.getHeight() + 80, "IMatching");
338  }
339 
340  bool opt_click = false;
341  double error;
343  while((opt_display && !g.end()) || (!opt_display && g.getFrameIndex() < 30)) {
344  g.acquire(I);
345 
346  if(opt_display) {
348 
349  //Display image matching
350  keypoints.insertImageMatching(I, IMatching);
351 
352  vpDisplay::display(IMatching);
353  }
354 
355  //Match keypoints and estimate the pose
356  if(keypoints.matchPoint(I, cam, cMo, error, elapsedTime)) {
357  tracker.setCameraParameters(cam);
358  tracker.setPose(I, cMo);
359 
360  if(opt_display) {
361  tracker.display(I, cMo, cam, vpColor::red, 2);
362  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
363 
364  std::vector<vpImagePoint> ransacInliers = keypoints.getRansacInliers();
365  std::vector<vpImagePoint> ransacOutliers = keypoints.getRansacOutliers();
366 
367  for(std::vector<vpImagePoint>::const_iterator it = ransacInliers.begin(); it != ransacInliers.end(); ++it) {
369  vpImagePoint imPt(*it);
370  imPt.set_u(imPt.get_u() + I.getWidth());
371  imPt.set_v(imPt.get_v() + I.getHeight());
372  vpDisplay::displayCircle(IMatching, imPt, 4, vpColor::green);
373  }
374 
375  for(std::vector<vpImagePoint>::const_iterator it = ransacOutliers.begin(); it != ransacOutliers.end(); ++it) {
377  vpImagePoint imPt(*it);
378  imPt.set_u(imPt.get_u() + I.getWidth());
379  imPt.set_v(imPt.get_v() + I.getHeight());
380  vpDisplay::displayCircle(IMatching, imPt, 4, vpColor::red);
381  }
382 
383  keypoints.displayMatching(I, IMatching);
384 
385  //Display model in the correct sub-image in IMatching
386  vpCameraParameters cam2;
387  cam2.initPersProjWithoutDistortion(cam.get_px(), cam.get_py(),
388  cam.get_u0() + I.getWidth(), cam.get_v0() + I.getHeight());
389  tracker.setCameraParameters(cam2);
390  tracker.setPose(IMatching, cMo);
391  tracker.display(IMatching, cMo, cam2, vpColor::red, 2);
392  vpDisplay::displayFrame(IMatching, cMo, cam2, 0.025, vpColor::none, 3);
393  }
394  }
395 
396  if(opt_display) {
397  vpDisplay::flush(I);
398  vpDisplay::flush(IMatching);
399  }
400 
401  if (opt_click_allowed && opt_display) {
402  //Click requested to process next image
403  if(opt_click) {
404  vpDisplay::getClick(I, button, true);
405  if(button == vpMouseButton::button3) {
406  opt_click = false;
407  }
408  } else {
409  //Use right click to enable/disable step by step tracking
410  if(vpDisplay::getClick(I, button, false)) {
411  if (button == vpMouseButton::button3) {
412  opt_click = true;
413  }
414  else if(button == vpMouseButton::button1) {
415  break;
416  }
417  }
418  }
419  }
420  }
421 
422  } catch(vpException &e) {
423  std::cerr << e.what() << std::endl;
424  return -1;
425  }
426 
427  std::cout << "testKeyPoint-2 is ok !" << std::endl;
428  return 0;
429 }
430 #else
431 int main() {
432  std::cerr << "You need OpenCV library." << std::endl;
433 
434  return 0;
435 }
436 
437 #endif
virtual void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setMovingEdge(const vpMe &me)
long getFrameIndex() const
double get_u0() const
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1091
unsigned int getWidth() const
Definition: vpImage.h:161
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:232
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:430
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:460
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:260
void setNbTotalSample(const int &nb)
Definition: vpMe.h:188
Define the X11 console to display images.
Definition: vpDisplayX.h:148
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:175
error that can be emited by ViSP classes.
Definition: vpException.h:73
Definition: vpMe.h:59
Make the complete tracking of an object by using its CAD model.
double get_py() const
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
void loadConfigFile(const std::string &configFile)
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
void setMu1(const double &mu_1)
Definition: vpMe.h:160
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static const vpColor red
Definition: vpColor.h:163
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
virtual void setNearClippingDistance(const double &dist)
void open(vpImage< vpRGBa > &I)
const char * what() const
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:468
double get_v0() const
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:356
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1265
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidate, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=NULL)
Definition: vpKeyPoint.cpp:615
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:225
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
void acquire(vpImage< vpRGBa > &I)
virtual void setFarClippingDistance(const double &dist)
void setFileName(const char *filename)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:419
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
double get_px() const
static double rad(double deg)
Definition: vpMath.h:104
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, vpImagePoint offset=vpImagePoint(0, 0))
Definition: vpDisplay.cpp:373
void setMu2(const double &mu_2)
Definition: vpMe.h:174
virtual void loadModel(const char *modelFile, const bool verbose=false)
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:213
void setThreshold(const double &t)
Definition: vpMe.h:288
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)
unsigned int getHeight() const
Definition: vpImage.h:152
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setRange(const unsigned int &r)
Definition: vpMe.h:218
virtual void setClipping(const unsigned int &flags)
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(const bool orderPolygons=true, const bool useVisibility=true)