Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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(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 
188 template<typename Type>
189 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display,
190  vpImage<Type> &Iinput, vpImage<Type> &I)
191 {
192  // Set the path location of the image sequence
193  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
194 
195  // Build the name of the image files
196  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
197  vpImageIo::read(Iinput, filename);
198  Iinput.quarterSizeImage(I);
199 
200 #if defined VISP_HAVE_X11
201  vpDisplayX display;
202 #elif defined VISP_HAVE_GTK
203  vpDisplayGTK display;
204 #elif defined VISP_HAVE_GDI
205  vpDisplayGDI display;
206 #else
207  vpDisplayOpenCV display;
208 #endif
209 
210  if (opt_display) {
211  display.init(I, 0, 0, "KeyPoints detection.");
212  }
213 
214  vpKeyPoint keyPoints;
215 
216  std::vector<std::string> descriptorNames;
217 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
218  descriptorNames.push_back("SIFT");
219  descriptorNames.push_back("SURF");
220 #endif
221  descriptorNames.push_back("ORB");
222 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
223  descriptorNames.push_back("BRISK");
224 #endif
225 #if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
226  descriptorNames.push_back("BRIEF");
227 #if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
228  descriptorNames.push_back("FREAK");
229 #endif
230 #endif
231 #if defined(VISP_HAVE_OPENCV_XFEATURES2D)
232  descriptorNames.push_back("DAISY");
233  descriptorNames.push_back("LATCH");
234 #endif
235 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
236  descriptorNames.push_back("VGG");
237  descriptorNames.push_back("BoostDesc");
238 #endif
239 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
240  descriptorNames.push_back("KAZE");
241  descriptorNames.push_back("AKAZE");
242 #endif
243 
244  std::string detectorName = "FAST";
245  keyPoints.setDetector(detectorName);
246  std::vector<cv::KeyPoint> kpts;
247 
248  keyPoints.detect(I, kpts);
249  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
250  if (kpts.empty()) {
251  std::stringstream ss;
252  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
253  throw(vpException(vpException::fatalError, ss.str()));
254  }
255 
256  for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
257  keyPoints.setExtractor(*itd);
258 
259  if (*itd == "KAZE") {
260  detectorName = "KAZE";
261  keyPoints.setDetector(detectorName);
262  keyPoints.detect(I, kpts);
263  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
264  if (kpts.empty()) {
265  std::stringstream ss;
266  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
267  throw(vpException(vpException::fatalError, ss.str()));
268  }
269  } else if (*itd == "AKAZE") {
270  detectorName = "AKAZE";
271  keyPoints.setDetector(detectorName);
272  keyPoints.detect(I, kpts);
273  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
274  if (kpts.empty()) {
275  std::stringstream ss;
276  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
277  throw(vpException(vpException::fatalError, ss.str()));
278  }
279  } else if (*itd == "BoostDesc") {
280 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
281  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
282  // Init BIN BOOST descriptor for FAST keypoints
283  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
284 #endif
285  }
286 
287  double t = vpTime::measureTimeMs();
288  cv::Mat descriptor;
289  keyPoints.extract(I, kpts, descriptor);
290  t = vpTime::measureTimeMs() - t;
291 
292  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
293  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
294  << " ms." << std::endl;
295  if (descriptor.empty()) {
296  std::stringstream ss;
297  ss << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
298  throw(vpException(vpException::fatalError, ss.str()));
299  }
300 
301  if (opt_display) {
303 
304  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
305  vpImagePoint imPt;
306  imPt.set_uv(it->pt.x, it->pt.y);
307 
309  }
310 
311  vpDisplay::flush(I);
312 
313  if (opt_click_allowed) {
315  }
316  }
317  }
318 
319  std::cout << "\n\n";
320 
321  std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
322 
323  for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
325 
326  if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
327  detectorName = "KAZE";
328  keyPoints.setDetector(detectorName);
329  keyPoints.detect(I, kpts);
330  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
331  if (kpts.empty()) {
332  std::stringstream ss;
333  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
334  throw(vpException(vpException::fatalError, ss.str()));
335  }
336  } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
337  detectorName = "AKAZE";
338  keyPoints.setDetector(detectorName);
339  keyPoints.detect(I, kpts);
340  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
341  if (kpts.empty()) {
342  std::stringstream ss;
343  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
344  throw(vpException(vpException::fatalError, ss.str()));
345  }
346  } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
347 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
348  detectorName = "FAST";
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::stringstream ss;
354  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
355  throw(vpException(vpException::fatalError, ss.str()));
356  }
357 
358  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
359  // Init BIN BOOST descriptor for FAST keypoints
360  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
361 #endif
362  }
363 
364  double t = vpTime::measureTimeMs();
365  cv::Mat descriptor;
366  keyPoints.extract(I, kpts, descriptor);
367  t = vpTime::measureTimeMs() - t;
368 
369  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
370  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
371  << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
372  << std::endl;
373  if (descriptor.empty()) {
374  std::stringstream ss;
375  ss << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
376  << " and image:" << filename << "." << std::endl;
377  throw(vpException(vpException::fatalError, ss.str()));
378  }
379 
380  if (opt_display) {
382 
383  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
384  vpImagePoint imPt;
385  imPt.set_uv(it->pt.x, it->pt.y);
386 
388  }
389 
390  vpDisplay::flush(I);
391 
392  if (opt_click_allowed) {
394  }
395  }
396  }
397 }
398 
404 int main(int argc, const char **argv)
405 {
406  try {
407  std::string env_ipath;
408  bool opt_click_allowed = true;
409  bool opt_display = true;
410 
411  // Read the command line options
412  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
413  exit(EXIT_FAILURE);
414  }
415 
416  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
417  // environment variable value
418  env_ipath = vpIoTools::getViSPImagesDataPath();
419 
420  if (env_ipath.empty()) {
421  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
422  "variable value."
423  << std::endl;
424  return EXIT_FAILURE;
425  }
426 
427  {
428  vpImage<unsigned char> Iinput, I;
429 
430  std::cout << "-- Test on gray level images" << std::endl;
431  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
432  }
433 
434  {
435  vpImage<vpRGBa> Iinput, I;
436 
437  std::cout << "-- Test on color images" << std::endl;
438  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
439  }
440 
441  } catch (const vpException &e) {
442  std::cerr << e.what() << std::endl;
443  return EXIT_FAILURE;
444  }
445 
446  std::cout << "testKeyPoint-6 is ok !" << std::endl;
447  return EXIT_SUCCESS;
448 }
449 #else
450 #include <cstdlib>
451 
452 int main()
453 {
454  std::cerr << "You need OpenCV library." << std::endl;
455 
456  return EXIT_SUCCESS;
457 }
458 
459 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1292
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void set_uv(double u, double v)
Definition: vpImagePoint.h:248
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
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:126
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:179
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1537
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
Definition: vpKeyPoint.h:583
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:286
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:777
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
const char * what() const
void quarterSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:1331
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:243
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:222
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
Definition: vpKeyPoint.h:543
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:835
Definition of the vpImage class member functions.
Definition: vpImage.h:124
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)