ViSP
testSurfKeyPoint.cpp
1 /****************************************************************************
2  *
3  * $Id: testSurfKeyPoint.cpp 5202 2015-01-24 09:29:06Z 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  * Test auto detection of dots.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
42 
43 
44 #include <visp/vpDebug.h>
45 #include <visp/vpConfig.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <sstream>
49 #include <iomanip>
50 #if ((defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_OPENCV_NONFREE) && (VISP_HAVE_OPENCV_VERSION < 0x030000)) // Require opencv >= 1.1.0 < 3.0.0
51 
52 #include <visp/vpKeyPointSurf.h>
53 #include <visp/vpImage.h>
54 #include <visp/vpImageIo.h>
55 #include <visp/vpDisplayX.h>
56 #include <visp/vpDisplayGTK.h>
57 #include <visp/vpDisplayGDI.h>
58 
59 
60 #include <visp/vpCameraParameters.h>
61 
62 #include <visp/vpParseArgv.h>
63 #include <visp/vpIoTools.h>
64 
71 // List of allowed command line options
72 #define GETOPTARGS "cdi:h"
73 
74 void usage(const char *name, const char *badparam, std::string ipath);
75 bool getOptions(int argc, const char **argv, std::string &ipath,
76  bool &click_allowed, bool &display);
77 
87 void usage(const char *name, const char *badparam, std::string ipath)
88 {
89  fprintf(stdout, "\n\
90 Test dot tracking.\n\
91 \n\
92 SYNOPSIS\n\
93  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  -i <input image path> %s\n\
98  Set image input path.\n\
99  From this path read image \n\
100  \"ViSP-images/ellipse/ellipse.pgm\"\n\
101  Setting the VISP_INPUT_IMAGE_PATH environment\n\
102  variable produces the same behaviour than using\n\
103  this option.\n\
104 \n\
105  -c\n\
106  Disable the mouse click. Useful to automaze the \n\
107  execution of this program without humain intervention.\n\
108 \n\
109  -d \n\
110  Turn off the display.\n\
111 \n\
112  -h\n\
113  Print the help.\n",
114  ipath.c_str());
115 
116  if (badparam)
117  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
118 
119 }
132 bool getOptions(int argc, const char **argv, std::string &ipath,
133  bool &click_allowed, bool &display)
134 {
135  const char *optarg_;
136  int c;
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138 
139  switch (c) {
140  case 'c': click_allowed = false; break;
141  case 'd': display = false; break;
142  case 'i': ipath = optarg_; break;
143  case 'h': usage(argv[0], NULL, ipath); return false; break;
144 
145  default:
146  usage(argv[0], optarg_, ipath);
147  return false; break;
148  }
149  }
150 
151  if ((c == 1) || (c == -1)) {
152  // standalone param or error
153  usage(argv[0], NULL, ipath);
154  std::cerr << "ERROR: " << std::endl;
155  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
156  return false;
157  }
158 
159  return true;
160 }
161 
162 
163 int
164 main(int argc, const char ** argv)
165 {
166  try {
167  std::string env_ipath;
168  std::string opt_ipath;
169  std::string ipath;
170  std::string dirname;
171  std::string filenameRef;
172  std::string filenameCur;
173  bool opt_click_allowed = true;
174  bool opt_display = true;
175 
176  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
177  env_ipath = vpIoTools::getViSPImagesDataPath();
178 
179  // Set the default input path
180  if (! env_ipath.empty())
181  ipath = env_ipath;
182 
183 
184  // Read the command line options
185  if (getOptions(argc, argv, opt_ipath,
186  opt_click_allowed, opt_display) == false) {
187  exit (-1);
188  }
189 
190  // Get the option values
191  if (!opt_ipath.empty())
192  ipath = opt_ipath;
193 
194  // Compare ipath and env_ipath. If they differ, we take into account
195  // the input path comming from the command line option
196  if (!opt_ipath.empty() && !env_ipath.empty()) {
197  if (ipath != env_ipath) {
198  std::cout << std::endl
199  << "WARNING: " << std::endl;
200  std::cout << " Since -i <visp image path=" << ipath << "> "
201  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
202  << " we skip the environment variable." << std::endl;
203  }
204  }
205 
206  // Test if an input path is set
207  if (opt_ipath.empty() && env_ipath.empty()){
208  usage(argv[0], NULL, ipath);
209  std::cerr << std::endl
210  << "ERROR:" << std::endl;
211  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
212  << std::endl
213  << " environment variable to specify the location of the " << std::endl
214  << " image path where test images are located." << std::endl << std::endl;
215  exit(-1);
216  }
217 
218 
219  // Declare an image, this is a gray level image (unsigned char)
220  // it size is not defined yet, it will be defined when the image will
221  // read on the disk
224 
225  // Set the path location of the image sequence
226  dirname = vpIoTools::createFilePath(ipath, "ViSP-images/cube");
227 
228  // Build the name of the image file
229  filenameRef = vpIoTools::createFilePath(dirname, "image.0000.pgm");
230  filenameCur = vpIoTools::createFilePath(dirname, "image.0079.pgm");
231 
232  // Read the PGM image named "filename" on the disk, and put the
233  // bitmap into the image structure I. I is initialized to the
234  // correct size
235  //
236  // exception readPGM may throw various exception if, for example,
237  // the file does not exist, or if the memory cannot be allocated
238  try{
239  vpCTRACE << "Load: " << filenameRef << std::endl;
240 
241  vpImageIo::read(Iref, filenameRef) ;
242 
243  vpCTRACE << "Load: " << filenameCur << std::endl;
244 
245  vpImageIo::read(Icur, filenameCur) ;
246  }
247  catch(...)
248  {
249  // an exception is throwned if an exception from readPGM has been catched
250  // here this will result in the end of the program
251  // Note that another error message has been printed from readPGM
252  // to give more information about the error
253  std::cerr << std::endl
254  << "ERROR:" << std::endl;
255  std::cerr << " Cannot read " << filenameRef << "or" << filenameCur <<std::endl;
256  std::cerr << " Check your -i " << ipath << " option " << std::endl
257  << " or VISP_INPUT_IMAGE_PATH environment variable."
258  << std::endl;
259  exit(-1);
260  }
261 
262  // We open a window using either X11, GTK or GDI.
263 #if defined VISP_HAVE_X11
264  vpDisplayX display[2];
265 #elif defined VISP_HAVE_GTK
266  vpDisplayGTK display[2];
267 #elif defined VISP_HAVE_GDI
268  vpDisplayGDI display[2];
269 #endif
270 
271  if (opt_display) {
272  // Display size is automatically defined by the image (I) size
273  display[0].init(Iref, 100, 100, "Reference image") ;
274  // Display the image
275  // The image class has a member that specify a pointer toward
276  // the display that has been initialized in the display declaration
277  // therefore is is no longuer necessary to make a reference to the
278  // display variable.
279  vpDisplay::display(Iref) ;
280  //Flush the display
281  vpDisplay::flush(Iref) ;
282  }
283 
284  vpKeyPointSurf surf;
285  unsigned int nbrRef;
286 
287  if (opt_click_allowed && opt_display)
288  {
289  std::cout << "Select a part of the image where the reference points will be computed. This part is a rectangle." << std::endl;
290  std::cout << "Click first on the top left corner and then on the bottom right corner." << std::endl;
291  vpImagePoint corners[2];
292  for (int i=0 ; i < 2 ; i++)
293  {
294  vpDisplay::getClick(Iref, corners[i]);
295  }
296 
297  vpDisplay::displayRectangle(Iref, corners[0], corners[1], vpColor::red);
298  vpDisplay::flush(Iref);
299  unsigned int height, width;
300  height = (unsigned int)(corners[1].get_i() - corners[0].get_i());
301  width = (unsigned int)(corners[1].get_j() - corners[0].get_j());
302 
303  //Computes the reference points
304  nbrRef = surf.buildReference(Iref, corners[0], height, width);
305  }
306 
307  else
308  {
309  nbrRef = surf.buildReference(Iref);
310  }
311 
312  if(nbrRef < 1)
313  {
314  vpTRACE("No reference point");
315  exit(-1);
316  }
317 
318 
319  unsigned int nbrPair;
320  if (opt_display) {
321  display[1].init(Icur, (int)(100+Iref.getWidth()), 100, "Current image") ;
322  // display variable.
323  vpDisplay::display(Icur) ;
324  //Flush the display
325  vpDisplay::flush(Icur) ;
326  }
327 
328  if (opt_click_allowed && opt_display)
329  {
330  std::cout << "Select a part of the current image where the reference will be search. This part is a rectangle." << std::endl;
331  std::cout << "Click first on the top left corner and then on the bottom right corner." << std::endl;
332  vpImagePoint corners[2];
333  for (int i=0 ; i < 2 ; i++)
334  {
335  vpDisplay::getClick(Icur, corners[i]);
336  }
337  vpDisplay::displayRectangle(Icur, corners[0], corners[1], vpColor::green);
338  vpDisplay::flush(Icur);
339  unsigned int height, width;
340  height = (unsigned int)(corners[1].get_i() - corners[0].get_i());
341  width = (unsigned int)(corners[1].get_j() - corners[0].get_j());
342 
343  //Computes the reference points
344  nbrPair = surf.matchPoint(Icur, corners[0], height, width);
345  }
346 
347  else
348  {
349  nbrPair = surf.matchPoint(Icur);
350  }
351 
352  if(nbrPair < 1)
353  {
354  vpTRACE("No point matched");
355  }
356 
357  if (opt_display)
358  {
359  surf.display(Iref, Icur, 7);
360  vpDisplay::flush(Iref) ;
361  vpDisplay::flush(Icur) ;
362  if (opt_click_allowed)
363  {
364  std::cout << "A click on the reference image to exit..." << std::endl;
365  vpDisplay::getClick(Iref);
366  }
367  }
368  return (0);
369  }
370  catch(vpException e) {
371  std::cout << "Catch an exception: " << e << std::endl;
372  return (1);
373  }
374 }
375 #else
376 int
377 main()
378 {
379 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) )
380  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
381 #else
382  vpERROR_TRACE("You do not have 1.1.0 <= OpenCV < 2.4.8 that contains opencv_nonfree component...");
383 #endif
384 }
385 
386 #endif
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
double get_i() const
Definition: vpImagePoint.h:195
unsigned int getWidth() const
Definition: vpImage.h:161
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
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
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:2232
double get_j() const
Definition: vpImagePoint.h:206
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
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 OpenCV library.
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
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:93
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278