Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testKeyPoint-6.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 descriptor computation.
32 */
33
39
40#include <iostream>
41
42#include <visp3/core/vpConfig.h>
43
44#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && \
45 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
46 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES)))
47
48#include <visp3/core/vpImage.h>
49#include <visp3/core/vpIoTools.h>
50#include <visp3/gui/vpDisplayFactory.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
58#ifdef ENABLE_VISP_NAMESPACE
59using namespace VISP_NAMESPACE_NAME;
60#endif
61
62void usage(const char *name, const char *badparam);
63bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
64std::string getOpenCVType(int type);
65
72void usage(const char *name, const char *badparam)
73{
74 fprintf(stdout, "\n\
75Test keypoint descriptor extraction.\n\
76\n\
77SYNOPSIS\n\
78 %s [-c] [-d] [-h]\n",
79 name);
80
81 fprintf(stdout, "\n\
82OPTIONS: \n\
83\n\
84 -c\n\
85 Disable the mouse click. Useful to automate the \n\
86 execution of this program without human intervention.\n\
87\n\
88 -d \n\
89 Turn off the display.\n\
90\n\
91 -h\n\
92 Print the help.\n");
93
94 if (badparam)
95 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96}
97
109bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
110{
111 const char *optarg_;
112 int c;
113 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114
115 switch (c) {
116 case 'c':
117 click_allowed = false;
118 break;
119 case 'd':
120 display = false;
121 break;
122 case 'h':
123 usage(argv[0], nullptr);
124 return false;
125
126 default:
127 usage(argv[0], optarg_);
128 return false;
129 }
130 }
131
132 if ((c == 1) || (c == -1)) {
133 // standalone param or error
134 usage(argv[0], nullptr);
135 std::cerr << "ERROR: " << std::endl;
136 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137 return false;
138 }
139
140 return true;
141}
142
151std::string getOpenCVType(int type)
152{
153 std::string type_string = "";
154
155 switch (type) {
156 case CV_8U:
157 type_string = "CV_8U";
158 break;
159
160 case CV_8S:
161 type_string = "CV_8S";
162 break;
163
164 case CV_16U:
165 type_string = "CV_16U";
166 break;
167
168 case CV_16S:
169 type_string = "CV_16S";
170 break;
171
172 case CV_32S:
173 type_string = "CV_32S";
174 break;
175
176 case CV_32F:
177 type_string = "CV_32F";
178 break;
179
180 case CV_64F:
181 type_string = "CV_64F";
182 break;
183
184 default:
185 type_string = "Problem with type !";
186 break;
187 }
188
189 return type_string;
190}
191
192template <typename Type>
193void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
194 vpImage<Type> &I)
195{
196 // Set the path location of the image sequence
197 std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
198
199 // Build the name of the image files
200 std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
201 vpImageIo::read(Iinput, filename);
202 Iinput.quarterSizeImage(I);
203
204 vpDisplay *display = nullptr;
205
206 if (opt_display) {
207#ifdef VISP_HAVE_DISPLAY
208 display = vpDisplayFactory::allocateDisplay(I, 0, 0, "KeyPoints detection.");
209#else
210 std::cout << "No image viewer is available..." << std::endl;
211#endif
212 }
213
214 vpKeyPoint keyPoints;
215
216 std::vector<std::string> descriptorNames;
217#if defined(VISP_HAVE_OPENCV) && \
218 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || \
219 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
220 descriptorNames.push_back("SIFT");
221#endif
222#if defined(HAVE_OPENCV_NONFREE) && defined(HAVE_OPENCV_XFEATURES2D)
223 descriptorNames.push_back("SURF");
224#endif
225#if defined(VISP_HAVE_OPENCV) && \
226 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
227 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
228 descriptorNames.push_back("ORB");
229#endif
230#if defined(VISP_HAVE_OPENCV) && \
231 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
232 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)))
233 descriptorNames.push_back("BRISK");
234#endif
235#if defined(HAVE_OPENCV_XFEATURES2D)
236 descriptorNames.push_back("BRIEF");
237 descriptorNames.push_back("FREAK");
238 descriptorNames.push_back("DAISY");
239 descriptorNames.push_back("LATCH");
240#endif
241#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(HAVE_OPENCV_XFEATURES2D)
242 descriptorNames.push_back("VGG");
243 descriptorNames.push_back("BoostDesc");
244#endif
245#if defined(VISP_HAVE_OPENCV) && \
246 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
247 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)))
248#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
249 descriptorNames.push_back("KAZE");
250 descriptorNames.push_back("AKAZE");
251#endif
252#endif
253#if defined(VISP_HAVE_OPENCV) && \
254 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
255 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
256 std::string detectorName = "FAST";
257#endif
258 keyPoints.setDetector(detectorName);
259 std::vector<cv::KeyPoint> kpts;
260
261 keyPoints.detect(I, kpts);
262 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
263 if (kpts.empty()) {
264 std::stringstream ss;
265 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
266 throw(vpException(vpException::fatalError, ss.str()));
267 }
268
269 for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
270 keyPoints.setExtractor(*itd);
271
272 if (*itd == "KAZE") {
273 detectorName = "KAZE";
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 }
283 else if (*itd == "AKAZE") {
284 detectorName = "AKAZE";
285 keyPoints.setDetector(detectorName);
286 keyPoints.detect(I, kpts);
287 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
288 if (kpts.empty()) {
289 std::stringstream ss;
290 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
291 throw(vpException(vpException::fatalError, ss.str()));
292 }
293 }
294 else if (*itd == "BoostDesc") {
295#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(HAVE_OPENCV_XFEATURES2D)
296 cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
297 // Init BIN BOOST descriptor for FAST keypoints
298 boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
299#endif
300 }
301
302 double t = vpTime::measureTimeMs();
303 cv::Mat descriptor;
304 keyPoints.extract(I, kpts, descriptor);
306
307 std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
308 << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
309 << " ms." << std::endl;
310 if (descriptor.empty()) {
311 std::stringstream ss;
312 ss << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
313 throw(vpException(vpException::fatalError, ss.str()));
314 }
315
316 if (opt_display) {
318
319 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
320 vpImagePoint imPt;
321 imPt.set_uv(static_cast<double>(it->pt.x), static_cast<double>(it->pt.y));
322
324 }
325
327
328 if (opt_click_allowed) {
330 }
331 }
332 }
333
334 std::cout << "\n\n";
335
336 std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
337
338 for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
340
341 if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
342 detectorName = "KAZE";
343 keyPoints.setDetector(detectorName);
344 keyPoints.detect(I, kpts);
345 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
346 if (kpts.empty()) {
347 std::stringstream ss;
348 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
349 throw(vpException(vpException::fatalError, ss.str()));
350 }
351 }
352 else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
353 detectorName = "AKAZE";
354 keyPoints.setDetector(detectorName);
355 keyPoints.detect(I, kpts);
356 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
357 if (kpts.empty()) {
358 std::stringstream ss;
359 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
360 throw(vpException(vpException::fatalError, ss.str()));
361 }
362 }
363 else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
364#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(HAVE_OPENCV_XFEATURES2D)
365 detectorName = "FAST";
366 keyPoints.setDetector(detectorName);
367 keyPoints.detect(I, kpts);
368 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
369 if (kpts.empty()) {
370 std::stringstream ss;
371 ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
372 throw(vpException(vpException::fatalError, ss.str()));
373 }
374
375 cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
376 // Init BIN BOOST descriptor for FAST keypoints
377 boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
378#endif
379 }
380
381 double t = vpTime::measureTimeMs();
382 cv::Mat descriptor;
383 keyPoints.extract(I, kpts, descriptor);
385
386 std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
387 << " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
388 << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
389 << std::endl;
390 if (descriptor.empty()) {
391 std::stringstream ss;
392 ss << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
393 << " and image:" << filename << "." << std::endl;
394 throw(vpException(vpException::fatalError, ss.str()));
395 }
396
397 if (opt_display) {
399
400 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
401 vpImagePoint imPt;
402 imPt.set_uv(static_cast<double>(it->pt.x), static_cast<double>(it->pt.y));
403
405 }
406
408
409 if (opt_click_allowed) {
411 }
412 }
413 }
414 if (display) {
415 delete display;
416 }
417}
418
419int main(int argc, const char **argv)
420{
421 try {
422 std::string env_ipath;
423 bool opt_click_allowed = true;
424 bool opt_display = true;
425
426 // Read the command line options
427 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
428 exit(EXIT_FAILURE);
429 }
430
431 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
432 // environment variable value
434
435 if (env_ipath.empty()) {
436 std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
437 "variable value."
438 << std::endl;
439 return EXIT_FAILURE;
440 }
441
442 {
443 vpImage<unsigned char> Iinput, I;
444
445 std::cout << "-- Test on gray level images" << std::endl;
446 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
447 }
448
449 {
450 vpImage<vpRGBa> Iinput, I;
451
452 std::cout << "-- Test on color images" << std::endl;
453 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
454 }
455
456 }
457 catch (const vpException &e) {
458 std::cerr << e.what() << std::endl;
459 return EXIT_FAILURE;
460 }
461
462 std::cout << "testKeyPoint-6 is ok !" << std::endl;
463 return EXIT_SUCCESS;
464}
465#else
466#include <cstdlib>
467
468int main()
469{
470 std::cerr << "You need OpenCV library." << std::endl;
471
472 return EXIT_SUCCESS;
473}
474
475#endif
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:60
@ fatalError
Fatal error.
Definition vpException.h:72
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_uv(double u, double v)
Definition of the vpImage class member functions.
Definition vpImage.h:131
void quarterSizeImage(vpImage< Type > &res) const
Definition vpImage.h:793
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
Class that allows keypoints 2D features detection (and descriptors extraction) and matching thanks to...
Definition vpKeyPoint.h:274
void setExtractor(const vpFeatureDescriptorType &extractorType)
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, std::vector< cv::Point3f > *trainPoints=nullptr)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
vpFeatureDescriptorType
Definition vpKeyPoint.h:366
@ DESCRIPTOR_TYPE_SIZE
Number of descriptors available.
Definition vpKeyPoint.h:413
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
void setDetector(const vpFeatureDetectorType &detectorType)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()