Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testKeyPoint-5.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 keypoints detection with OpenCV, specially the Pyramid implementation
32 * feature misssing in OpenCV 3.0.
33 */
34
41
42#include <iostream>
43
44#include <visp3/core/vpConfig.h>
45
46#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && \
47 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
48 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES)))
49
50#include <visp3/core/vpImage.h>
51#include <visp3/core/vpIoTools.h>
52#include <visp3/gui/vpDisplayFactory.h>
53#include <visp3/io/vpImageIo.h>
54#include <visp3/io/vpParseArgv.h>
55#include <visp3/vision/vpKeyPoint.h>
56
57// List of allowed command line options
58#define GETOPTARGS "cdh"
59
60#ifdef ENABLE_VISP_NAMESPACE
61using namespace VISP_NAMESPACE_NAME;
62#endif
63
64void usage(const char *name, const char *badparam);
65bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
66
75void usage(const char *name, const char *badparam)
76{
77 fprintf(stdout, "\n\
78Test keypoints detection.\n\
79\n\
80SYNOPSIS\n\
81 %s [-c] [-d] [-h]\n",
82 name);
83
84 fprintf(stdout, "\n\
85OPTIONS: \n\
86\n\
87 -c\n\
88 Disable the mouse click. Useful to automate the \n\
89 execution of this program without human intervention.\n\
90\n\
91 -d \n\
92 Turn off the display.\n\
93\n\
94 -h\n\
95 Print the help.\n");
96
97 if (badparam)
98 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99}
100
112bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
113{
114 const char *optarg_;
115 int c;
116 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117
118 switch (c) {
119 case 'c':
120 click_allowed = false;
121 break;
122 case 'd':
123 display = false;
124 break;
125 case 'h':
126 usage(argv[0], nullptr);
127 return false;
128
129 default:
130 usage(argv[0], optarg_);
131 return false;
132 }
133 }
134
135 if ((c == 1) || (c == -1)) {
136 // standalone param or error
137 usage(argv[0], nullptr);
138 std::cerr << "ERROR: " << std::endl;
139 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
140 return false;
141 }
142
143 return true;
144}
145
146template <typename Type>
147void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
148 vpImage<Type> &I)
149{
150 // Set the path location of the image sequence
151 std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
152
153 // Build the name of the image files
154 std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
155 vpImageIo::read(Iinput, filename);
156 Iinput.halfSizeImage(I);
157
158 vpDisplay *display = nullptr;
159
160 if (opt_display) {
161#ifdef VISP_HAVE_DISPLAY
162 display = vpDisplayFactory::allocateDisplay(I, 0, 0, "KeyPoints detection.");
163#else
164 std::cout << "No image viewer is available..." << std::endl;
165#endif
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 even for features "
172 << "that are scale invariant to detect potential problem in ViSP." << std::endl
173 << std::endl;
174 vpKeyPoint keyPoints;
175
176 // Will test the different types of keypoints detection to see if there is
177 // a problem between OpenCV versions, modules or constructors
178 std::vector<std::string> detectorNames;
179#if defined(VISP_HAVE_OPENCV) && \
180 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
181 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
182 detectorNames.push_back("PyramidFAST");
183 detectorNames.push_back("FAST");
184#endif
185#if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
186 detectorNames.push_back("PyramidMSER");
187 detectorNames.push_back("MSER");
188 detectorNames.push_back("PyramidGFTT");
189 detectorNames.push_back("GFTT");
190 detectorNames.push_back("PyramidSimpleBlob");
191 detectorNames.push_back("SimpleBlob");
192#endif
193
194 // In contrib modules
195#if defined(HAVE_OPENCV_XFEATURES2D)
196 detectorNames.push_back("PyramidSTAR");
197 detectorNames.push_back("STAR");
198#endif
199#if defined(VISP_HAVE_OPENCV) && \
200 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
201 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
202 detectorNames.push_back("PyramidORB");
203 detectorNames.push_back("ORB");
204#endif
205#if defined(VISP_HAVE_OPENCV) && \
206 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
207 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)))
208#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
209 detectorNames.push_back("PyramidAGAST");
210 detectorNames.push_back("AGAST");
211#endif
212 detectorNames.push_back("PyramidBRISK");
213 detectorNames.push_back("BRISK");
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#endif
221
222#if defined(VISP_HAVE_OPENCV) && \
223 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || \
224 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
225#if (VISP_HAVE_OPENCV_VERSION != 0x040504) && (VISP_HAVE_OPENCV_VERSION != 0x040505) && \
226 (VISP_HAVE_OPENCV_VERSION != 0x040600) && (VISP_HAVE_OPENCV_VERSION != 0x040700) && \
227 (VISP_HAVE_OPENCV_VERSION != 0x040900) && (VISP_HAVE_OPENCV_VERSION != 0x040A00) && \
228 (defined(__APPLE__) && defined(__MACH__))
229 // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
230 // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
231 // installed with brew. It seems working when OpenCV is build from source
232 std::cout << "-- Add SIFT detector" << std::endl;
233 detectorNames.push_back("PyramidSIFT");
234 detectorNames.push_back("SIFT");
235#endif
236#endif
237#if defined(OPENCV_ENABLE_NONFREE) && defined(HAVE_OPENCV_XFEATURES2D)
238 detectorNames.push_back("PyramidSURF");
239 detectorNames.push_back("SURF");
240#endif
241
242 for (std::vector<std::string>::const_iterator itd = detectorNames.begin(); itd != detectorNames.end(); ++itd) {
243 keyPoints.setDetector(*itd);
244
245 std::vector<cv::KeyPoint> kpts;
246
247 keyPoints.detect(I, kpts);
248 std::cout << "Nb keypoints detected: " << kpts.size() << " for " << *itd << " method." << std::endl;
249 if (kpts.empty()) {
250 std::stringstream ss;
251 ss << "No keypoints detected with " << *itd << " and image: " << filename << "." << std::endl;
252 throw(vpException(vpException::fatalError, ss.str()));
253 }
254
255 if (opt_display) {
257
258 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
259 vpImagePoint imPt;
260 imPt.set_uv(it->pt.x, it->pt.y);
261
263 }
264
266
267 if (opt_click_allowed) {
269 }
270 }
271 }
272
273 std::cout << "\n\n";
274
275 std::map<vpKeyPoint::vpFeatureDetectorType, std::string> mapOfDetectorNames = keyPoints.getDetectorNames();
276 for (int i = 0; i < vpKeyPoint::DETECTOR_TYPE_SIZE; i++) {
277#if defined(VISP_HAVE_OPENCV) && \
278 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && ((VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || \
279 (VISP_HAVE_OPENCV_VERSION >= 0x040400)) && defined(HAVE_OPENCV_FEATURES2D))) || \
280 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
281#if ((VISP_HAVE_OPENCV_VERSION == 0x040504) || (VISP_HAVE_OPENCV_VERSION == 0x040505) || \
282 (VISP_HAVE_OPENCV_VERSION == 0x040600) || (VISP_HAVE_OPENCV_VERSION == 0x040700) || \
283 (VISP_HAVE_OPENCV_VERSION == 0x040900) || (VISP_HAVE_OPENCV_VERSION == 0x040A00)) && \
284 (defined(__APPLE__) && defined(__MACH__))
285 // SIFT is known to be unstable with OpenCV 4.5.4 and 4.5.5 on macOS (see #1048)
286 // Same for OpenCV 4.6.0 (see #1106) where it produces an Illegal Instruction error when OpenCV 4.6.0 is
287 // installed with brew. It seems working when OpenCV is build from source
288 if (i == vpKeyPoint::DETECTOR_SIFT) {
289 std::cout << "-- Skip SIFT detector" << std::endl;
290 continue;
291 }
292#endif
293#endif
295
296 std::vector<cv::KeyPoint> kpts;
297
298 keyPoints.detect(I, kpts);
299 std::cout << "Nb keypoints detected: " << kpts.size() << " for "
300 << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i] << " method." << std::endl;
301 if (kpts.empty()) {
302 std::stringstream ss;
303 ss << "No keypoints detected with " << mapOfDetectorNames[(vpKeyPoint::vpFeatureDetectorType)i]
304 << " method and image: " << filename << "." << std::endl;
305 throw(vpException(vpException::fatalError, ss.str()));
306 }
307
308 if (opt_display) {
310
311 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
312 vpImagePoint imPt;
313 imPt.set_uv(it->pt.x, it->pt.y);
314
316 }
317
319
320 if (opt_click_allowed) {
322 }
323 }
324 }
325
326 if (display) {
327 delete display;
328 }
329}
330
331int main(int argc, const char **argv)
332{
333 try {
334 std::string env_ipath;
335 bool opt_click_allowed = true;
336 bool opt_display = true;
337
338 // Read the command line options
339 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
340 exit(EXIT_FAILURE);
341 }
342
343 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
344 // environment variable value
346
347 if (env_ipath.empty()) {
348 std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
349 "variable value."
350 << std::endl;
351 return EXIT_FAILURE;
352 }
353
354 {
355 vpImage<unsigned char> Iinput, I;
356
357 std::cout << "-- Test on gray level images" << std::endl;
358 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
359 }
360
361 {
362 vpImage<vpRGBa> Iinput, I;
363
364 std::cout << "-- Test on color images" << std::endl;
365 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
366 }
367
368 }
369 catch (const vpException &e) {
370 std::cerr << e.what() << std::endl;
371 return EXIT_FAILURE;
372 }
373
374 std::cout << "testKeyPoint-5 is ok !" << std::endl;
375 return EXIT_SUCCESS;
376}
377#else
378#include <cstdlib>
379
380int main()
381{
382 std::cerr << "You need OpenCV library." << std::endl;
383
384 return EXIT_SUCCESS;
385}
386
387#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 halfSizeImage(vpImage< Type > &res) const
Definition vpImage.h:725
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
@ DETECTOR_TYPE_SIZE
Number of detectors available.
Definition vpKeyPoint.h:361
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
std::map< vpFeatureDetectorType, std::string > getDetectorNames() 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.