Visual Servoing Platform  version 3.6.1 under development (2025-01-17)
testKeyPoint-5.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 keypoints detection with OpenCV, specially the Pyramid implementation
32  * feature misssing in OpenCV 3.0.
33  */
34 
42 #include <iostream>
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && \
47  ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
48  ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES))
49 
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/gui/vpDisplayFactory.h>
53 #include <visp3/io/vpImageIo.h>
54 #include <visp3/io/vpParseArgv.h>
55 #include <visp3/vision/vpKeyPoint.h>
56 
57 // List of allowed command line options
58 #define GETOPTARGS "cdh"
59 
60 #ifdef ENABLE_VISP_NAMESPACE
61 using namespace VISP_NAMESPACE_NAME;
62 #endif
63 
64 void usage(const char *name, const char *badparam);
65 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
66 
75 void usage(const char *name, const char *badparam)
76 {
77  fprintf(stdout, "\n\
78 Test keypoints detection.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-c] [-d] [-h]\n",
82  name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: \n\
86 \n\
87  -c\n\
88  Disable the mouse click. Useful to automate the \n\
89  execution of this program without human intervention.\n\
90 \n\
91  -d \n\
92  Turn off the display.\n\
93 \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam)
98  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99 }
100 
112 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
113 {
114  const char *optarg_;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117 
118  switch (c) {
119  case 'c':
120  click_allowed = false;
121  break;
122  case 'd':
123  display = false;
124  break;
125  case 'h':
126  usage(argv[0], nullptr);
127  return false;
128  break;
129 
130  default:
131  usage(argv[0], optarg_);
132  return false;
133  break;
134  }
135  }
136 
137  if ((c == 1) || (c == -1)) {
138  // standalone param or error
139  usage(argv[0], nullptr);
140  std::cerr << "ERROR: " << std::endl;
141  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
142  return false;
143  }
144 
145  return true;
146 }
147 
148 template <typename Type>
149 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
150  vpImage<Type> &I)
151 {
152  // Set the path location of the image sequence
153  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
154 
155  // Build the name of the image files
156  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
157  vpImageIo::read(Iinput, filename);
158  Iinput.halfSizeImage(I);
159 
160  vpDisplay *display = nullptr;
161 
162  if (opt_display) {
163 #ifdef VISP_HAVE_DISPLAY
164  display = vpDisplayFactory::allocateDisplay(I, 0, 0, "KeyPoints detection.");
165 #else
166  std::cout << "No image viewer is available..." << std::endl;
167 #endif
168  }
169 
170  // Here, we want to test feature detection on a pyramid of images even for
171  // features that are scale invariant to detect potential problem in ViSP.
172  std::cout << "INFORMATION: " << std::endl;
173  std::cout << "Here, we want to test feature detection on a pyramid of images even for features "
174  << "that are scale invariant to detect potential problem in ViSP." << std::endl
175  << std::endl;
176  vpKeyPoint keyPoints;
177 
178  // Will test the different types of keypoints detection to see if there is
179  // a problem between OpenCV versions, modules or constructors
180  std::vector<std::string> detectorNames;
181 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
182  detectorNames.push_back("PyramidFAST");
183  detectorNames.push_back("FAST");
184 #endif
185 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
186  detectorNames.push_back("PyramidMSER");
187  detectorNames.push_back("MSER");
188  detectorNames.push_back("PyramidGFTT");
189  detectorNames.push_back("GFTT");
190  detectorNames.push_back("PyramidSimpleBlob");
191  detectorNames.push_back("SimpleBlob");
192 #endif
193 
194  // In contrib modules
195 #if defined(HAVE_OPENCV_XFEATURES2D)
196  detectorNames.push_back("PyramidSTAR");
197  detectorNames.push_back("STAR");
198 #endif
199 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
200  detectorNames.push_back("PyramidORB");
201  detectorNames.push_back("ORB");
202 #endif
203 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XFEATURES2D))
204 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
205  detectorNames.push_back("PyramidAGAST");
206  detectorNames.push_back("AGAST");
207 #endif
208  detectorNames.push_back("PyramidBRISK");
209  detectorNames.push_back("BRISK");
210 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
211  detectorNames.push_back("PyramidKAZE");
212  detectorNames.push_back("KAZE");
213  detectorNames.push_back("PyramidAKAZE");
214  detectorNames.push_back("AKAZE");
215 #endif
216 #endif
217 
218 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
219 #if (VISP_HAVE_OPENCV_VERSION != 0x040504) && (VISP_HAVE_OPENCV_VERSION != 0x040505) && \
220  (VISP_HAVE_OPENCV_VERSION != 0x040600) && (VISP_HAVE_OPENCV_VERSION != 0x040700) && \
221  (VISP_HAVE_OPENCV_VERSION != 0x040900) && (VISP_HAVE_OPENCV_VERSION != 0x040A00) && \
222  (defined(__APPLE__) && defined(__MACH__))
223  // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
224  // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
225  // installed with brew. It seems working when OpenCV is build from source
226  std::cout << "-- Add SIFT detector" << std::endl;
227  detectorNames.push_back("PyramidSIFT");
228  detectorNames.push_back("SIFT");
229 #endif
230 #endif
231 #if defined(OPENCV_ENABLE_NONFREE) && defined(HAVE_OPENCV_XFEATURES2D)
232  detectorNames.push_back("PyramidSURF");
233  detectorNames.push_back("SURF");
234 #endif
235 
236  for (std::vector<std::string>::const_iterator itd = detectorNames.begin(); itd != detectorNames.end(); ++itd) {
237  keyPoints.setDetector(*itd);
238 
239  std::vector<cv::KeyPoint> kpts;
240 
241  keyPoints.detect(I, kpts);
242  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << *itd << " method." << std::endl;
243  if (kpts.empty()) {
244  std::stringstream ss;
245  ss << "No keypoints detected with " << *itd << " and image: " << filename << "." << std::endl;
246  throw(vpException(vpException::fatalError, ss.str()));
247  }
248 
249  if (opt_display) {
251 
252  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
253  vpImagePoint imPt;
254  imPt.set_uv(it->pt.x, it->pt.y);
255 
257  }
258 
259  vpDisplay::flush(I);
260 
261  if (opt_click_allowed) {
263  }
264  }
265  }
266 
267  std::cout << "\n\n";
268 
269  std::map<vpKeyPoint::vpFeatureDetectorType, std::string> mapOfDetectorNames = keyPoints.getDetectorNames();
270  for (int i = 0; i < vpKeyPoint::DETECTOR_TYPE_SIZE; i++) {
271 #if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
272 #if ((VISP_HAVE_OPENCV_VERSION == 0x040504) || (VISP_HAVE_OPENCV_VERSION == 0x040505) || \
273  (VISP_HAVE_OPENCV_VERSION == 0x040600) || (VISP_HAVE_OPENCV_VERSION == 0x040700) || \
274  (VISP_HAVE_OPENCV_VERSION == 0x040900) || (VISP_HAVE_OPENCV_VERSION == 0x040A00)) && \
275  (defined(__APPLE__) && defined(__MACH__))
276  // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
277  // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
278  // installed with brew. It seems working when OpenCV is build from source
279  if (i == vpKeyPoint::DETECTOR_SIFT) {
280  std::cout << "-- Skip SIFT detector" << std::endl;
281  continue;
282  }
283 #endif
284 #endif
286 
287  std::vector<cv::KeyPoint> kpts;
288 
289  keyPoints.detect(I, kpts);
290  std::cout << "Nb keypoints detected: " << kpts.size() << " for "
291  << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i] << " method." << std::endl;
292  if (kpts.empty()) {
293  std::stringstream ss;
294  ss << "No keypoints detected with " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i]
295  << " method and image: " << filename << "." << std::endl;
296  throw(vpException(vpException::fatalError, ss.str()));
297  }
298 
299  if (opt_display) {
301 
302  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
303  vpImagePoint imPt;
304  imPt.set_uv(it->pt.x, it->pt.y);
305 
307  }
308 
309  vpDisplay::flush(I);
310 
311  if (opt_click_allowed) {
313  }
314  }
315  }
316 
317  if (display) {
318  delete display;
319  }
320 }
321 
322 int main(int argc, const char **argv)
323 {
324  try {
325  std::string env_ipath;
326  bool opt_click_allowed = true;
327  bool opt_display = true;
328 
329  // Read the command line options
330  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
331  exit(EXIT_FAILURE);
332  }
333 
334  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
335  // environment variable value
336  env_ipath = vpIoTools::getViSPImagesDataPath();
337 
338  if (env_ipath.empty()) {
339  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
340  "variable value."
341  << std::endl;
342  return EXIT_FAILURE;
343  }
344 
345  {
346  vpImage<unsigned char> Iinput, I;
347 
348  std::cout << "-- Test on gray level images" << std::endl;
349  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
350  }
351 
352  {
353  vpImage<vpRGBa> Iinput, I;
354 
355  std::cout << "-- Test on color images" << std::endl;
356  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
357  }
358 
359  }
360  catch (const vpException &e) {
361  std::cerr << e.what() << std::endl;
362  return EXIT_FAILURE;
363  }
364 
365  std::cout << "testKeyPoint-5 is ok !" << std::endl;
366  return EXIT_SUCCESS;
367 }
368 #else
369 #include <cstdlib>
370 
371 int main()
372 {
373  std::cerr << "You need OpenCV library." << std::endl;
374 
375  return EXIT_SUCCESS;
376 }
377 
378 #endif
static const vpColor red
Definition: vpColor.h:217
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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 halfSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:725
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 2D features detection (and descriptors extraction) and matching thanks to...
Definition: vpKeyPoint.h:267
vpFeatureDetectorType
Definition: vpKeyPoint.h:305
@ DETECTOR_TYPE_SIZE
Number of detectors available.
Definition: vpKeyPoint.h:352
@ DETECTOR_SIFT
SIFT detector.
Definition: vpKeyPoint.h:344
std::map< vpFeatureDetectorType, std::string > getDetectorNames() const
Definition: vpKeyPoint.h:1127
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:989
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1651
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.