Visual Servoing Platform version 3.5.0
testKeyPoint-6.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 descriptor computation.
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/core/vpIoTools.h>
47#include <visp3/gui/vpDisplayGDI.h>
48#include <visp3/gui/vpDisplayGTK.h>
49#include <visp3/gui/vpDisplayOpenCV.h>
50#include <visp3/gui/vpDisplayX.h>
51#include <visp3/io/vpImageIo.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
58void usage(const char *name, const char *badparam);
59bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
60
67void usage(const char *name, const char *badparam)
68{
69 fprintf(stdout, "\n\
70Test keypoint descriptor extraction.\n\
71\n\
72SYNOPSIS\n\
73 %s [-c] [-d] [-h]\n", name);
74
75 fprintf(stdout, "\n\
76OPTIONS: \n\
77\n\
78 -c\n\
79 Disable the mouse click. Useful to automaze the \n\
80 execution of this program without humain intervention.\n\
81\n\
82 -d \n\
83 Turn off the display.\n\
84\n\
85 -h\n\
86 Print the help.\n");
87
88 if (badparam)
89 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
90}
91
103bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
104{
105 const char *optarg_;
106 int c;
107 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
108
109 switch (c) {
110 case 'c':
111 click_allowed = false;
112 break;
113 case 'd':
114 display = false;
115 break;
116 case 'h':
117 usage(argv[0], NULL);
118 return false;
119 break;
120
121 default:
122 usage(argv[0], optarg_);
123 return false;
124 break;
125 }
126 }
127
128 if ((c == 1) || (c == -1)) {
129 // standalone param or error
130 usage(argv[0], NULL);
131 std::cerr << "ERROR: " << std::endl;
132 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133 return false;
134 }
135
136 return true;
137}
138
147std::string getOpenCVType(int type)
148{
149 std::string type_string = "";
150
151 switch (type) {
152 case CV_8U:
153 type_string = "CV_8U";
154 break;
155
156 case CV_8S:
157 type_string = "CV_8S";
158 break;
159
160 case CV_16U:
161 type_string = "CV_16U";
162 break;
163
164 case CV_16S:
165 type_string = "CV_16S";
166 break;
167
168 case CV_32S:
169 type_string = "CV_32S";
170 break;
171
172 case CV_32F:
173 type_string = "CV_32F";
174 break;
175
176 case CV_64F:
177 type_string = "CV_64F";
178 break;
179
180 default:
181 type_string = "Problem with type !";
182 break;
183 }
184
185 return type_string;
186}
187
188template<typename Type>
189void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display,
190 vpImage<Type> &Iinput, vpImage<Type> &I)
191{
192 // Set the path location of the image sequence
193 std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
194
195 // Build the name of the image files
196 std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
197 vpImageIo::read(Iinput, filename);
198 Iinput.quarterSizeImage(I);
199
200#if defined VISP_HAVE_X11
201 vpDisplayX display;
202#elif defined VISP_HAVE_GTK
203 vpDisplayGTK display;
204#elif defined VISP_HAVE_GDI
205 vpDisplayGDI display;
206#else
207 vpDisplayOpenCV display;
208#endif
209
210 if (opt_display) {
211 display.init(I, 0, 0, "KeyPoints detection.");
212 }
213
214 vpKeyPoint keyPoints;
215
216 std::vector<std::string> descriptorNames;
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 descriptorNames.push_back("SIFT");
220#endif
221#if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
222 descriptorNames.push_back("SURF");
223#endif
224 descriptorNames.push_back("ORB");
225#if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
226 descriptorNames.push_back("BRISK");
227#endif
228#if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
229 descriptorNames.push_back("BRIEF");
230#if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
231 descriptorNames.push_back("FREAK");
232#endif
233#endif
234#if defined(VISP_HAVE_OPENCV_XFEATURES2D)
235 descriptorNames.push_back("DAISY");
236 descriptorNames.push_back("LATCH");
237#endif
238#if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
239 descriptorNames.push_back("VGG");
240 descriptorNames.push_back("BoostDesc");
241#endif
242#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
243 descriptorNames.push_back("KAZE");
244 descriptorNames.push_back("AKAZE");
245#endif
246
247 std::string detectorName = "FAST";
248 keyPoints.setDetector(detectorName);
249 std::vector<cv::KeyPoint> kpts;
250
251 keyPoints.detect(I, kpts);
252 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
253 if (kpts.empty()) {
254 std::stringstream ss;
255 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
256 throw(vpException(vpException::fatalError, ss.str()));
257 }
258
259 for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
260 keyPoints.setExtractor(*itd);
261
262 if (*itd == "KAZE") {
263 detectorName = "KAZE";
264 keyPoints.setDetector(detectorName);
265 keyPoints.detect(I, kpts);
266 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
267 if (kpts.empty()) {
268 std::stringstream ss;
269 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
270 throw(vpException(vpException::fatalError, ss.str()));
271 }
272 } else if (*itd == "AKAZE") {
273 detectorName = "AKAZE";
274 keyPoints.setDetector(detectorName);
275 keyPoints.detect(I, kpts);
276 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
277 if (kpts.empty()) {
278 std::stringstream ss;
279 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
280 throw(vpException(vpException::fatalError, ss.str()));
281 }
282 } else if (*itd == "BoostDesc") {
283#if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
284 cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
285 // Init BIN BOOST descriptor for FAST keypoints
286 boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
287#endif
288 }
289
290 double t = vpTime::measureTimeMs();
291 cv::Mat descriptor;
292 keyPoints.extract(I, kpts, descriptor);
293 t = vpTime::measureTimeMs() - t;
294
295 std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
296 << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
297 << " ms." << std::endl;
298 if (descriptor.empty()) {
299 std::stringstream ss;
300 ss << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
301 throw(vpException(vpException::fatalError, ss.str()));
302 }
303
304 if (opt_display) {
306
307 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
308 vpImagePoint imPt;
309 imPt.set_uv(it->pt.x, it->pt.y);
310
312 }
313
315
316 if (opt_click_allowed) {
318 }
319 }
320 }
321
322 std::cout << "\n\n";
323
324 std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
325
326 for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
328
329 if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
330 detectorName = "KAZE";
331 keyPoints.setDetector(detectorName);
332 keyPoints.detect(I, kpts);
333 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
334 if (kpts.empty()) {
335 std::stringstream ss;
336 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
337 throw(vpException(vpException::fatalError, ss.str()));
338 }
339 } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
340 detectorName = "AKAZE";
341 keyPoints.setDetector(detectorName);
342 keyPoints.detect(I, kpts);
343 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
344 if (kpts.empty()) {
345 std::stringstream ss;
346 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
347 throw(vpException(vpException::fatalError, ss.str()));
348 }
349 } else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
350#if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
351 detectorName = "FAST";
352 keyPoints.setDetector(detectorName);
353 keyPoints.detect(I, kpts);
354 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
355 if (kpts.empty()) {
356 std::stringstream ss;
357 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
358 throw(vpException(vpException::fatalError, ss.str()));
359 }
360
361 cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
362 // Init BIN BOOST descriptor for FAST keypoints
363 boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
364#endif
365 }
366
367 double t = vpTime::measureTimeMs();
368 cv::Mat descriptor;
369 keyPoints.extract(I, kpts, descriptor);
370 t = vpTime::measureTimeMs() - t;
371
372 std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
373 << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
374 << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
375 << std::endl;
376 if (descriptor.empty()) {
377 std::stringstream ss;
378 ss << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
379 << " and image:" << filename << "." << std::endl;
380 throw(vpException(vpException::fatalError, ss.str()));
381 }
382
383 if (opt_display) {
385
386 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
387 vpImagePoint imPt;
388 imPt.set_uv(it->pt.x, it->pt.y);
389
391 }
392
394
395 if (opt_click_allowed) {
397 }
398 }
399 }
400}
401
407int main(int argc, const char **argv)
408{
409 try {
410 std::string env_ipath;
411 bool opt_click_allowed = true;
412 bool opt_display = true;
413
414 // Read the command line options
415 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
416 exit(EXIT_FAILURE);
417 }
418
419 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
420 // environment variable value
422
423 if (env_ipath.empty()) {
424 std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
425 "variable value."
426 << std::endl;
427 return EXIT_FAILURE;
428 }
429
430 {
431 vpImage<unsigned char> Iinput, I;
432
433 std::cout << "-- Test on gray level images" << std::endl;
434 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
435 }
436
437 {
438 vpImage<vpRGBa> Iinput, I;
439
440 std::cout << "-- Test on color images" << std::endl;
441 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
442 }
443
444 } catch (const vpException &e) {
445 std::cerr << e.what() << std::endl;
446 return EXIT_FAILURE;
447 }
448
449 std::cout << "testKeyPoint-6 is ok !" << std::endl;
450 return EXIT_SUCCESS;
451}
452#else
453#include <cstdlib>
454
455int main()
456{
457 std::cerr << "You need OpenCV library." << std::endl;
458
459 return EXIT_SUCCESS;
460}
461
462#endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
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:135
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 emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96
const char * what() const
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:149
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void set_uv(double u, double v)
Definition: vpImagePoint.h:247
Definition of the vpImage class member functions.
Definition: vpImage.h:139
void quarterSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:1256
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:223
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:840
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
Definition: vpKeyPoint.h:549
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, std::vector< cv::Point3f > *trainPoints=NULL)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
vpFeatureDescriptorType
Definition: vpKeyPoint.h:289
@ DESCRIPTOR_TYPE_SIZE
Definition: vpKeyPoint.h:317
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
Definition: vpKeyPoint.h:589
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:782
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
VISP_EXPORT double measureTimeMs()