Visual Servoing Platform  version 3.6.1 under development (2025-01-15)
testKeyPoint.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
40 #include <iostream>
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if 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
60 using namespace VISP_NAMESPACE_NAME;
61 #endif
62 
63 void usage(const char *name, const char *badparam);
64 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
65 
74 void 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 
110 bool 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  break;
127 
128  default:
129  usage(argv[0], optarg_);
130  return false;
131  break;
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 
146 template <typename Type>
147 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iref,
148  vpImage<Type> &Icur, vpImage<Type> &Imatch)
149 {
150 #if VISP_HAVE_DATASET_VERSION >= 0x030600
151  std::string ext("png");
152 #else
153  std::string ext("pgm");
154 #endif
155  // Set the path location of the image sequence
156  std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
157 
158  // Build the name of the image files
159  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000." + ext);
160  vpImageIo::read(Iref, filenameRef);
161  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d." + ext);
162 
163  // Init keypoints
164  vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
165  std::cout << "Build " << keypoints.buildReference(Iref) << " reference points." << std::endl;
166 
167  vpVideoReader g;
168  g.setFileName(filenameCur);
169  g.open(Icur);
170  g.acquire(Icur);
171 
172  Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
173  Imatch.insert(Iref, vpImagePoint(0, 0));
174 
175  vpDisplay *display = nullptr;
176 
177  if (opt_display) {
178 #ifdef VISP_HAVE_DISPLAY
179  display = vpDisplayFactory::allocateDisplay(Imatch, 0, 0, "ORB keypoints matching");
180  display->setDownScalingFactor(vpDisplay::SCALE_AUTO);
181 #else
182  std::cout << "No image viewer is available..." << std::endl;
183 #endif
184  }
185 
186  bool opt_click = false;
188  while (!g.end()) {
189  g.acquire(Icur);
190  Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
191 
192  if (opt_display) {
193  vpDisplay::display(Imatch);
194  }
195 
196  // Match keypoints
197  keypoints.matchPoint(Icur);
198  // Display image with keypoints matched
199  keypoints.displayMatching(Iref, Imatch);
200 
201  if (opt_display) {
202  vpDisplay::flush(Imatch);
203  }
204 
205  // Click requested to process next image
206  if (opt_click_allowed && opt_display) {
207  if (opt_click) {
208  vpDisplay::getClick(Imatch, button, true);
209  if (button == vpMouseButton::button3) {
210  opt_click = false;
211  }
212  }
213  else {
214  // Use right click to enable/disable step by step tracking
215  if (vpDisplay::getClick(Imatch, button, false)) {
216  if (button == vpMouseButton::button3) {
217  opt_click = true;
218  }
219  else if (button == vpMouseButton::button1) {
220  break;
221  }
222  }
223  }
224  }
225  }
226 
227  if (display) {
228  delete display;
229  }
230 }
231 
232 int main(int argc, const char **argv)
233 {
234  try {
235  std::string env_ipath;
236  bool opt_click_allowed = true;
237  bool opt_display = true;
238 
239  // Read the command line options
240  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
241  return EXIT_FAILURE;
242  }
243 
244  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
245  // environment variable value
246  env_ipath = vpIoTools::getViSPImagesDataPath();
247 
248  if (env_ipath.empty()) {
249  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
250  "variable value."
251  << std::endl;
252  return EXIT_FAILURE;
253  }
254 
255  {
256  vpImage<unsigned char> Iref, Icur, Imatch;
257 
258  std::cout << "-- Test on gray level images" << std::endl;
259  run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
260  }
261 
262  {
263  vpImage<vpRGBa> Iref, Icur, Imatch;
264 
265  std::cout << "-- Test on color images" << std::endl;
266  run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
267  }
268 
269  }
270  catch (const vpException &e) {
271  std::cerr << e.what() << std::endl;
272  return EXIT_FAILURE;
273  }
274 
275  std::cout << "testKeyPoint is ok !" << std::endl;
276  return EXIT_SUCCESS;
277 }
278 #else
279 int main()
280 {
281  std::cerr << "You need OpenCV library." << std::endl;
282 
283  return EXIT_SUCCESS;
284 }
285 
286 #endif
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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)
@ SCALE_AUTO
Definition: vpDisplay.h:184
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
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()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
Class that allows keypoints 2D features detection (and descriptors extraction) and matching thanks to...
Definition: vpKeyPoint.h:267
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.