Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
testKeyPoint-5.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 
35 #include <iostream>
36 
37 #include <visp3/core/vpConfig.h>
38 
39 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D) && defined(HAVE_OPENCV_VIDEO)
40 
41 #include <visp3/core/vpImage.h>
42 #include <visp3/core/vpIoTools.h>
43 #include <visp3/gui/vpDisplayGDI.h>
44 #include <visp3/gui/vpDisplayGTK.h>
45 #include <visp3/gui/vpDisplayOpenCV.h>
46 #include <visp3/gui/vpDisplayX.h>
47 #include <visp3/io/vpImageIo.h>
48 #include <visp3/io/vpParseArgv.h>
49 #include <visp3/vision/vpKeyPoint.h>
50 
51 
52 
53 
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 
69 void usage(const char *name, const char *badparam)
70 {
71  fprintf(stdout, "\n\
72 Test keypoints detection.\n\
73 \n\
74 SYNOPSIS\n\
75  %s [-c] [-d] [-h]\n",
76  name);
77 
78  fprintf(stdout, "\n\
79 OPTIONS: \n\
80 \n\
81  -c\n\
82  Disable the mouse click. Useful to automate the \n\
83  execution of this program without human intervention.\n\
84 \n\
85  -d \n\
86  Turn off the display.\n\
87 \n\
88  -h\n\
89  Print the help.\n");
90 
91  if (badparam)
92  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
93 }
94 
106 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'c':
114  click_allowed = false;
115  break;
116  case 'd':
117  display = false;
118  break;
119  case 'h':
120  usage(argv[0], nullptr);
121  return false;
122  break;
123 
124  default:
125  usage(argv[0], optarg_);
126  return false;
127  break;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], nullptr);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 template <typename Type>
143 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
144  vpImage<Type> &I)
145 {
146  // Set the path location of the image sequence
147  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
148 
149  // Build the name of the image files
150  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
151  vpImageIo::read(Iinput, filename);
152  Iinput.halfSizeImage(I);
153 
154 #if defined(VISP_HAVE_X11)
156 #elif defined(VISP_HAVE_GTK)
158 #elif defined(VISP_HAVE_GDI)
160 #elif defined(HAVE_OPENCV_HIGHGUI)
162 #endif
163 
164  if (opt_display) {
165  display.init(I, 0, 0, "KeyPoints detection.");
166  }
167 
168  // Here, we want to test feature detection on a pyramid of images even for
169  // features that are scale invariant to detect potential problem in ViSP.
170  std::cout << "INFORMATION: " << std::endl;
171  std::cout << "Here, we want to test feature detection on a pyramid of images even for features "
172  "that are scale invariant to detect potential problem in ViSP."
173  << std::endl
174  << std::endl;
175  vpKeyPoint keyPoints;
176 
177  // Will test the different types of keypoints detection to see if there is
178  // a problem between OpenCV versions, modules or constructors
179  std::vector<std::string> detectorNames;
180  detectorNames.push_back("PyramidFAST");
181  detectorNames.push_back("FAST");
182  detectorNames.push_back("PyramidMSER");
183  detectorNames.push_back("MSER");
184  detectorNames.push_back("PyramidGFTT");
185  detectorNames.push_back("GFTT");
186  detectorNames.push_back("PyramidSimpleBlob");
187  detectorNames.push_back("SimpleBlob");
188 // In contrib modules
189 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
190  detectorNames.push_back("PyramidSTAR");
191  detectorNames.push_back("STAR");
192 #endif
193 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
194  detectorNames.push_back("PyramidAGAST");
195  detectorNames.push_back("AGAST");
196 #endif
197  detectorNames.push_back("PyramidORB");
198  detectorNames.push_back("ORB");
199 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
200  detectorNames.push_back("PyramidBRISK");
201  detectorNames.push_back("BRISK");
202 #endif
203 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
204  detectorNames.push_back("PyramidKAZE");
205  detectorNames.push_back("KAZE");
206  detectorNames.push_back("PyramidAKAZE");
207  detectorNames.push_back("AKAZE");
208 #endif
209 
210 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
211  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
212 #if (VISP_HAVE_OPENCV_VERSION != 0x040504) && (VISP_HAVE_OPENCV_VERSION != 0x040505) && \
213  (VISP_HAVE_OPENCV_VERSION != 0x040600) && (VISP_HAVE_OPENCV_VERSION != 0x040700) && \
214  (VISP_HAVE_OPENCV_VERSION != 0x040900) && \
215  (defined(__APPLE__) && defined(__MACH__))
216  // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
217  // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
218  // installed with brew. It seems working when OpenCV is build from source
219  detectorNames.push_back("PyramidSIFT");
220  detectorNames.push_back("SIFT");
221 #endif
222 #endif
223 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
224  detectorNames.push_back("PyramidSURF");
225  detectorNames.push_back("SURF");
226 #endif
227 
228  for (std::vector<std::string>::const_iterator itd = detectorNames.begin(); itd != detectorNames.end(); ++itd) {
229  keyPoints.setDetector(*itd);
230 
231  std::vector<cv::KeyPoint> kpts;
232 
233  keyPoints.detect(I, kpts);
234  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << *itd << " method." << std::endl;
235  if (kpts.empty()) {
236  std::stringstream ss;
237  ss << "No keypoints detected with " << *itd << " and image: " << filename << "." << std::endl;
238  throw(vpException(vpException::fatalError, ss.str()));
239  }
240 
241  if (opt_display) {
243 
244  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
245  vpImagePoint imPt;
246  imPt.set_uv(it->pt.x, it->pt.y);
247 
249  }
250 
251  vpDisplay::flush(I);
252 
253  if (opt_click_allowed) {
255  }
256  }
257  }
258 
259  std::cout << "\n\n";
260 
261  std::map<vpKeyPoint::vpFeatureDetectorType, std::string> mapOfDetectorNames = keyPoints.getDetectorNames();
262  for (int i = 0; i < vpKeyPoint::DETECTOR_TYPE_SIZE; i++) {
263 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
264  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
265 #if ((VISP_HAVE_OPENCV_VERSION == 0x040504) || (VISP_HAVE_OPENCV_VERSION == 0x040505) || \
266  (VISP_HAVE_OPENCV_VERSION == 0x040600) || (VISP_HAVE_OPENCV_VERSION == 0x040700) || \
267  (VISP_HAVE_OPENCV_VERSION == 0x040900)) && \
268  (defined(__APPLE__) && defined(__MACH__))
269  // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
270  // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
271  // installed with brew. It seems working when OpenCV is build from source
272  if (i == vpKeyPoint::DETECTOR_SIFT) {
273  continue;
274  }
275 #endif
276 #endif
278 
279  std::vector<cv::KeyPoint> kpts;
280 
281  keyPoints.detect(I, kpts);
282  std::cout << "Nb keypoints detected: " << kpts.size() << " for "
283  << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i] << " method." << std::endl;
284  if (kpts.empty()) {
285  std::stringstream ss;
286  ss << "No keypoints detected with " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i]
287  << " method and image: " << filename << "." << std::endl;
288  throw(vpException(vpException::fatalError, ss.str()));
289  }
290 
291  if (opt_display) {
293 
294  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
295  vpImagePoint imPt;
296  imPt.set_uv(it->pt.x, it->pt.y);
297 
299  }
300 
301  vpDisplay::flush(I);
302 
303  if (opt_click_allowed) {
305  }
306  }
307  }
308 }
309 
316 int main(int argc, const char **argv)
317 {
318  try {
319  std::string env_ipath;
320  bool opt_click_allowed = true;
321  bool opt_display = true;
322 
323  // Read the command line options
324  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
325  exit(EXIT_FAILURE);
326  }
327 
328  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
329  // environment variable value
330  env_ipath = vpIoTools::getViSPImagesDataPath();
331 
332  if (env_ipath.empty()) {
333  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
334  "variable value."
335  << std::endl;
336  return EXIT_FAILURE;
337  }
338 
339  {
340  vpImage<unsigned char> Iinput, I;
341 
342  std::cout << "-- Test on gray level images" << std::endl;
343  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
344  }
345 
346  {
347  vpImage<vpRGBa> Iinput, I;
348 
349  std::cout << "-- Test on color images" << std::endl;
350  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
351  }
352 
353  }
354  catch (const vpException &e) {
355  std::cerr << e.what() << std::endl;
356  return EXIT_FAILURE;
357  }
358 
359  std::cout << "testKeyPoint-5 is ok !" << std::endl;
360  return EXIT_SUCCESS;
361 }
362 #else
363 #include <cstdlib>
364 
365 int main()
366 {
367  std::cerr << "You need OpenCV library." << std::endl;
368 
369  return EXIT_SUCCESS;
370 }
371 
372 #endif
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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:59
@ fatalError
Fatal error.
Definition: vpException.h:84
const char * what() const
Definition: vpException.cpp:70
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
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:352
Definition of the vpImage class member functions.
Definition: vpImage.h:69
void halfSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:1610
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1781
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2142
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:212
vpFeatureDetectorType
Definition: vpKeyPoint.h:250
@ DETECTOR_TYPE_SIZE
Number of detectors available.
Definition: vpKeyPoint.h:277
@ DETECTOR_SIFT
SIFT detector.
Definition: vpKeyPoint.h:263
std::map< vpFeatureDetectorType, std::string > getDetectorNames() const
Definition: vpKeyPoint.h:1038
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:970
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:1566
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.