Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
37 
38 #include <iostream>
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
43 
44 #include <visp3/vision/vpKeyPoint.h>
45 #include <visp3/core/vpImage.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/gui/vpDisplayX.h>
48 #include <visp3/gui/vpDisplayGTK.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayOpenCV.h>
51 #include <visp3/io/vpVideoReader.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/io/vpParseArgv.h>
54 
55 // List of allowed command line options
56 #define GETOPTARGS "cdh"
57 
58 void usage(const char *name, const char *badparam);
59 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
60 
69 void usage(const char *name, const char *badparam)
70 {
71  fprintf(stdout, "\n\
72 Test keypoints matching.\n\
73 \n\
74 SYNOPSIS\n\
75  %s [-c] [-d] [-h]\n", name);
76 
77  fprintf(stdout, "\n\
78 OPTIONS: \n\
79 \n\
80  -c\n\
81  Disable the mouse click. Useful to automate the \n\
82  execution of this program without human intervention.\n\
83 \n\
84  -d \n\
85  Turn off the display.\n\
86 \n\
87  -h\n\
88  Print the help.\n");
89 
90  if (badparam)
91  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
92 }
93 
105 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
106 {
107  const char *optarg_;
108  int c;
109  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
110 
111  switch (c) {
112  case 'c': click_allowed = false; break;
113  case 'd': display = false; break;
114  case 'h': usage(argv[0], NULL); return false; break;
115 
116  default:
117  usage(argv[0], optarg_);
118  return false; break;
119  }
120  }
121 
122  if ((c == 1) || (c == -1)) {
123  // standalone param or error
124  usage(argv[0], NULL);
125  std::cerr << "ERROR: " << std::endl;
126  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
127  return false;
128  }
129 
130  return true;
131 }
132 
138 int main(int argc, const char ** argv) {
139  try {
140  std::string env_ipath;
141  bool opt_click_allowed = true;
142  bool opt_display = true;
143 
144  // Read the command line options
145  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
146  exit (-1);
147  }
148 
149  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
150  env_ipath = vpIoTools::getViSPImagesDataPath();
151 
152  if(env_ipath.empty()) {
153  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment variable value." << std::endl;
154  return -1;
155  }
156 
157  vpImage<unsigned char> Iref, Icur, Imatch;
158 
159  //Set the path location of the image sequence
160  std::string dirname = vpIoTools::createFilePath(env_ipath, "ViSP-images/mbt/cube");
161 
162  //Build the name of the image files
163  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000.pgm");
164  vpImageIo::read(Iref, filenameRef);
165  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
166 
167  //Init keypoints
168  vpKeyPoint keypoints("ORB", "ORB", "BruteForce-Hamming");
169  std::cout << "Build " << keypoints.buildReference(Iref) << " reference points." << std::endl;
170 
171  vpVideoReader g;
172  g.setFileName(filenameCur);
173  g.open(Icur);
174  g.acquire(Icur);
175 
176  Imatch.resize(Icur.getHeight(), 2*Icur.getWidth());
177  Imatch.insert(Iref, vpImagePoint(0,0));
178 
179 #if defined VISP_HAVE_X11
180  vpDisplayX display;
181 #elif defined VISP_HAVE_GTK
182  vpDisplayGTK display;
183 #elif defined VISP_HAVE_GDI
184  vpDisplayGDI display;
185 #else
186  vpDisplayOpenCV display;
187 #endif
188 
189  if (opt_display) {
191  display.init(Imatch, 0, 0, "ORB keypoints matching");
192  }
193 
194  bool opt_click = false;
196  while(!g.end()) {
197  g.acquire(Icur);
198  Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
199 
200  if(opt_display) {
201  vpDisplay::display(Imatch);
202  }
203 
204  //Match keypoints
205  keypoints.matchPoint(Icur);
206  //Display image with keypoints matched
207  keypoints.displayMatching(Iref, Imatch);
208 
209  if(opt_display) {
210  vpDisplay::flush(Imatch);
211  }
212 
213  //Click requested to process next image
214  if (opt_click_allowed && opt_display) {
215  if(opt_click) {
216  vpDisplay::getClick(Imatch, button, true);
217  if(button == vpMouseButton::button3) {
218  opt_click = false;
219  }
220  } else {
221  //Use right click to enable/disable step by step tracking
222  if(vpDisplay::getClick(Imatch, button, false)) {
223  if (button == vpMouseButton::button3) {
224  opt_click = true;
225  }
226  else if(button == vpMouseButton::button1) {
227  break;
228  }
229  }
230  }
231  }
232  }
233 
234  } catch(vpException &e) {
235  std::cerr << e.what() << std::endl;
236  return -1;
237  }
238 
239  std::cout << "testKeyPoint is ok !" << std::endl;
240  return 0;
241 }
242 #else
243 int main() {
244  std::cerr << "You need OpenCV library." << std::endl;
245 
246  return 0;
247 }
248 
249 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
unsigned int getWidth() const
Definition: vpImage.h:226
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:248
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:153
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:73
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:76
void open(vpImage< vpRGBa > &I)
const char * what() const
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
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:903
void setFileName(const char *filename)
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:217
void insert(const vpImage< Type > &src, const vpImagePoint topLeft)
Definition: vpImage.h:1226
unsigned int getHeight() const
Definition: vpImage.h:175
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88