Visual Servoing Platform  version 3.5.1 under development (2022-11-26)
testKeyPoint.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test keypoint matching.
33  *
34  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
44 
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/gui/vpDisplayGDI.h>
48 #include <visp3/gui/vpDisplayGTK.h>
49 #include <visp3/gui/vpDisplayOpenCV.h>
50 #include <visp3/gui/vpDisplayX.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 void usage(const char *name, const char *badparam);
60 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
61 
70 void usage(const char *name, const char *badparam)
71 {
72  fprintf(stdout, "\n\
73  Test keypoints matching.\n\
74  \n\
75  SYNOPSIS\n\
76  %s [-c] [-d] [-h]\n",
77  name);
78 
79  fprintf(stdout, "\n\
80  OPTIONS: \n\
81  \n\
82  -c\n\
83  Disable the mouse click. Useful to automate the \n\
84  execution of this program without human intervention.\n\
85  \n\
86  -d \n\
87  Turn off the display.\n\
88  \n\
89  -h\n\
90  Print the help.\n");
91 
92  if (badparam)
93  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94 }
95 
107 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
108 {
109  const char *optarg_;
110  int c;
111  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
112 
113  switch (c) {
114  case 'c':
115  click_allowed = false;
116  break;
117  case 'd':
118  display = false;
119  break;
120  case 'h':
121  usage(argv[0], NULL);
122  return false;
123  break;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false;
128  break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
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 
143 template <typename Type>
144 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iref,
145  vpImage<Type> &Icur, vpImage<Type> &Imatch)
146 {
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.pgm");
152  vpImageIo::read(Iref, filenameRef);
153  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
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
168  vpDisplayX display;
169 #elif defined VISP_HAVE_GTK
170  vpDisplayGTK display;
171 #elif defined VISP_HAVE_GDI
172  vpDisplayGDI display;
173 #else
174  vpDisplayOpenCV display;
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  exit(-1);
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 -1;
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 -1;
267  }
268 
269  std::cout << "testKeyPoint is ok !" << std::endl;
270  return 0;
271 }
272 #else
273 int main()
274 {
275  std::cerr << "You need OpenCV library." << std::endl;
276 
277  return 0;
278 }
279 
280 #endif
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
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:135
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:183
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * what() const
Definition: vpException.cpp:99
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:148
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
Definition of the vpImage class member functions.
Definition: vpImage.h:73
unsigned int getWidth() const
Definition: vpImage.h:246
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:799
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1112
unsigned int getHeight() const
Definition: vpImage.h:188
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1378
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1696
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:223
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)