Visual Servoing Platform  version 3.4.0
fernClassifier.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  * Detection of points of interests and matching using the Ferns classifier.
33  *
34  * Authors:
35  * Romain Tallonneau
36  *
37  *****************************************************************************/
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpDebug.h>
53 
54 #if ((defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && \
55  (VISP_HAVE_OPENCV_VERSION >= 0x020000) && (VISP_HAVE_OPENCV_VERSION < 0x030000))
56 
57 #include <iomanip>
58 #include <iostream>
59 #include <stdlib.h>
60 #include <visp3/core/vpImage.h>
61 #include <visp3/core/vpIoTools.h>
62 #include <visp3/core/vpTime.h>
63 #include <visp3/gui/vpDisplayGDI.h>
64 #include <visp3/gui/vpDisplayGTK.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/io/vpImageIo.h>
67 #include <visp3/io/vpParseArgv.h>
68 #include <visp3/vision/vpFernClassifier.h>
69 #include <visp3/vision/vpHomography.h>
70 
71 #define GETOPTARGS "hlcdb:i:p"
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv, bool &isLearning, std::string &dataFile, bool &click_allowed,
75  bool &display, bool &displayPoints, std::string &ipath);
76 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Detection of points of interests and matching using the Ferns classifier. The \
89 object needs first to be learned (-l option). This learning process will create\
90 a file used to detect the object.\n\
91 \n\
92 SYNOPSIS\n\
93  %s [-l] [-h] [-b] [-c] [-d] [-p] [-i]\n", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: \n\
97  -l\n\
98  learn an object.\n\
99 \n\
100  -i <input image path> \n\
101  Set image input path.\n\
102  From this path read \"line/image.%%04d.pgm\"\n\
103  images. \n\
104  Setting the VISP_INPUT_IMAGE_PATH environment\n\
105  variable produces the same behaviour than using\n\
106  this option.\n\
107 \n\
108  -b\n\
109  database filename to use (default is ./dataFern).\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -p \n\
119  display points of interest.\n\
120 \n\
121  -h\n\
122  Print this help.\n");
123 
124  if (badparam)
125  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
126 }
127 
144 bool getOptions(int argc, const char **argv, bool &isLearning, std::string &dataFile, bool &click_allowed,
145  bool &display, bool &displayPoints, std::string &ipath)
146 {
147  const char *optarg_;
148  int c;
149  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
150 
151  switch (c) {
152  case 'c':
153  click_allowed = false;
154  break;
155  case 'd':
156  display = false;
157  break;
158  case 'l':
159  isLearning = true;
160  break;
161  case 'h':
162  usage(argv[0], NULL);
163  return false;
164  break;
165  case 'b':
166  dataFile = optarg_;
167  break;
168  case 'p':
169  displayPoints = true;
170  break;
171  case 'i':
172  ipath = optarg_;
173  break;
174  default:
175  usage(argv[0], optarg_);
176  return false;
177  break;
178  }
179  }
180 
181  if ((c == 1) || (c == -1)) {
182  // standalone param or error
183  usage(argv[0], NULL);
184  std::cerr << "ERROR: " << std::endl;
185  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
186  return false;
187  }
188 
189  return true;
190 }
191 
192 int main(int argc, const char **argv)
193 {
194  try {
195  bool isLearning = false;
196  std::string dataFile("./dataFern");
197  bool opt_click_allowed = true;
198  bool opt_display = true;
199  std::string objectName("object");
200  bool displayPoints = false;
201  std::string opt_ipath;
202  std::string ipath;
203  std::string env_ipath;
204  std::string dirname;
205  std::string filename;
206 
207  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
208  // environment variable value
209  env_ipath = vpIoTools::getViSPImagesDataPath();
210 
211  // Set the default input path
212  if (!env_ipath.empty()) {
213  ipath = env_ipath;
214  }
215 
216  // Read the command line options
217  if (getOptions(argc, argv, isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
218  false) {
219  exit(-1);
220  }
221 
222  // Get the option values
223  if (!opt_ipath.empty()) {
224  ipath = opt_ipath;
225  }
226 
227  // Compare ipath and env_ipath. If they differ, we take into account
228  // the input path comming from the command line option
229  if (!opt_ipath.empty() && !env_ipath.empty()) {
230  if (ipath != env_ipath) {
231  std::cout << std::endl << "WARNING: " << std::endl;
232  std::cout << " Since -i <visp image path=" << ipath << "> "
233  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
234  << " we skip the environment variable." << std::endl;
235  }
236  }
237 
238  // Test if an input path is set
239  if (opt_ipath.empty() && env_ipath.empty()) {
240  usage(argv[0], NULL);
241  std::cerr << std::endl << "ERROR:" << std::endl;
242  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
243  << " environment variable to specify the location of the " << std::endl
244  << " image path where test images are located." << std::endl
245  << std::endl;
246  exit(-1);
247  }
248 
249  // Declare two images, these are gray level images (unsigned char)
252 
253  // Set the path location of the image sequence
254  dirname = vpIoTools::createFilePath(ipath, "cube");
255 
256  // Build the name of the image file
257  unsigned iter = 0; // Image number
258  std::ostringstream s;
259  s.setf(std::ios::right, std::ios::adjustfield);
260  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
261  filename = vpIoTools::createFilePath(dirname, s.str());
262 
263  // Read the PGM image named "filename" on the disk, and put the
264  // bitmap into the image structure I. I is initialized to the
265  // correct size
266  //
267  // exception readPGM may throw various exception if, for example,
268  // the file does not exist, or if the memory cannot be allocated
269  try {
270  std::cout << "Load: " << filename << std::endl;
271  vpImageIo::read(Iref, filename);
272  I = Iref;
273  } catch (...) {
274  // an exception is throwned if an exception from readPGM has been
275  // catched here this will result in the end of the program Note that
276  // another error message has been printed from readPGM to give more
277  // information about the error
278  std::cerr << std::endl << "ERROR:" << std::endl;
279  std::cerr << " Cannot read " << filename << std::endl;
280  std::cerr << " Check your -i " << ipath << " option " << std::endl
281  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
282  exit(-1);
283  }
284 
285 #if defined VISP_HAVE_X11
286  vpDisplayX display;
287 #elif defined VISP_HAVE_GTK
288  vpDisplayGTK display;
289 #elif defined VISP_HAVE_GDI
290  vpDisplayGDI display;
291 #endif
292 
293 #if defined VISP_HAVE_X11
294  vpDisplayX displayRef;
295 #elif defined VISP_HAVE_GTK
296  vpDisplayGTK displayRef;
297 #elif defined VISP_HAVE_GDI
298  vpDisplayGDI displayRef;
299 #endif
300 
301  // declare a planar object detector
302  vpFernClassifier fern;
303 
304  if (isLearning) {
305  if (opt_display) {
306  displayRef.init(Iref, 100, 100, "Reference image");
307  vpDisplay::display(Iref);
308  vpDisplay::flush(Iref);
309  }
310  vpImagePoint corners[2];
311  if (opt_display && opt_click_allowed) {
312  std::cout << "Click on the top left and the bottom right corners to "
313  "define the reference plane"
314  << std::endl;
315  for (int i = 0; i < 2; i++) {
316  vpDisplay::getClick(Iref, corners[i]);
317  std::cout << corners[i] << std::endl;
318  }
319  } else {
320  corners[0].set_ij(1, 1);
321  corners[1].set_ij(I.getHeight() - 2, I.getWidth() - 2);
322  }
323 
324  if (opt_display) {
325  // Display the rectangle which defines the part of the image where the
326  // reference points are computed.
327  vpDisplay::displayRectangle(Iref, corners[0], corners[1], vpColor::green);
328  vpDisplay::flush(Iref);
329  }
330 
331  if (opt_click_allowed) {
332  std::cout << "Click on the image to continue" << std::endl;
333  vpDisplay::getClick(Iref);
334  }
335 
336  vpRect roi(corners[0], corners[1]);
337 
338  std::cout << "> train the classifier on the selected plane. (may take "
339  "up to several minutes)."
340  << std::endl;
341  if (opt_display) {
342  vpDisplay::display(Iref);
343  vpDisplay::flush(Iref);
344  }
345 
346  try {
347  fern.buildReference(Iref, roi);
348  } catch (const vpException &e) {
349  std::cout << e.getMessage() << std::endl;
350  } catch (...) {
351  std::cout << "unknown error, line " << __LINE__ << std::endl;
352  }
353  try {
354  fern.record(objectName, dataFile);
355  } catch (const vpException &e) {
356  std::cout << e.getMessage() << std::endl;
357  } catch (...) {
358  std::cout << "unknown error, line " << __LINE__ << std::endl;
359  }
360  std::cout << __LINE__ << std::endl;
361  } else {
362  if (!vpIoTools::checkFilename(dataFile)) {
363  vpERROR_TRACE("cannot load the database with the specified name. Has "
364  "the object been learned with the -l option? ");
365  exit(-1);
366  }
367  try {
368  // load a previously recorded file
369  fern.load(dataFile, objectName);
370  } catch (...) {
371  vpERROR_TRACE("cannot load the database with the specified name. Has "
372  "the object been learned with the -l option? ");
373  exit(-1);
374  }
375  }
376 
377  if (opt_display) {
378  display.init(I, 110 + (int)Iref.getWidth(), 100, "Current image");
380  vpDisplay::flush(I);
381  }
382 
383  if (opt_display && opt_click_allowed) {
384  std::cout << "Click on the current image to continue" << std::endl;
385  vpDisplay::displayText(I, vpImagePoint(15, 15), "Click on the current image to continue", vpColor::red);
386  vpDisplay::flush(I);
388  }
389 
390  for (;;) {
391  // acquire a new image
392  iter++;
393  if (iter >= 80) {
394  break;
395  }
396  s.str("");
397  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
398  filename = vpIoTools::createFilePath(dirname, s.str());
399  // read the image
400  vpImageIo::read(I, filename);
401 
402  if (opt_display) {
404  if (isLearning)
405  vpDisplay::display(Iref);
406  }
407 
408  double t0 = vpTime::measureTimeMs();
409  // detection of the reference image
410  unsigned int nbpts;
411  try {
412  nbpts = fern.matchPoint(I);
413  } catch (const vpException &e) {
414  std::cout << e.getMessage() << std::endl;
415  return -1;
416  } catch (...) {
417  std::cout << "unknown error line " << __LINE__ << std::endl;
418  return -1;
419  }
420  std::cout << "matching " << nbpts << " points : " << vpTime::measureTimeMs() - t0 << " ms" << std::endl;
421 
422  if (opt_display) {
423  fern.display(Iref, I, 7);
424  vpDisplay::flush(I);
425  if (isLearning)
426  vpDisplay::flush(Iref);
427  if (vpDisplay::getClick(I, false)) {
428  break;
429  }
430  }
431  }
432 
433  return EXIT_SUCCESS;
434  } catch (const vpException &e) {
435  std::cout << "Catch an exception: " << e << std::endl;
436  return EXIT_SUCCESS;
437  }
438 }
439 
440 #else
441 int main()
442 {
443 #if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)))
444  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
445  std::cout << "Tip if you are on a unix-like system:" << std::endl;
446  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
447  std::cout << "Tip if you are on a windows-like system:" << std::endl;
448  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
449 #else
450  std::cout << "You do not have OpenCV functionalities" << std::endl;
451  std::cout << "Tip:" << std::endl;
452  std::cout << "- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
453 #endif
454  return EXIT_SUCCESS;
455 }
456 
457 #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
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:217
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 vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void set_ij(double ii, double jj)
Definition: vpImagePoint.h:188
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:244
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:640
unsigned int getHeight() const
Definition: vpImage.h:188
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
const char * getMessage() const
Definition: vpException.cpp:90