Visual Servoing Platform  version 3.2.0
testKeyPoint-6.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 
216  vpImage<unsigned char> Iinput, I;
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(Iinput, filename);
224  Iinput.quarterSizeImage(I);
225 
226 #if defined VISP_HAVE_X11
227  vpDisplayX display;
228 #elif defined VISP_HAVE_GTK
229  vpDisplayGTK display;
230 #elif defined VISP_HAVE_GDI
231  vpDisplayGDI display;
232 #else
233  vpDisplayOpenCV display;
234 #endif
235 
236  if (opt_display) {
237  display.init(I, 0, 0, "KeyPoints detection.");
238  }
239 
240  vpKeyPoint keyPoints;
241 
242  std::vector<std::string> descriptorNames;
243 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
244  descriptorNames.push_back("SIFT");
245  descriptorNames.push_back("SURF");
246 #endif
247  descriptorNames.push_back("ORB");
248 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
249  descriptorNames.push_back("BRISK");
250 #endif
251 #if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
252  descriptorNames.push_back("BRIEF");
253 #if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
254  descriptorNames.push_back("FREAK");
255 #endif
256 #endif
257 #if defined(VISP_HAVE_OPENCV_XFEATURES2D)
258  descriptorNames.push_back("DAISY");
259  descriptorNames.push_back("LATCH");
260 #endif
261 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
262  descriptorNames.push_back("VGG");
263  descriptorNames.push_back("BoostDesc");
264 #endif
265 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
266  descriptorNames.push_back("KAZE");
267  descriptorNames.push_back("AKAZE");
268 #endif
269 
270  std::string detectorName = "FAST";
271  keyPoints.setDetector(detectorName);
272  std::vector<cv::KeyPoint> kpts;
273 
274  keyPoints.detect(I, kpts);
275  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
276  if (kpts.empty()) {
277  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
278  return EXIT_FAILURE;
279  }
280 
281  for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
282  keyPoints.setExtractor(*itd);
283 
284  if (*itd == "KAZE") {
285  detectorName = "KAZE";
286  keyPoints.setDetector(detectorName);
287  keyPoints.detect(I, kpts);
288  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
289  if (kpts.empty()) {
290  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
291  return EXIT_FAILURE;
292  }
293  } else if (*itd == "AKAZE") {
294  detectorName = "AKAZE";
295  keyPoints.setDetector(detectorName);
296  keyPoints.detect(I, kpts);
297  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
298  if (kpts.empty()) {
299  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
300  return EXIT_FAILURE;
301  }
302  } else if (*itd == "BoostDesc") {
303 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
304  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
305  // Init BIN BOOST descriptor for FAST keypoints
306  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
307 #endif
308  }
309 
310  double t = vpTime::measureTimeMs();
311  cv::Mat descriptor;
312  keyPoints.extract(I, kpts, descriptor);
313  t = vpTime::measureTimeMs() - t;
314 
315  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
316  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
317  << " ms." << std::endl;
318  if (descriptor.empty()) {
319  std::cerr << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
320  return EXIT_FAILURE;
321  }
322 
323  if (opt_display) {
325 
326  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
327  vpImagePoint imPt;
328  imPt.set_uv(it->pt.x, it->pt.y);
329 
331  }
332 
333  vpDisplay::flush(I);
334 
335  if (opt_click_allowed) {
337  }
338  }
339  }
340 
341  std::cout << "\n\n";
342 
343  std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
344 
345  for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
347 
348  if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
349  detectorName = "KAZE";
350  keyPoints.setDetector(detectorName);
351  keyPoints.detect(I, kpts);
352  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
353  if (kpts.empty()) {
354  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
355  return EXIT_FAILURE;
356  }
357  } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
358  detectorName = "AKAZE";
359  keyPoints.setDetector(detectorName);
360  keyPoints.detect(I, kpts);
361  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
362  if (kpts.empty()) {
363  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
364  return EXIT_FAILURE;
365  }
366  } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
367 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
368  detectorName = "FAST";
369  keyPoints.setDetector(detectorName);
370  keyPoints.detect(I, kpts);
371  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
372  if (kpts.empty()) {
373  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
374  return EXIT_FAILURE;
375  }
376 
377  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
378  // Init BIN BOOST descriptor for FAST keypoints
379  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
380 #endif
381  }
382 
383  double t = vpTime::measureTimeMs();
384  cv::Mat descriptor;
385  keyPoints.extract(I, kpts, descriptor);
386  t = vpTime::measureTimeMs() - t;
387 
388  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
389  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
390  << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
391  << std::endl;
392  if (descriptor.empty()) {
393  std::cerr << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
394  << " and image:" << filename << "." << std::endl;
395  return EXIT_FAILURE;
396  }
397 
398  if (opt_display) {
400 
401  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
402  vpImagePoint imPt;
403  imPt.set_uv(it->pt.x, it->pt.y);
404 
406  }
407 
408  vpDisplay::flush(I);
409 
410  if (opt_click_allowed) {
412  }
413  }
414  }
415 
416  } catch (const vpException &e) {
417  std::cerr << e.what() << std::endl;
418  return EXIT_FAILURE;
419  }
420 
421  std::cout << "testKeyPoint-6 is ok !" << std::endl;
422  return EXIT_SUCCESS;
423 }
424 #else
425 #include <cstdlib>
426 
427 int main()
428 {
429  std::cerr << "You need OpenCV library." << std::endl;
430 
431  return EXIT_SUCCESS;
432 }
433 
434 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1317
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:1542
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
Definition: vpKeyPoint.h:557
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:292
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:734
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
void quarterSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:1327
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:228
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:517
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:792
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)