Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testKeyPoint-6.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 descriptor computation.
32  */
33 
40 #include <iostream>
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D) && defined(HAVE_OPENCV_VIDEO)
45 
46 #include <visp3/core/vpImage.h>
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/gui/vpDisplayGDI.h>
49 #include <visp3/gui/vpDisplayGTK.h>
50 #include <visp3/gui/vpDisplayOpenCV.h>
51 #include <visp3/gui/vpDisplayX.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/vision/vpKeyPoint.h>
55 
56 // List of allowed command line options
57 #define GETOPTARGS "cdh"
58 
59 #ifdef ENABLE_VISP_NAMESPACE
60 using namespace VISP_NAMESPACE_NAME;
61 #endif
62 
63 void usage(const char *name, const char *badparam);
64 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
65 
72 void usage(const char *name, const char *badparam)
73 {
74  fprintf(stdout, "\n\
75 Test keypoint descriptor extraction.\n\
76 \n\
77 SYNOPSIS\n\
78  %s [-c] [-d] [-h]\n",
79  name);
80 
81  fprintf(stdout, "\n\
82 OPTIONS: \n\
83 \n\
84  -c\n\
85  Disable the mouse click. Useful to automate the \n\
86  execution of this program without human intervention.\n\
87 \n\
88  -d \n\
89  Turn off the display.\n\
90 \n\
91  -h\n\
92  Print the help.\n");
93 
94  if (badparam)
95  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96 }
97 
109 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
110 {
111  const char *optarg_;
112  int c;
113  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114 
115  switch (c) {
116  case 'c':
117  click_allowed = false;
118  break;
119  case 'd':
120  display = false;
121  break;
122  case 'h':
123  usage(argv[0], nullptr);
124  return false;
125  break;
126 
127  default:
128  usage(argv[0], optarg_);
129  return false;
130  break;
131  }
132  }
133 
134  if ((c == 1) || (c == -1)) {
135  // standalone param or error
136  usage(argv[0], nullptr);
137  std::cerr << "ERROR: " << std::endl;
138  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
153 std::string getOpenCVType(int type)
154 {
155  std::string type_string = "";
156 
157  switch (type) {
158  case CV_8U:
159  type_string = "CV_8U";
160  break;
161 
162  case CV_8S:
163  type_string = "CV_8S";
164  break;
165 
166  case CV_16U:
167  type_string = "CV_16U";
168  break;
169 
170  case CV_16S:
171  type_string = "CV_16S";
172  break;
173 
174  case CV_32S:
175  type_string = "CV_32S";
176  break;
177 
178  case CV_32F:
179  type_string = "CV_32F";
180  break;
181 
182  case CV_64F:
183  type_string = "CV_64F";
184  break;
185 
186  default:
187  type_string = "Problem with type !";
188  break;
189  }
190 
191  return type_string;
192 }
193 
194 template <typename Type>
195 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
196  vpImage<Type> &I)
197 {
198  // Set the path location of the image sequence
199  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
200 
201  // Build the name of the image files
202  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
203  vpImageIo::read(Iinput, filename);
204  Iinput.quarterSizeImage(I);
205 
206 #if defined(VISP_HAVE_X11)
207  vpDisplayX display;
208 #elif defined(VISP_HAVE_GTK)
209  vpDisplayGTK display;
210 #elif defined(VISP_HAVE_GDI)
211  vpDisplayGDI display;
212 #elif defined(HAVE_OPENCV_HIGHGUI)
213  vpDisplayOpenCV display;
214 #endif
215 
216  if (opt_display) {
217  display.init(I, 0, 0, "KeyPoints detection.");
218  }
219 
220  vpKeyPoint keyPoints;
221 
222  std::vector<std::string> descriptorNames;
223 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
224  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
225  descriptorNames.push_back("SIFT");
226 #endif
227 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
228  descriptorNames.push_back("SURF");
229 #endif
230  descriptorNames.push_back("ORB");
231 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
232  descriptorNames.push_back("BRISK");
233 #endif
234 #if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
235  descriptorNames.push_back("BRIEF");
236 #if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
237  descriptorNames.push_back("FREAK");
238 #endif
239 #endif
240 #if defined(VISP_HAVE_OPENCV_XFEATURES2D)
241  descriptorNames.push_back("DAISY");
242  descriptorNames.push_back("LATCH");
243 #endif
244 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
245  descriptorNames.push_back("VGG");
246  descriptorNames.push_back("BoostDesc");
247 #endif
248 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
249  descriptorNames.push_back("KAZE");
250  descriptorNames.push_back("AKAZE");
251 #endif
252 
253  std::string detectorName = "FAST";
254  keyPoints.setDetector(detectorName);
255  std::vector<cv::KeyPoint> kpts;
256 
257  keyPoints.detect(I, kpts);
258  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
259  if (kpts.empty()) {
260  std::stringstream ss;
261  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
262  throw(vpException(vpException::fatalError, ss.str()));
263  }
264 
265  for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
266  keyPoints.setExtractor(*itd);
267 
268  if (*itd == "KAZE") {
269  detectorName = "KAZE";
270  keyPoints.setDetector(detectorName);
271  keyPoints.detect(I, kpts);
272  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
273  if (kpts.empty()) {
274  std::stringstream ss;
275  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
276  throw(vpException(vpException::fatalError, ss.str()));
277  }
278  }
279  else if (*itd == "AKAZE") {
280  detectorName = "AKAZE";
281  keyPoints.setDetector(detectorName);
282  keyPoints.detect(I, kpts);
283  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
284  if (kpts.empty()) {
285  std::stringstream ss;
286  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
287  throw(vpException(vpException::fatalError, ss.str()));
288  }
289  }
290  else if (*itd == "BoostDesc") {
291 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
292  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
293  // Init BIN BOOST descriptor for FAST keypoints
294  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
295 #endif
296  }
297 
298  double t = vpTime::measureTimeMs();
299  cv::Mat descriptor;
300  keyPoints.extract(I, kpts, descriptor);
301  t = vpTime::measureTimeMs() - t;
302 
303  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
304  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
305  << " ms." << std::endl;
306  if (descriptor.empty()) {
307  std::stringstream ss;
308  ss << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
309  throw(vpException(vpException::fatalError, ss.str()));
310  }
311 
312  if (opt_display) {
314 
315  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
316  vpImagePoint imPt;
317  imPt.set_uv(it->pt.x, it->pt.y);
318 
320  }
321 
322  vpDisplay::flush(I);
323 
324  if (opt_click_allowed) {
326  }
327  }
328  }
329 
330  std::cout << "\n\n";
331 
332  std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
333 
334  for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
336 
337  if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
338  detectorName = "KAZE";
339  keyPoints.setDetector(detectorName);
340  keyPoints.detect(I, kpts);
341  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
342  if (kpts.empty()) {
343  std::stringstream ss;
344  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
345  throw(vpException(vpException::fatalError, ss.str()));
346  }
347  }
348  else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
349  detectorName = "AKAZE";
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::stringstream ss;
355  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
356  throw(vpException(vpException::fatalError, ss.str()));
357  }
358  }
359  else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
360 #if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
361  detectorName = "FAST";
362  keyPoints.setDetector(detectorName);
363  keyPoints.detect(I, kpts);
364  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
365  if (kpts.empty()) {
366  std::stringstream ss;
367  ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
368  throw(vpException(vpException::fatalError, ss.str()));
369  }
370 
371  cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
372  // Init BIN BOOST descriptor for FAST keypoints
373  boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
374 #endif
375  }
376 
377  double t = vpTime::measureTimeMs();
378  cv::Mat descriptor;
379  keyPoints.extract(I, kpts, descriptor);
380  t = vpTime::measureTimeMs() - t;
381 
382  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
383  << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
384  << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
385  << std::endl;
386  if (descriptor.empty()) {
387  std::stringstream ss;
388  ss << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
389  << " and image:" << filename << "." << std::endl;
390  throw(vpException(vpException::fatalError, ss.str()));
391  }
392 
393  if (opt_display) {
395 
396  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
397  vpImagePoint imPt;
398  imPt.set_uv(it->pt.x, it->pt.y);
399 
401  }
402 
403  vpDisplay::flush(I);
404 
405  if (opt_click_allowed) {
407  }
408  }
409  }
410 }
411 
412 int main(int argc, const char **argv)
413 {
414  try {
415  std::string env_ipath;
416  bool opt_click_allowed = true;
417  bool opt_display = true;
418 
419  // Read the command line options
420  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
421  exit(EXIT_FAILURE);
422  }
423 
424  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
425  // environment variable value
426  env_ipath = vpIoTools::getViSPImagesDataPath();
427 
428  if (env_ipath.empty()) {
429  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
430  "variable value."
431  << std::endl;
432  return EXIT_FAILURE;
433  }
434 
435  {
436  vpImage<unsigned char> Iinput, I;
437 
438  std::cout << "-- Test on gray level images" << std::endl;
439  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
440  }
441 
442  {
443  vpImage<vpRGBa> Iinput, I;
444 
445  std::cout << "-- Test on color images" << std::endl;
446  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
447  }
448 
449  }
450  catch (const vpException &e) {
451  std::cerr << e.what() << std::endl;
452  return EXIT_FAILURE;
453  }
454 
455  std::cout << "testKeyPoint-6 is ok !" << std::endl;
456  return EXIT_SUCCESS;
457 }
458 #else
459 #include <cstdlib>
460 
461 int main()
462 {
463  std::cerr << "You need OpenCV library." << std::endl;
464 
465  return EXIT_SUCCESS;
466 }
467 
468 #endif
static const vpColor red
Definition: vpColor.h:217
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 displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
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
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
void set_uv(double u, double v)
Definition: vpImagePoint.h:357
Definition of the vpImage class member functions.
Definition: vpImage.h:131
void quarterSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:791
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:221
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:1633
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, std::vector< cv::Point3f > *trainPoints=nullptr)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:975
vpFeatureDescriptorType
Definition: vpKeyPoint.h:291
@ DESCRIPTOR_TYPE_SIZE
Number of descriptors available.
Definition: vpKeyPoint.h:319
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
Definition: vpKeyPoint.h:1103
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1575
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
Definition: vpKeyPoint.h:1063
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT double measureTimeMs()