ViSP  2.9.0
keyPointSurf.cpp
1 /****************************************************************************
2  *
3  * $Id: keyPointSurf.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Tracking of Surf key points.
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Fabien Spindler
40  *
41  *****************************************************************************/
56 #include <visp/vpDebug.h>
57 #include <visp/vpConfig.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <sstream>
61 #include <iomanip>
62 #if ((defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_OPENCV_NONFREE) && (VISP_HAVE_OPENCV_VERSION >= 0x010100))
63 
64 #include <visp/vpKeyPointSurf.h>
65 
66 #include <visp/vpImage.h>
67 #include <visp/vpImageIo.h>
68 #include <visp/vpImagePoint.h>
69 #include <visp/vpDisplayX.h>
70 #include <visp/vpDisplayGTK.h>
71 #include <visp/vpDisplayGDI.h>
72 
73 #include <visp/vpParseArgv.h>
74 #include <visp/vpIoTools.h>
75 
76 // List of allowed command line options
77 #define GETOPTARGS "cdi:h"
78 
79 void usage(const char *name, const char *badparam, std::string ipath);
80 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
81 
91 void usage(const char *name, const char *badparam, std::string ipath)
92 {
93  fprintf(stdout, "\n\
94 Tracking of Surf key-points.\n\
95 \n\
96 SYNOPSIS\n\
97  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
98 
99  fprintf(stdout, "\n\
100 OPTIONS: Default\n\
101  -i <input image path> %s\n\
102  Set image input path.\n\
103  From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
104  images. \n\
105  Setting the VISP_INPUT_IMAGE_PATH environment\n\
106  variable produces the same behaviour than using\n\
107  this option.\n\
108 \n\
109  -c\n\
110  Disable the mouse click. Useful to automaze the \n\
111  execution of this program without humain intervention.\n\
112 \n\
113  -d \n\
114  Turn off the display.\n\
115 \n\
116  -h\n\
117  Print the help.\n",
118  ipath.c_str());
119 
120  if (badparam)
121  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
122 }
136 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
137 {
138  const char *optarg_;
139  int c;
140  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141 
142  switch (c) {
143  case 'c': click_allowed = false; break;
144  case 'd': display = false; break;
145  case 'i': ipath = optarg_; break;
146  case 'h': usage(argv[0], NULL, ipath); return false; break;
147 
148  default:
149  usage(argv[0], optarg_, ipath);
150  return false; break;
151  }
152  }
153 
154  if ((c == 1) || (c == -1)) {
155  // standalone param or error
156  usage(argv[0], NULL, ipath);
157  std::cerr << "ERROR: " << std::endl;
158  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
159  return false;
160  }
161 
162  return true;
163 }
164 
165 
166 int
167 main(int argc, const char ** argv)
168 {
169  try {
170  std::string env_ipath;
171  std::string opt_ipath;
172  std::string ipath;
173  std::string dirname;
174  std::string filename;
175  bool opt_click_allowed = true;
176  bool opt_display = true;
177 
178  // Get the VISP_IMAGE_PATH environment variable value
179  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
180  if (ptenv != NULL)
181  env_ipath = ptenv;
182 
183  // Set the default input path
184  if (! env_ipath.empty())
185  ipath = env_ipath;
186 
187  // Read the command line options
188  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
189  opt_display) == false) {
190  exit (-1);
191  }
192 
193  // Get the option values
194  if (!opt_ipath.empty())
195  ipath = opt_ipath;
196 
197  // Compare ipath and env_ipath. If they differ, we take into account
198  // the input path comming from the command line option
199  if (!opt_ipath.empty() && !env_ipath.empty()) {
200  if (ipath != env_ipath) {
201  std::cout << std::endl
202  << "WARNING: " << std::endl;
203  std::cout << " Since -i <visp image path=" << ipath << "> "
204  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
205  << " we skip the environment variable." << std::endl;
206  }
207  }
208 
209  // Test if an input path is set
210  if (opt_ipath.empty() && env_ipath.empty()){
211  usage(argv[0], NULL, ipath);
212  std::cerr << std::endl
213  << "ERROR:" << std::endl;
214  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
215  << std::endl
216  << " environment variable to specify the location of the " << std::endl
217  << " image path where test images are located." << std::endl << std::endl;
218  exit(-1);
219  }
220 
221  // Declare an image, this is a gray level image (unsigned char)
222  // it size is not defined yet, it will be defined when the image will
223  // read on the disk
226 
227  // Set the path location of the image sequence
228  dirname = ipath + vpIoTools::path("/ViSP-images/cube/");
229 
230  // Build the name of the image file
231  unsigned int iter = 0; // Image number
232  std::ostringstream s;
233  s.setf(std::ios::right, std::ios::adjustfield);
234  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
235  filename = dirname + s.str();
236 
237  // Read the PGM image named "filename" on the disk, and put the
238  // bitmap into the image structure I. I is initialized to the
239  // correct size
240  //
241  // exception readPGM may throw various exception if, for example,
242  // the file does not exist, or if the memory cannot be allocated
243  try{
244  vpCTRACE << "Load: " << filename << std::endl;
245 
246  vpImageIo::read(Iref, filename) ;
247  }
248  catch(...)
249  {
250  // an exception is throwned if an exception from readPGM has been catched
251  // here this will result in the end of the program
252  // Note that another error message has been printed from readPGM
253  // to give more information about the error
254  std::cerr << std::endl
255  << "ERROR:" << std::endl;
256  std::cerr << " Cannot read " << filename << std::endl;
257  std::cerr << " Check your -i " << ipath << " option " << std::endl
258  << " or VISP_INPUT_IMAGE_PATH environment variable."
259  << std::endl;
260  exit(-1);
261  }
262 
263  // We open a window using either X11, GTK or GDI.
264 #if defined VISP_HAVE_X11
265  vpDisplayX display[2];
266 #elif defined VISP_HAVE_GTK
267  vpDisplayGTK display[2];
268 #elif defined VISP_HAVE_GDI
269  vpDisplayGDI display[2];
270 #endif
271 
272  if (opt_display) {
273  try{
274  // Display size is automatically defined by the image (I) size
275  display[0].init(Iref, 100, 100,"Display reference image") ;
276  vpDisplay::display(Iref) ;
277  vpDisplay::flush(Iref) ;
278  }
279  catch(...)
280  {
281  vpERROR_TRACE("Error while displaying the image") ;
282  exit(-1);
283  }
284  }
285 
286  vpImagePoint corners[2];
287  if (opt_display && opt_click_allowed)
288  {
289  std::cout << "Click on the top left and the bottom right corners to define the part of the image where the reference points will be computed" << std::endl;
290  for (unsigned int i=0 ; i < 2 ; i++)
291  {
292  vpDisplay::getClick(Iref, corners[i]);
293  std::cout << corners[i] << std::endl;
294  }
295  }
296  else
297  {
298  corners[0].set_ij(156,209);
299  corners[1].set_ij(272,378);
300  }
301 
302  if (opt_display)
303  {
304  //Display the rectangle which defines the part of the image where the reference points are computed.
305  vpDisplay::displayRectangle(Iref, corners[0], corners[1], vpColor::green);
306  vpDisplay::flush(Iref);
307  }
308 
309  if (opt_click_allowed)
310  {
311  std::cout << "Click on the image to continue" << std::endl;
312  vpDisplay::getClick(Iref);
313  }
314 
315  vpKeyPointSurf surf;
316  // unsigned int nbrRef;
317  unsigned int height, width;
318  height = (unsigned int)(corners[1].get_i() - corners[0].get_i());
319  width = (unsigned int)(corners[1].get_j() - corners[0].get_j());
320 
321  //Computes the reference points
322  /* nbrRef = */ surf.buildReference(Iref, corners[0], height, width);
323 
324  unsigned int nbrPair = 0;
325 
326  vpImageIo::read(Icur, filename);
327 
328  if (opt_display) {
329  try{
330  // Display size is automatically defined by the image (I) size
331  display[1].init(Icur, (int)(100+Iref.getWidth()), 100,"Display current image") ;
332  vpDisplay::display(Icur) ;
333  vpDisplay::flush(Icur) ;
334  }
335  catch(...)
336  {
337  vpERROR_TRACE("Error while displaying the image") ;
338  exit(-1);
339  }
340  }
341 
342  for (iter = 1 ; iter < 30 ; iter++)
343  {
344  std::cout <<"----------------------------------------------------------"<<std::endl;
345  // set the new image name
346  s.str("");
347  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
348  filename = dirname + s.str();
349  // read the image
350  vpImageIo::read(Icur, filename);
351  if (opt_display) {
352  // Display the image
353  vpDisplay::display(Iref) ;
354  vpDisplay::display(Icur) ;
355  }
356 
357  nbrPair = surf.matchPoint(Icur);
358  std::cout << "Number of matched point : " << nbrPair <<std::endl;
359 
360  if (opt_display)
361  {
362  // Display the matched features
363  surf.display(Iref, Icur, 7);
364  vpDisplay::displayRectangle(Iref, corners[0], corners[1], vpColor::red);
365  vpDisplay::flush(Iref) ;
366  vpDisplay::flush(Icur) ;
367  }
368  }
369  return 0;
370  }
371  catch(vpException e) {
372  std::cout << "Catch an exception: " << e << std::endl;
373  return 1;
374  }
375 }
376 
377 #else
378 int
379 main()
380 {
381 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) )
382  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
383 #else
384  vpERROR_TRACE("You do not have OpenCV-1.1.0 or a more recent release that contains opencv_nonfree component...");
385 #endif
386 }
387 
388 #endif
double get_i() const
Definition: vpImagePoint.h:194
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpERROR_TRACE
Definition: vpDebug.h:395
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
unsigned int matchPoint(const vpImage< unsigned char > &I)
void display(const vpImage< unsigned char > &Iref, const vpImage< unsigned char > &Icurrent, unsigned int size=3)
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
double get_j() const
Definition: vpImagePoint.h:205
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:167
#define vpCTRACE
Definition: vpDebug.h:341
Class that implements the SURF key points and technics thanks to the OpenCV library.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int buildReference(const vpImage< unsigned char > &I)
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
void set_ij(const double ii, const double jj)
Definition: vpImagePoint.h:180