ViSP  3.0.0
testKeyPoint-6.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 descriptor computation.
32  *
33  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
37 
38 #include <iostream>
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
43 
44 #include <visp3/core/vpImage.h>
45 #include <visp3/io/vpImageIo.h>
46 #include <visp3/gui/vpDisplayX.h>
47 #include <visp3/gui/vpDisplayGTK.h>
48 #include <visp3/gui/vpDisplayGDI.h>
49 #include <visp3/gui/vpDisplayOpenCV.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/io/vpParseArgv.h>
52 #include <visp3/vision/vpKeyPoint.h>
53 
54 // List of allowed command line options
55 #define GETOPTARGS "cdh"
56 
57 void usage(const char *name, const char *badparam);
58 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
59 
68 void usage(const char *name, const char *badparam)
69 {
70  fprintf(stdout, "\n\
71 Test keypoint descriptor extraction.\n\
72 \n\
73 SYNOPSIS\n\
74  %s [-c] [-d] [-h]\n", name);
75 
76  fprintf(stdout, "\n\
77 OPTIONS: \n\
78 \n\
79  -c\n\
80  Disable the mouse click. Useful to automaze the \n\
81  execution of this program without humain intervention.\n\
82 \n\
83  -d \n\
84  Turn off the display.\n\
85 \n\
86  -h\n\
87  Print the help.\n");
88 
89  if (badparam)
90  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
91 }
92 
104 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
105 {
106  const char *optarg_;
107  int c;
108  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
109 
110  switch (c) {
111  case 'c': click_allowed = false; break;
112  case 'd': display = false; break;
113  case 'h': usage(argv[0], NULL); return false; break;
114 
115  default:
116  usage(argv[0], optarg_);
117  return false; break;
118  }
119  }
120 
121  if ((c == 1) || (c == -1)) {
122  // standalone param or error
123  usage(argv[0], NULL);
124  std::cerr << "ERROR: " << std::endl;
125  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
126  return false;
127  }
128 
129  return true;
130 }
131 
140 std::string getOpenCVType(const int type) {
141  std::string type_string = "";
142 
143  switch(type) {
144  case CV_8U:
145  type_string = "CV_8U";
146  break;
147 
148  case CV_8S:
149  type_string = "CV_8S";
150  break;
151 
152  case CV_16U:
153  type_string = "CV_16U";
154  break;
155 
156  case CV_16S:
157  type_string = "CV_16S";
158  break;
159 
160  case CV_32S:
161  type_string = "CV_32S";
162  break;
163 
164  case CV_32F:
165  type_string = "CV_32F";
166  break;
167 
168  case CV_64F:
169  type_string = "CV_64F";
170  break;
171 
172  default:
173  type_string = "Problem with type !";
174  break;
175  }
176 
177  return type_string;
178 }
179 
185 int main(int argc, const char ** argv) {
186  try {
187  std::string env_ipath;
188  bool opt_click_allowed = true;
189  bool opt_display = true;
190 
191  // Read the command line options
192  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
193  exit (-1);
194  }
195 
196  //Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
197  env_ipath = vpIoTools::getViSPImagesDataPath();
198 
199  if(env_ipath.empty()) {
200  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment variable value." << std::endl;
201  return -1;
202  }
203 
205 
206  //Set the path location of the image sequence
207  std::string dirname = vpIoTools::createFilePath(env_ipath, "ViSP-images/Klimt");
208 
209  //Build the name of the image files
210  std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
211  vpImageIo::read(I, filename);
212 
213 #if defined VISP_HAVE_X11
214  vpDisplayX display;
215 #elif defined VISP_HAVE_GTK
216  vpDisplayGTK display;
217 #elif defined VISP_HAVE_GDI
218  vpDisplayGDI display;
219 #else
220  vpDisplayOpenCV display;
221 #endif
222 
223  if(opt_display) {
224  display.init(I, 0, 0, "KeyPoints detection.");
225  }
226 
227  vpKeyPoint keyPoints;
228 
229  std::vector<std::string> descriptorNames;
230 #if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
231  descriptorNames.push_back("SIFT");
232  descriptorNames.push_back("SURF");
233 #endif
234  descriptorNames.push_back("ORB");
235 #if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
236  descriptorNames.push_back("BRISK");
237 #endif
238 #if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
239  descriptorNames.push_back("BRIEF");
240 #if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
241  descriptorNames.push_back("FREAK");
242 #endif
243 #endif
244 #if defined(VISP_HAVE_OPENCV_XFEATURES2D)
245  descriptorNames.push_back("DAISY");
246  descriptorNames.push_back("LATCH");
247 // descriptorNames.push_back("LUCID"); //Problem
248 #endif
249 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
250  descriptorNames.push_back("KAZE");
251  descriptorNames.push_back("AKAZE");
252 #endif
253 
254  std::string detectorName = "FAST";
255  keyPoints.setDetector(detectorName);
256  std::vector<cv::KeyPoint> kpts;
257  double elapsedTime;
258  keyPoints.detect(I, kpts, elapsedTime);
259  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
260  if(kpts.empty()) {
261  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
262  return -1;
263  }
264 
265  for(std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
266  if(*itd == "KAZE") {
267  detectorName = "KAZE";
268  keyPoints.setDetector(detectorName);
269  keyPoints.detect(I, kpts, elapsedTime);
270  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
271  if(kpts.empty()) {
272  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
273  return -1;
274  }
275  } else if(*itd == "AKAZE") {
276  detectorName = "AKAZE";
277  keyPoints.setDetector(detectorName);
278  keyPoints.detect(I, kpts, elapsedTime);
279  std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
280  if(kpts.empty()) {
281  std::cerr << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
282  return -1;
283  }
284  }
285 
286  keyPoints.setExtractor(*itd);
287 
288  double t = vpTime::measureTimeMs();
289  cv::Mat descriptor;
290  keyPoints.extract(I, kpts, descriptor, elapsedTime);
291  t = vpTime::measureTimeMs() - t;
292 
293  std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols << " (rows x cols) ; type=" <<
294  getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t << " ms." << std::endl;
295  if(descriptor.empty()) {
296  std::cerr << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
297  return -1;
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  } catch(vpException &e) {
319  std::cerr << e.what() << std::endl;
320  return -1;
321  }
322 
323  std::cout << "testKeyPoint-6 is ok !" << std::endl;
324  return 0;
325 }
326 #else
327 int main() {
328  std::cerr << "You need OpenCV library." << std::endl;
329 
330  return 0;
331 }
332 
333 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1091
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, double &elapsedTime, const vpRect &rectangle=vpRect())
void setExtractor(const std::string &extractorName)
Definition: vpKeyPoint.h:603
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Define the X11 console to display images.
Definition: vpDisplayX.h:148
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
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
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1265
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:213
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, double &elapsedTime, std::vector< cv::Point3f > *trainPoints=NULL)
void set_uv(const double u, const double v)
Definition: vpImagePoint.h:234
void setDetector(const std::string &detectorName)
Definition: vpKeyPoint.h:563
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274