Visual Servoing Platform  version 3.4.0
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", name);
77 
78  fprintf(stdout, "\n\
79  OPTIONS: \n\
80  \n\
81  -c\n\
82  Disable the mouse click. Useful to automate the \n\
83  execution of this program without human intervention.\n\
84  \n\
85  -d \n\
86  Turn off the display.\n\
87  \n\
88  -h\n\
89  Print the help.\n");
90 
91  if (badparam)
92  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
93 }
94 
106 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'c':
114  click_allowed = false;
115  break;
116  case 'd':
117  display = false;
118  break;
119  case 'h':
120  usage(argv[0], NULL);
121  return false;
122  break;
123 
124  default:
125  usage(argv[0], optarg_);
126  return false;
127  break;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], NULL);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 template<typename Type>
143 void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display,
144  vpImage<Type> &Iref, vpImage<Type> &Icur, vpImage<Type> &Imatch)
145 {
146  // Set the path location of the image sequence
147  std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
148 
149  // Build the name of the image files
150  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000.pgm");
151  vpImageIo::read(Iref, filenameRef);
152  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
153 
154  // Init keypoints
155  vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
156  std::cout << "Build " << keypoints.buildReference(Iref) << " reference points." << std::endl;
157 
158  vpVideoReader g;
159  g.setFileName(filenameCur);
160  g.open(Icur);
161  g.acquire(Icur);
162 
163  Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
164  Imatch.insert(Iref, vpImagePoint(0, 0));
165 
166 #if defined VISP_HAVE_X11
167  vpDisplayX display;
168 #elif defined VISP_HAVE_GTK
169  vpDisplayGTK display;
170 #elif defined VISP_HAVE_GDI
171  vpDisplayGDI display;
172 #else
173  vpDisplayOpenCV display;
174 #endif
175 
176  if (opt_display) {
178  display.init(Imatch, 0, 0, "ORB keypoints matching");
179  }
180 
181  bool opt_click = false;
183  while (!g.end()) {
184  g.acquire(Icur);
185  Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
186 
187  if (opt_display) {
188  vpDisplay::display(Imatch);
189  }
190 
191  // Match keypoints
192  keypoints.matchPoint(Icur);
193  // Display image with keypoints matched
194  keypoints.displayMatching(Iref, Imatch);
195 
196  if (opt_display) {
197  vpDisplay::flush(Imatch);
198  }
199 
200  // Click requested to process next image
201  if (opt_click_allowed && opt_display) {
202  if (opt_click) {
203  vpDisplay::getClick(Imatch, button, true);
204  if (button == vpMouseButton::button3) {
205  opt_click = false;
206  }
207  } else {
208  // Use right click to enable/disable step by step tracking
209  if (vpDisplay::getClick(Imatch, button, false)) {
210  if (button == vpMouseButton::button3) {
211  opt_click = true;
212  } else if (button == vpMouseButton::button1) {
213  break;
214  }
215  }
216  }
217  }
218  }
219 }
220 
226 int main(int argc, const char **argv)
227 {
228  try {
229  std::string env_ipath;
230  bool opt_click_allowed = true;
231  bool opt_display = true;
232 
233  // Read the command line options
234  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
235  exit(-1);
236  }
237 
238  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
239  // environment variable value
240  env_ipath = vpIoTools::getViSPImagesDataPath();
241 
242  if (env_ipath.empty()) {
243  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
244  "variable value."
245  << std::endl;
246  return -1;
247  }
248 
249  {
250  vpImage<unsigned char> Iref, Icur, Imatch;
251 
252  std::cout << "-- Test on gray level images" << std::endl;
253  run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
254  }
255 
256  {
257  vpImage<vpRGBa> Iref, Icur, Imatch;
258 
259  std::cout << "-- Test on color images" << std::endl;
260  run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
261  }
262 
263  } catch (const vpException &e) {
264  std::cerr << e.what() << std::endl;
265  return -1;
266  }
267 
268  std::cout << "testKeyPoint is ok !" << std::endl;
269  return 0;
270 }
271 #else
272 int main()
273 {
274  std::cerr << "You need OpenCV library." << std::endl;
275 
276  return 0;
277 }
278 
279 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1202
unsigned int getWidth() const
Definition: vpImage.h:246
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:800
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:231
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void open(vpImage< vpRGBa > &I)
const char * what() const
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1446
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
void acquire(vpImage< vpRGBa > &I)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1115
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:244
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:222
void setFileName(const std::string &filename)
unsigned int getHeight() const
Definition: vpImage.h:188
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
Definition of the vpImage class member functions.
Definition: vpImage.h:126