Visual Servoing Platform  version 3.1.0
testKeyPoint.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
147 int main(int argc, const char **argv)
148 {
149  try {
150  std::string env_ipath;
151  bool opt_click_allowed = true;
152  bool opt_display = true;
153 
154  // Read the command line options
155  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
156  exit(-1);
157  }
158 
159  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
160  // environment variable value
161  env_ipath = vpIoTools::getViSPImagesDataPath();
162 
163  if (env_ipath.empty()) {
164  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
165  "variable value."
166  << std::endl;
167  return -1;
168  }
169 
170  vpImage<unsigned char> Iref, Icur, Imatch;
171 
172  // Set the path location of the image sequence
173  std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
174 
175  // Build the name of the image files
176  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000.pgm");
177  vpImageIo::read(Iref, filenameRef);
178  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
179 
180  // Init keypoints
181  vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
182  std::cout << "Build " << keypoints.buildReference(Iref) << " reference points." << std::endl;
183 
184  vpVideoReader g;
185  g.setFileName(filenameCur);
186  g.open(Icur);
187  g.acquire(Icur);
188 
189  Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
190  Imatch.insert(Iref, vpImagePoint(0, 0));
191 
192 #if defined VISP_HAVE_X11
193  vpDisplayX display;
194 #elif defined VISP_HAVE_GTK
195  vpDisplayGTK display;
196 #elif defined VISP_HAVE_GDI
197  vpDisplayGDI display;
198 #else
199  vpDisplayOpenCV display;
200 #endif
201 
202  if (opt_display) {
204  display.init(Imatch, 0, 0, "ORB keypoints matching");
205  }
206 
207  bool opt_click = false;
209  while (!g.end()) {
210  g.acquire(Icur);
211  Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
212 
213  if (opt_display) {
214  vpDisplay::display(Imatch);
215  }
216 
217  // Match keypoints
218  keypoints.matchPoint(Icur);
219  // Display image with keypoints matched
220  keypoints.displayMatching(Iref, Imatch);
221 
222  if (opt_display) {
223  vpDisplay::flush(Imatch);
224  }
225 
226  // Click requested to process next image
227  if (opt_click_allowed && opt_display) {
228  if (opt_click) {
229  vpDisplay::getClick(Imatch, button, true);
230  if (button == vpMouseButton::button3) {
231  opt_click = false;
232  }
233  } else {
234  // Use right click to enable/disable step by step tracking
235  if (vpDisplay::getClick(Imatch, button, false)) {
236  if (button == vpMouseButton::button3) {
237  opt_click = true;
238  } else if (button == vpMouseButton::button1) {
239  break;
240  }
241  }
242  }
243  }
244  }
245 
246  } catch (vpException &e) {
247  std::cerr << e.what() << std::endl;
248  return -1;
249  }
250 
251  std::cout << "testKeyPoint is ok !" << std::endl;
252  return 0;
253 }
254 #else
255 int main()
256 {
257  std::cerr << "You need OpenCV library." << std::endl;
258 
259  return 0;
260 }
261 
262 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1210
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:232
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
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)
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1435
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:138
void acquire(vpImage< vpRGBa > &I)
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:856
void setFileName(const char *filename)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1102
const char * what() const
unsigned int getHeight() const
Definition: vpImage.h:178
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:223
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
unsigned int getWidth() const
Definition: vpImage.h:229