Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
testKeyPoint.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 
34 #include <iostream>
35 
36 #include <visp3/core/vpConfig.h>
37 
38 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D) && defined(HAVE_OPENCV_VIDEO)
39 
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpIoTools.h>
42 #include <visp3/gui/vpDisplayGDI.h>
43 #include <visp3/gui/vpDisplayGTK.h>
44 #include <visp3/gui/vpDisplayOpenCV.h>
45 #include <visp3/gui/vpDisplayX.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/io/vpParseArgv.h>
48 #include <visp3/io/vpVideoReader.h>
49 #include <visp3/vision/vpKeyPoint.h>
50 
51 // List of allowed command line options
52 #define GETOPTARGS "cdh"
53 
54 void usage(const char *name, const char *badparam);
55 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
56 
65 void usage(const char *name, const char *badparam)
66 {
67  fprintf(stdout, "\n\
68  Test keypoints matching.\n\
69  \n\
70  SYNOPSIS\n\
71  %s [-c] [-d] [-h]\n",
72  name);
73 
74  fprintf(stdout, "\n\
75  OPTIONS: \n\
76  \n\
77  -c\n\
78  Disable the mouse click. Useful to automate the \n\
79  execution of this program without human intervention.\n\
80  \n\
81  -d \n\
82  Turn off the display.\n\
83  \n\
84  -h\n\
85  Print the help.\n");
86 
87  if (badparam)
88  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
89 }
90 
102 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
103 {
104  const char *optarg_;
105  int c;
106  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
107 
108  switch (c) {
109  case 'c':
110  click_allowed = false;
111  break;
112  case 'd':
113  display = false;
114  break;
115  case 'h':
116  usage(argv[0], nullptr);
117  return false;
118  break;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false;
123  break;
124  }
125  }
126 
127  if ((c == 1) || (c == -1)) {
128  // standalone param or error
129  usage(argv[0], nullptr);
130  std::cerr << "ERROR: " << std::endl;
131  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
132  return false;
133  }
134 
135  return true;
136 }
137 
138 template <typename Type>
139 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iref,
140  vpImage<Type> &Icur, vpImage<Type> &Imatch)
141 {
142 #if VISP_HAVE_DATASET_VERSION >= 0x030600
143  std::string ext("png");
144 #else
145  std::string ext("pgm");
146 #endif
147  // Set the path location of the image sequence
148  std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
149 
150  // Build the name of the image files
151  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000." + ext);
152  vpImageIo::read(Iref, filenameRef);
153  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d." + ext);
154 
155  // Init keypoints
156  vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
157  std::cout << "Build " << keypoints.buildReference(Iref) << " reference points." << std::endl;
158 
159  vpVideoReader g;
160  g.setFileName(filenameCur);
161  g.open(Icur);
162  g.acquire(Icur);
163 
164  Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
165  Imatch.insert(Iref, vpImagePoint(0, 0));
166 
167 #if defined(VISP_HAVE_X11)
169 #elif defined(VISP_HAVE_GTK)
171 #elif defined(VISP_HAVE_GDI)
173 #elif defined(HAVE_OPENCV_HIGHGUI)
175 #endif
176 
177  if (opt_display) {
178  display.setDownScalingFactor(vpDisplay::SCALE_AUTO);
179  display.init(Imatch, 0, 0, "ORB keypoints matching");
180  }
181 
182  bool opt_click = false;
184  while (!g.end()) {
185  g.acquire(Icur);
186  Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
187 
188  if (opt_display) {
189  vpDisplay::display(Imatch);
190  }
191 
192  // Match keypoints
193  keypoints.matchPoint(Icur);
194  // Display image with keypoints matched
195  keypoints.displayMatching(Iref, Imatch);
196 
197  if (opt_display) {
198  vpDisplay::flush(Imatch);
199  }
200 
201  // Click requested to process next image
202  if (opt_click_allowed && opt_display) {
203  if (opt_click) {
204  vpDisplay::getClick(Imatch, button, true);
205  if (button == vpMouseButton::button3) {
206  opt_click = false;
207  }
208  } else {
209  // Use right click to enable/disable step by step tracking
210  if (vpDisplay::getClick(Imatch, button, false)) {
211  if (button == vpMouseButton::button3) {
212  opt_click = true;
213  } else if (button == vpMouseButton::button1) {
214  break;
215  }
216  }
217  }
218  }
219  }
220 }
221 
227 int main(int argc, const char **argv)
228 {
229  try {
230  std::string env_ipath;
231  bool opt_click_allowed = true;
232  bool opt_display = true;
233 
234  // Read the command line options
235  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
236  return EXIT_FAILURE;
237  }
238 
239  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
240  // environment variable value
241  env_ipath = vpIoTools::getViSPImagesDataPath();
242 
243  if (env_ipath.empty()) {
244  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
245  "variable value."
246  << std::endl;
247  return EXIT_FAILURE;
248  }
249 
250  {
251  vpImage<unsigned char> Iref, Icur, Imatch;
252 
253  std::cout << "-- Test on gray level images" << std::endl;
254  run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
255  }
256 
257  {
258  vpImage<vpRGBa> Iref, Icur, Imatch;
259 
260  std::cout << "-- Test on color images" << std::endl;
261  run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
262  }
263 
264  } catch (const vpException &e) {
265  std::cerr << e.what() << std::endl;
266  return EXIT_FAILURE;
267  }
268 
269  std::cout << "testKeyPoint is ok !" << std::endl;
270  return EXIT_SUCCESS;
271 }
272 #else
273 int main()
274 {
275  std::cerr << "You need OpenCV library." << std::endl;
276 
277  return EXIT_SUCCESS;
278 }
279 
280 #endif
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
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:128
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:179
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const char * what() const
Definition: vpException.cpp:70
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
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:69
unsigned int getWidth() const
Definition: vpImage.h:245
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:783
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1533
unsigned int getHeight() const
Definition: vpImage.h:184
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1781
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2142
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:212
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
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)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.