Visual Servoing Platform  version 3.1.0
testKeyPoint-6.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test descriptor computation.
33  *
34  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
44 
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/gui/vpDisplayGDI.h>
48 #include <visp3/gui/vpDisplayGTK.h>
49 #include <visp3/gui/vpDisplayOpenCV.h>
50 #include <visp3/gui/vpDisplayX.h>
51 #include <visp3/io/vpImageIo.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/vision/vpKeyPoint.h>
54 
55 // List of allowed command line options
56 #define GETOPTARGS "cdh"
57 
58 void usage(const char *name, const char *badparam);
59 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
60 
67 void usage(const char *name, const char *badparam)
68 {
69  fprintf(stdout, "\n\
70 Test keypoint descriptor extraction.\n\
71 \n\
72 SYNOPSIS\n\
73  %s [-c] [-d] [-h]\n", name);
74 
75  fprintf(stdout, "\n\
76 OPTIONS: \n\
77 \n\
78  -c\n\
79  Disable the mouse click. Useful to automaze the \n\
80  execution of this program without humain intervention.\n\
81 \n\
82  -d \n\
83  Turn off the display.\n\
84 \n\
85  -h\n\
86  Print the help.\n");
87 
88  if (badparam)
89  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
90 }
91 
103 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
104 {
105  const char *optarg_;
106  int c;
107  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
108 
109  switch (c) {
110  case 'c':
111  click_allowed = false;
112  break;
113  case 'd':
114  display = false;
115  break;
116  case 'h':
117  usage(argv[0], NULL);
118  return false;
119  break;
120 
121  default:
122  usage(argv[0], optarg_);
123  return false;
124  break;
125  }
126  }
127 
128  if ((c == 1) || (c == -1)) {
129  // standalone param or error
130  usage(argv[0], NULL);
131  std::cerr << "ERROR: " << std::endl;
132  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133  return false;
134  }
135 
136  return true;
137 }
138 
147 std::string getOpenCVType(const int type)
148 {
149  std::string type_string = "";
150 
151  switch (type) {
152  case CV_8U:
153  type_string = "CV_8U";
154  break;
155 
156  case CV_8S:
157  type_string = "CV_8S";
158  break;
159 
160  case CV_16U:
161  type_string = "CV_16U";
162  break;
163 
164  case CV_16S:
165  type_string = "CV_16S";
166  break;
167 
168  case CV_32S:
169  type_string = "CV_32S";
170  break;
171 
172  case CV_32F:
173  type_string = "CV_32F";
174  break;
175 
176  case CV_64F:
177  type_string = "CV_64F";
178  break;
179 
180  default:
181  type_string = "Problem with type !";
182  break;
183  }
184 
185  return type_string;
186 }
187 
193 int main(int argc, const char **argv)
194 {
195  try {
196  std::string env_ipath;
197  bool opt_click_allowed = true;
198  bool opt_display = true;
199 
200  // Read the command line options
201  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
202  exit(EXIT_FAILURE);
203  }
204 
205  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
206  // environment variable value
207  env_ipath = vpIoTools::getViSPImagesDataPath();
208 
209  if (env_ipath.empty()) {
210  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
211  "variable value."
212  << std::endl;
213  return EXIT_FAILURE;
214  }
215 
217 
218  // Set the path location of the image sequence
219  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
220 
221  // Build the name of the image files
222  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
223  vpImageIo::read(I, filename);
224 
225 #if defined VISP_HAVE_X11
226  vpDisplayX display;
227 #elif defined VISP_HAVE_GTK
228  vpDisplayGTK display;
229 #elif defined VISP_HAVE_GDI
230  vpDisplayGDI display;
231 #else
232  vpDisplayOpenCV display;
233 #endif
234 
235  if (opt_display) {
236  display.init(I, 0, 0, "KeyPoints detection.");
237  }
238 
239  vpKeyPoint keyPoints;
240 
241  std::vector<std::string> descriptorNames;
242 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
243  descriptorNames.push_back("SIFT");
244  descriptorNames.push_back("SURF");
245 #endif
246  descriptorNames.push_back("ORB");
247 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
248  descriptorNames.push_back("BRISK");
249 #endif
250 #if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
251  descriptorNames.push_back("BRIEF");
252 #if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
253  descriptorNames.push_back("FREAK");
254 #endif
255 #endif
256 #if defined(VISP_HAVE_OPENCV_XFEATURES2D)
257  descriptorNames.push_back("DAISY");
258  descriptorNames.push_back("LATCH");
259 #endif
260 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
261  descriptorNames.push_back("VGG");
262  descriptorNames.push_back("BoostDesc");
263 #endif
264 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
265  descriptorNames.push_back("KAZE");
266  descriptorNames.push_back("AKAZE");
267 #endif
268 
269  std::string detectorName = "FAST";
270  keyPoints.setDetector(detectorName);
271  std::vector<cv::KeyPoint> kpts;
272 
273  keyPoints.detect(I, kpts);
274  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
275  if (kpts.empty()) {
276  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
277  return EXIT_FAILURE;
278  }
279 
280  for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
281  keyPoints.setExtractor(*itd);
282 
283  if (*itd == "KAZE") {
284  detectorName = "KAZE";
285  keyPoints.setDetector(detectorName);
286  keyPoints.detect(I, kpts);
287  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
288  if (kpts.empty()) {
289  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
290  return EXIT_FAILURE;
291  }
292  } else if (*itd == "AKAZE") {
293  detectorName = "AKAZE";
294  keyPoints.setDetector(detectorName);
295  keyPoints.detect(I, kpts);
296  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
297  if (kpts.empty()) {
298  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
299  return EXIT_FAILURE;
300  }
301  } else if (*itd == "BoostDesc") {
302 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
303  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
304  // Init BIN BOOST descriptor for FAST keypoints
305  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
306 #endif
307  }
308 
309  double t = vpTime::measureTimeMs();
310  cv::Mat descriptor;
311  keyPoints.extract(I, kpts, descriptor);
312  t = vpTime::measureTimeMs() - t;
313 
314  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
315  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
316  << " ms." << std::endl;
317  if (descriptor.empty()) {
318  std::cerr << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
319  return EXIT_FAILURE;
320  }
321 
322  if (opt_display) {
324 
325  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
326  vpImagePoint imPt;
327  imPt.set_uv(it->pt.x, it->pt.y);
328 
330  }
331 
332  vpDisplay::flush(I);
333 
334  if (opt_click_allowed) {
336  }
337  }
338  }
339 
340  std::cout << "\n\n";
341 
342  std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
343 
344  for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
346 
347  if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
348  detectorName = "KAZE";
349  keyPoints.setDetector(detectorName);
350  keyPoints.detect(I, kpts);
351  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
352  if (kpts.empty()) {
353  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
354  return EXIT_FAILURE;
355  }
356  } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
357  detectorName = "AKAZE";
358  keyPoints.setDetector(detectorName);
359  keyPoints.detect(I, kpts);
360  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
361  if (kpts.empty()) {
362  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
363  return EXIT_FAILURE;
364  }
365  } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
366 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
367  detectorName = "FAST";
368  keyPoints.setDetector(detectorName);
369  keyPoints.detect(I, kpts);
370  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
371  if (kpts.empty()) {
372  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
373  return EXIT_FAILURE;
374  }
375 
376  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
377  // Init BIN BOOST descriptor for FAST keypoints
378  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
379 #endif
380  }
381 
382  double t = vpTime::measureTimeMs();
383  cv::Mat descriptor;
384  keyPoints.extract(I, kpts, descriptor);
385  t = vpTime::measureTimeMs() - t;
386 
387  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
388  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
389  << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
390  << std::endl;
391  if (descriptor.empty()) {
392  std::cerr << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
393  << " and image:" << filename << "." << std::endl;
394  return EXIT_FAILURE;
395  }
396 
397  if (opt_display) {
399 
400  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
401  vpImagePoint imPt;
402  imPt.set_uv(it->pt.x, it->pt.y);
403 
405  }
406 
407  vpDisplay::flush(I);
408 
409  if (opt_click_allowed) {
411  }
412  }
413  }
414 
415  } catch (vpException &e) {
416  std::cerr << e.what() << std::endl;
417  return EXIT_FAILURE;
418  }
419 
420  std::cout << "testKeyPoint-6 is ok !" << std::endl;
421  return EXIT_SUCCESS;
422 }
423 #else
424 #include <cstdlib>
425 
426 int main()
427 {
428  std::cerr << "You need OpenCV library." << std::endl;
429 
430  return EXIT_SUCCESS;
431 }
432 
433 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1210
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:180
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1435
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
Definition: vpKeyPoint.h:552
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
vpFeatureDescriptorType
Definition: vpKeyPoint.h:287
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:729
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
const char * what() const
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:223
void set_uv(const double u, const double v)
Definition: vpImagePoint.h:248
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
Definition: vpKeyPoint.h:512
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:787
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, std::vector< cv::Point3f > *trainPoints=NULL)