Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testKeyPoint-5.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
44 
45 #include <visp3/core/vpImage.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/gui/vpDisplayX.h>
48 #include <visp3/gui/vpDisplayGTK.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayOpenCV.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/vision/vpKeyPoint.h>
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", name);
76 
77  fprintf(stdout, "\n\
78 OPTIONS: \n\
79 \n\
80  -c\n\
81  Disable the mouse click. Useful to automaze the \n\
82  execution of this program without humain intervention.\n\
83 \n\
84  -d \n\
85  Turn off the display.\n\
86 \n\
87  -h\n\
88  Print the help.\n");
89 
90  if (badparam)
91  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
92 }
93 
105 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
106 {
107  const char *optarg_;
108  int c;
109  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
110 
111  switch (c) {
112  case 'c': click_allowed = false; break;
113  case 'd': display = false; break;
114  case 'h': usage(argv[0], NULL); return false; break;
115 
116  default:
117  usage(argv[0], optarg_);
118  return false; break;
119  }
120  }
121 
122  if ((c == 1) || (c == -1)) {
123  // standalone param or error
124  usage(argv[0], NULL);
125  std::cerr << "ERROR: " << std::endl;
126  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
127  return false;
128  }
129 
130  return true;
131 }
132 
139 int main(int argc, const char ** argv) {
140  try {
141  std::string env_ipath;
142  bool opt_click_allowed = true;
143  bool opt_display = true;
144 
145  // Read the command line options
146  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
147  exit (EXIT_FAILURE);
148  }
149 
150  //Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
151  env_ipath = vpIoTools::getViSPImagesDataPath();
152 
153  if(env_ipath.empty()) {
154  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment variable value." << std::endl;
155  return EXIT_FAILURE;
156  }
157 
159 
160  //Set the path location of the image sequence
161  std::string dirname = vpIoTools::createFilePath(env_ipath, "ViSP-images/Klimt");
162 
163  //Build the name of the image files
164  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
165  vpImageIo::read(I, filename);
166 
167 #if defined VISP_HAVE_X11
168  vpDisplayX display;
169 #elif defined VISP_HAVE_GTK
170  vpDisplayGTK display;
171 #elif defined VISP_HAVE_GDI
172  vpDisplayGDI display;
173 #else
174  vpDisplayOpenCV display;
175 #endif
176 
177  if(opt_display) {
178  display.init(I, 0, 0, "KeyPoints detection.");
179  }
180 
181  //Here, we want to test feature detection on a pyramid of images even for features that
182  //are scale invariant to detect potential problem in ViSP.
183  std::cout << "INFORMATION: " << std::endl;
184  std::cout << "Here, we want to test feature detection on a pyramid of images even for features "
185  "that are scale invariant to detect potential problem in ViSP." << std::endl << std::endl;
186  vpKeyPoint keyPoints;
187 
188  //Will test the different types of keypoints detection to see if there is a problem
189  //between OpenCV versions, modules or constructors
190  std::vector<std::string> detectorNames;
191  detectorNames.push_back("PyramidFAST");
192  detectorNames.push_back("FAST");
193  detectorNames.push_back("PyramidMSER");
194  detectorNames.push_back("MSER");
195  detectorNames.push_back("PyramidGFTT");
196  detectorNames.push_back("GFTT");
197  detectorNames.push_back("PyramidSimpleBlob");
198  detectorNames.push_back("SimpleBlob");
199  //In contrib modules
200 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
201  detectorNames.push_back("PyramidSTAR");
202  detectorNames.push_back("STAR");
203 #endif
204 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
205  detectorNames.push_back("PyramidAGAST");
206  detectorNames.push_back("AGAST");
207 #endif
208  detectorNames.push_back("PyramidORB");
209  detectorNames.push_back("ORB");
210 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
211  detectorNames.push_back("PyramidBRISK");
212  detectorNames.push_back("BRISK");
213 #endif
214 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
215  detectorNames.push_back("PyramidKAZE");
216  detectorNames.push_back("KAZE");
217  detectorNames.push_back("PyramidAKAZE");
218  detectorNames.push_back("AKAZE");
219 #endif
220 
221 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
222  detectorNames.push_back("PyramidSIFT");
223  detectorNames.push_back("SIFT");
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::cerr << "No keypoints detected with " << *itd << " and image: " << filename << "." << std::endl;
237  return EXIT_FAILURE;
238  }
239 
240  if (opt_display) {
242 
243  for(std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
244  vpImagePoint imPt;
245  imPt.set_uv(it->pt.x, it->pt.y);
246 
248  }
249 
250  vpDisplay::flush(I);
251 
252  if (opt_click_allowed) {
254  }
255  }
256  }
257 
258  std::cout << "\n\n";
259 
260  std::map<vpKeyPoint::vpFeatureDetectorType, std::string> mapOfDetectorNames = keyPoints.getDetectorNames();
261  for (int i = 0; i < vpKeyPoint::DETECTOR_TYPE_SIZE; i++) {
263 
264  std::vector<cv::KeyPoint> kpts;
265 
266  keyPoints.detect(I, kpts);
267  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType) i] << " method." << std::endl;
268  if (kpts.empty()) {
269  std::cerr << "No keypoints detected with " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType) i] << " method and image: " << filename << "." << std::endl;
270  return EXIT_FAILURE;
271  }
272 
273  if (opt_display) {
275 
276  for(std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
277  vpImagePoint imPt;
278  imPt.set_uv(it->pt.x, it->pt.y);
279 
281  }
282 
283  vpDisplay::flush(I);
284 
285  if (opt_click_allowed) {
287  }
288  }
289  }
290 
291  } catch(vpException &e) {
292  std::cerr << e.what() << std::endl;
293  return EXIT_FAILURE;
294  }
295 
296  std::cout << "testKeyPoint-5 is ok !" << std::endl;
297  return EXIT_SUCCESS;
298 }
299 #else
300 #include <cstdlib>
301 
302 int main() {
303  std::cerr << "You need OpenCV library." << std::endl;
304 
305  return EXIT_SUCCESS;
306 }
307 
308 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
std::map< vpFeatureDetectorType, std::string > getDetectorNames() const
Definition: vpKeyPoint.h:459
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:76
static const vpColor red
Definition: vpColor.h:163
const char * what() const
vpFeatureDetectorType
Definition: vpKeyPoint.h:246
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
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:700
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
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:205
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:217
void set_uv(const double u, const double v)
Definition: vpImagePoint.h:243
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())