Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testKeyPoint-5.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 keypoints detection with OpenCV, specially the Pyramid implementation
33  * feature misssing in OpenCV 3.0.
34  *
35  * Authors:
36  * Souriya Trinh
37  *
38  *****************************************************************************/
39 
40 #include <iostream>
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
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 void usage(const char *name, const char *badparam);
60 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
61 
70 void usage(const char *name, const char *badparam)
71 {
72  fprintf(stdout, "\n\
73 Test keypoints detection.\n\
74 \n\
75 SYNOPSIS\n\
76  %s [-c] [-d] [-h]\n", name);
77 
78  fprintf(stdout, "\n\
79 OPTIONS: \n\
80 \n\
81  -c\n\
82  Disable the mouse click. Useful to automaze the \n\
83  execution of this program without humain 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], NULL);
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], NULL);
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,
144  vpImage<Type> &Iinput, 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
155  vpDisplayX display;
156 #elif defined VISP_HAVE_GTK
157  vpDisplayGTK display;
158 #elif defined VISP_HAVE_GDI
159  vpDisplayGDI display;
160 #else
161  vpDisplayOpenCV display;
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 "
172  "even for features "
173  "that are scale invariant to detect potential problem in ViSP."
174  << 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  detectorNames.push_back("PyramidFAST");
182  detectorNames.push_back("FAST");
183  detectorNames.push_back("PyramidMSER");
184  detectorNames.push_back("MSER");
185  detectorNames.push_back("PyramidGFTT");
186  detectorNames.push_back("GFTT");
187  detectorNames.push_back("PyramidSimpleBlob");
188  detectorNames.push_back("SimpleBlob");
189 // In contrib modules
190 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
191  detectorNames.push_back("PyramidSTAR");
192  detectorNames.push_back("STAR");
193 #endif
194 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
195  detectorNames.push_back("PyramidAGAST");
196  detectorNames.push_back("AGAST");
197 #endif
198  detectorNames.push_back("PyramidORB");
199  detectorNames.push_back("ORB");
200 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
201  detectorNames.push_back("PyramidBRISK");
202  detectorNames.push_back("BRISK");
203 #endif
204 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
205  detectorNames.push_back("PyramidKAZE");
206  detectorNames.push_back("KAZE");
207  detectorNames.push_back("PyramidAKAZE");
208  detectorNames.push_back("AKAZE");
209 #endif
210 
211 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
212  detectorNames.push_back("PyramidSIFT");
213  detectorNames.push_back("SIFT");
214  detectorNames.push_back("PyramidSURF");
215  detectorNames.push_back("SURF");
216 #endif
217 
218  for (std::vector<std::string>::const_iterator itd = detectorNames.begin(); itd != detectorNames.end(); ++itd) {
219  keyPoints.setDetector(*itd);
220 
221  std::vector<cv::KeyPoint> kpts;
222 
223  keyPoints.detect(I, kpts);
224  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << *itd << " method." << std::endl;
225  if (kpts.empty()) {
226  std::stringstream ss;
227  ss << "No keypoints detected with " << *itd << " and image: " << filename << "." << std::endl;
228  throw(vpException(vpException::fatalError, ss.str()));
229  }
230 
231  if (opt_display) {
233 
234  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
235  vpImagePoint imPt;
236  imPt.set_uv(it->pt.x, it->pt.y);
237 
239  }
240 
241  vpDisplay::flush(I);
242 
243  if (opt_click_allowed) {
245  }
246  }
247  }
248 
249  std::cout << "\n\n";
250 
251  std::map<vpKeyPoint::vpFeatureDetectorType, std::string> mapOfDetectorNames = keyPoints.getDetectorNames();
252  for (int i = 0; i < vpKeyPoint::DETECTOR_TYPE_SIZE; i++) {
254 
255  std::vector<cv::KeyPoint> kpts;
256 
257  keyPoints.detect(I, kpts);
258  std::cout << "Nb keypoints detected: " << kpts.size() << " for "
259  << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i] << " method." << std::endl;
260  if (kpts.empty()) {
261  std::stringstream ss;
262  ss << "No keypoints detected with " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i]
263  << " method and image: " << filename << "." << std::endl;
264  throw(vpException(vpException::fatalError, ss.str()));
265  }
266 
267  if (opt_display) {
269 
270  for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
271  vpImagePoint imPt;
272  imPt.set_uv(it->pt.x, it->pt.y);
273 
275  }
276 
277  vpDisplay::flush(I);
278 
279  if (opt_click_allowed) {
281  }
282  }
283  }
284 }
285 
292 int main(int argc, const char **argv)
293 {
294  try {
295  std::string env_ipath;
296  bool opt_click_allowed = true;
297  bool opt_display = true;
298 
299  // Read the command line options
300  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
301  exit(EXIT_FAILURE);
302  }
303 
304  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
305  // environment variable value
306  env_ipath = vpIoTools::getViSPImagesDataPath();
307 
308  if (env_ipath.empty()) {
309  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
310  "variable value."
311  << std::endl;
312  return EXIT_FAILURE;
313  }
314 
315  {
316  vpImage<unsigned char> Iinput, I;
317 
318  std::cout << "-- Test on gray level images" << std::endl;
319  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
320  }
321 
322  {
323  vpImage<vpRGBa> Iinput, I;
324 
325  std::cout << "-- Test on color images" << std::endl;
326  run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
327  }
328 
329  } catch (const vpException &e) {
330  std::cerr << e.what() << std::endl;
331  return EXIT_FAILURE;
332  }
333 
334  std::cout << "testKeyPoint-5 is ok !" << std::endl;
335  return EXIT_SUCCESS;
336 }
337 #else
338 #include <cstdlib>
339 
340 int main()
341 {
342  std::cerr << "You need OpenCV library." << std::endl;
343 
344  return EXIT_SUCCESS;
345 }
346 
347 #endif
void halfSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:1271
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
std::map< vpFeatureDetectorType, std::string > getDetectorNames() const
Definition: vpKeyPoint.h:527
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)
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
vpFeatureDetectorType
Definition: vpKeyPoint.h:258
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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
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
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
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())