Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D) && defined(HAVE_OPENCV_VIDEO)
47 
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayGTK.h>
52 #include <visp3/gui/vpDisplayOpenCV.h>
53 #include <visp3/gui/vpDisplayX.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/vision/vpKeyPoint.h>
57 
58 // List of allowed command line options
59 #define GETOPTARGS "cdh"
60 
61 #ifdef ENABLE_VISP_NAMESPACE
62 using namespace VISP_NAMESPACE_NAME;
63 #endif
64 
65 void usage(const char *name, const char *badparam);
66 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
67 
76 void usage(const char *name, const char *badparam)
77 {
78  fprintf(stdout, "\n\
79 Test keypoints detection.\n\
80 \n\
81 SYNOPSIS\n\
82  %s [-c] [-d] [-h]\n",
83  name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: \n\
87 \n\
88  -c\n\
89  Disable the mouse click. Useful to automate the \n\
90  execution of this program without human intervention.\n\
91 \n\
92  -d \n\
93  Turn off the display.\n\
94 \n\
95  -h\n\
96  Print the help.\n");
97 
98  if (badparam)
99  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
100 }
101 
113 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
114 {
115  const char *optarg_;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118 
119  switch (c) {
120  case 'c':
121  click_allowed = false;
122  break;
123  case 'd':
124  display = false;
125  break;
126  case 'h':
127  usage(argv[0], nullptr);
128  return false;
129  break;
130 
131  default:
132  usage(argv[0], optarg_);
133  return false;
134  break;
135  }
136  }
137 
138  if ((c == 1) || (c == -1)) {
139  // standalone param or error
140  usage(argv[0], nullptr);
141  std::cerr << "ERROR: " << std::endl;
142  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
143  return false;
144  }
145 
146  return true;
147 }
148 
149 template <typename Type>
150 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
151  vpImage<Type> &I)
152 {
153  // Set the path location of the image sequence
154  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
155 
156  // Build the name of the image files
157  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
158  vpImageIo::read(Iinput, filename);
159  Iinput.halfSizeImage(I);
160 
161 #if defined(VISP_HAVE_X11)
162  vpDisplayX display;
163 #elif defined(VISP_HAVE_GTK)
164  vpDisplayGTK display;
165 #elif defined(VISP_HAVE_GDI)
166  vpDisplayGDI display;
167 #elif defined(HAVE_OPENCV_HIGHGUI)
168  vpDisplayOpenCV display;
169 #endif
170 
171  if (opt_display) {
172  display.init(I, 0, 0, "KeyPoints detection.");
173  }
174 
175  // Here, we want to test feature detection on a pyramid of images even for
176  // features that are scale invariant to detect potential problem in ViSP.
177  std::cout << "INFORMATION: " << std::endl;
178  std::cout << "Here, we want to test feature detection on a pyramid of images even for features "
179  "that are scale invariant to detect potential problem in ViSP."
180  << std::endl
181  << std::endl;
182  vpKeyPoint keyPoints;
183 
184  // Will test the different types of keypoints detection to see if there is
185  // a problem between OpenCV versions, modules or constructors
186  std::vector<std::string> detectorNames;
187  detectorNames.push_back("PyramidFAST");
188  detectorNames.push_back("FAST");
189  detectorNames.push_back("PyramidMSER");
190  detectorNames.push_back("MSER");
191  detectorNames.push_back("PyramidGFTT");
192  detectorNames.push_back("GFTT");
193  detectorNames.push_back("PyramidSimpleBlob");
194  detectorNames.push_back("SimpleBlob");
195 // In contrib modules
196 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
197  detectorNames.push_back("PyramidSTAR");
198  detectorNames.push_back("STAR");
199 #endif
200 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
201  detectorNames.push_back("PyramidAGAST");
202  detectorNames.push_back("AGAST");
203 #endif
204  detectorNames.push_back("PyramidORB");
205  detectorNames.push_back("ORB");
206 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
207  detectorNames.push_back("PyramidBRISK");
208  detectorNames.push_back("BRISK");
209 #endif
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 
217 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
218  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
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(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_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 defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
272  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
273 #if ((VISP_HAVE_OPENCV_VERSION == 0x040504) || (VISP_HAVE_OPENCV_VERSION == 0x040505) || \
274  (VISP_HAVE_OPENCV_VERSION == 0x040600) || (VISP_HAVE_OPENCV_VERSION == 0x040700) || \
275  (VISP_HAVE_OPENCV_VERSION == 0x040900) || (VISP_HAVE_OPENCV_VERSION == 0x040A00)) && \
276  (defined(__APPLE__) && defined(__MACH__))
277  // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
278  // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
279  // installed with brew. It seems working when OpenCV is build from source
280  if (i == vpKeyPoint::DETECTOR_SIFT) {
281  std::cout << "-- Skip SIFT detector" << std::endl;
282  continue;
283  }
284 #endif
285 #endif
287 
288  std::vector<cv::KeyPoint> kpts;
289 
290  keyPoints.detect(I, kpts);
291  std::cout << "Nb keypoints detected: " << kpts.size() << " for "
292  << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i] << " method." << std::endl;
293  if (kpts.empty()) {
294  std::stringstream ss;
295  ss << "No keypoints detected with " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i]
296  << " method and image: " << filename << "." << std::endl;
297  throw(vpException(vpException::fatalError, ss.str()));
298  }
299 
300  if (opt_display) {
302 
303  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
304  vpImagePoint imPt;
305  imPt.set_uv(it->pt.x, it->pt.y);
306 
308  }
309 
310  vpDisplay::flush(I);
311 
312  if (opt_click_allowed) {
314  }
315  }
316  }
317 }
318 
319 int main(int argc, const char **argv)
320 {
321  try {
322  std::string env_ipath;
323  bool opt_click_allowed = true;
324  bool opt_display = true;
325 
326  // Read the command line options
327  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
328  exit(EXIT_FAILURE);
329  }
330 
331  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
332  // environment variable value
333  env_ipath = vpIoTools::getViSPImagesDataPath();
334 
335  if (env_ipath.empty()) {
336  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
337  "variable value."
338  << std::endl;
339  return EXIT_FAILURE;
340  }
341 
342  {
343  vpImage<unsigned char> Iinput, I;
344 
345  std::cout << "-- Test on gray level images" << std::endl;
346  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
347  }
348 
349  {
350  vpImage<vpRGBa> Iinput, I;
351 
352  std::cout << "-- Test on color images" << std::endl;
353  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
354  }
355 
356  }
357  catch (const vpException &e) {
358  std::cerr << e.what() << std::endl;
359  return EXIT_FAILURE;
360  }
361 
362  std::cout << "testKeyPoint-5 is ok !" << std::endl;
363  return EXIT_SUCCESS;
364 }
365 #else
366 #include <cstdlib>
367 
368 int main()
369 {
370  std::cerr << "You need OpenCV library." << std::endl;
371 
372  return EXIT_SUCCESS;
373 }
374 
375 #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 halfSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:723
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
vpFeatureDetectorType
Definition: vpKeyPoint.h:259
@ DETECTOR_TYPE_SIZE
Number of detectors available.
Definition: vpKeyPoint.h:286
@ DETECTOR_SIFT
SIFT detector.
Definition: vpKeyPoint.h:272
std::map< vpFeatureDetectorType, std::string > getDetectorNames() const
Definition: vpKeyPoint.h:1047
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:975
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1575
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70