Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testKeyPoint.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 keypoint matching.
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/io/vpVideoReader.h>
54#include <visp3/vision/vpKeyPoint.h>
55
56// List of allowed command line options
57#define GETOPTARGS "cdh"
58
59#ifdef ENABLE_VISP_NAMESPACE
60using namespace VISP_NAMESPACE_NAME;
61#endif
62
63void usage(const char *name, const char *badparam);
64bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
65
74void usage(const char *name, const char *badparam)
75{
76 fprintf(stdout, "\n\
77 Test keypoints matching.\n\
78 \n\
79 SYNOPSIS\n\
80 %s [-c] [-d] [-h]\n", name);
81
82 fprintf(stdout, "\n\
83 OPTIONS: \n\
84 \n\
85 -c\n\
86 Disable the mouse click. Useful to automate the \n\
87 execution of this program without human intervention.\n\
88 \n\
89 -d \n\
90 Turn off the display.\n\
91 \n\
92 -h\n\
93 Print the help.\n");
94
95 if (badparam)
96 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
97}
98
110bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
111{
112 const char *optarg_;
113 int c;
114 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115
116 switch (c) {
117 case 'c':
118 click_allowed = false;
119 break;
120 case 'd':
121 display = false;
122 break;
123 case 'h':
124 usage(argv[0], nullptr);
125 return false;
126
127 default:
128 usage(argv[0], optarg_);
129 return false;
130 }
131 }
132
133 if ((c == 1) || (c == -1)) {
134 // standalone param or error
135 usage(argv[0], nullptr);
136 std::cerr << "ERROR: " << std::endl;
137 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
138 return false;
139 }
140
141 return true;
142}
143
144template <typename Type>
145void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iref,
146 vpImage<Type> &Icur, vpImage<Type> &Imatch)
147{
148#if defined(VISP_HAVE_DATASET)
149#if VISP_HAVE_DATASET_VERSION >= 0x030600
150 std::string ext("png");
151#else
152 std::string ext("pgm");
153#endif
154#else
155 // We suppose that the user will download a recent dataset
156 std::string ext("png");
157#endif
158 // Set the path location of the image sequence
159 std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
160
161 // Build the name of the image files
162 std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000." + ext);
163 vpImageIo::read(Iref, filenameRef);
164 std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d." + ext);
165
166 // Init keypoints
167 vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
168 std::cout << "Build " << keypoints.buildReference(Iref) << " reference points." << std::endl;
169
171 g.setFileName(filenameCur);
172 g.open(Icur);
173 g.acquire(Icur);
174
175 Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
176 Imatch.insert(Iref, vpImagePoint(0, 0));
177
178 vpDisplay *display = nullptr;
179
180 if (opt_display) {
181#ifdef VISP_HAVE_DISPLAY
182 display = vpDisplayFactory::allocateDisplay(Imatch, 0, 0, "ORB keypoints matching");
183 display->setDownScalingFactor(vpDisplay::SCALE_AUTO);
184#else
185 std::cout << "No image viewer is available..." << std::endl;
186#endif
187 }
188
189 bool opt_click = false;
191 while (!g.end()) {
192 g.acquire(Icur);
193 Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
194
195 if (opt_display) {
196 vpDisplay::display(Imatch);
197 }
198
199 // Match keypoints
200 keypoints.matchPoint(Icur);
201 // Display image with keypoints matched
202 keypoints.displayMatching(Iref, Imatch);
203
204 if (opt_display) {
205 vpDisplay::flush(Imatch);
206 }
207
208 // Click requested to process next image
209 if (opt_click_allowed && opt_display) {
210 if (opt_click) {
211 vpDisplay::getClick(Imatch, button, true);
212 if (button == vpMouseButton::button3) {
213 opt_click = false;
214 }
215 }
216 else {
217 // Use right click to enable/disable step by step tracking
218 if (vpDisplay::getClick(Imatch, button, false)) {
219 if (button == vpMouseButton::button3) {
220 opt_click = true;
221 }
222 else if (button == vpMouseButton::button1) {
223 break;
224 }
225 }
226 }
227 }
228 }
229
230 if (display) {
231 delete display;
232 }
233}
234
235int main(int argc, const char **argv)
236{
237 try {
238 std::string env_ipath;
239 bool opt_click_allowed = true;
240 bool opt_display = true;
241
242 // Read the command line options
243 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
244 return EXIT_FAILURE;
245 }
246
247 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
248 // environment variable value
250
251 if (env_ipath.empty()) {
252 std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
253 "variable value."
254 << std::endl;
255 return EXIT_FAILURE;
256 }
257
258 {
259 vpImage<unsigned char> Iref, Icur, Imatch;
260
261 std::cout << "-- Test on gray level images" << std::endl;
262 run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
263 }
264
265 {
266 vpImage<vpRGBa> Iref, Icur, Imatch;
267
268 std::cout << "-- Test on color images" << std::endl;
269 run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
270 }
271
272 }
273 catch (const vpException &e) {
274 std::cerr << e.what() << std::endl;
275 return EXIT_FAILURE;
276 }
277
278 std::cout << "testKeyPoint is ok !" << std::endl;
279 return EXIT_SUCCESS;
280}
281#else
282int main()
283{
284 std::cerr << "You need OpenCV library." << std::endl;
285
286 return EXIT_SUCCESS;
287}
288
289#endif
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 flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition vpException.h:60
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 ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition vpImage.h:544
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition vpImage.h:639
unsigned int getHeight() const
Definition vpImage.h:181
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
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.