Visual Servoing Platform  version 3.1.0
testKeyPoint-7.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 saving / loading learning files for vpKeyPoint class.
33  *
34  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
39 #include <iomanip>
40 #include <iostream>
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
45 
46 #include <visp3/core/vpException.h>
47 #include <visp3/core/vpImage.h>
48 #include <visp3/core/vpIoTools.h>
49 #include <visp3/io/vpImageIo.h>
50 #include <visp3/io/vpParseArgv.h>
51 #include <visp3/vision/vpKeyPoint.h>
52 
53 // List of allowed command line options
54 #define GETOPTARGS "cdo:h"
55 
64 void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user)
65 {
66  fprintf(stdout, "\n\
67 Test save / load learning files for vpKeyPoint class.\n\
68 \n\
69 SYNOPSIS\n\
70  %s [-c] [-d] [-h]\n", name);
71 
72  fprintf(stdout, "\n\
73 OPTIONS: \n\
74 \n\
75  -o <output image path> %s\n\
76  Set image output path.\n\
77  From this directory, creates the \"%s\"\n\
78  subdirectory depending on the username, where \n\
79  learning files will be written.\n\
80 \n\
81  -h\n\
82  Print the help.\n", opath.c_str(), user.c_str());
83 
84  if (badparam)
85  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
86 }
87 
99 bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user)
100 {
101  const char *optarg_;
102  int c;
103  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
104 
105  switch (c) {
106  case 'c':
107  break; // not used, to avoid error with default arguments ctest
108  case 'd':
109  break; // not used, to avoid error with default arguments ctest
110  case 'o':
111  opath = optarg_;
112  break;
113  case 'h':
114  usage(argv[0], NULL, opath, user);
115  return false;
116  break;
117 
118  default:
119  usage(argv[0], optarg_, opath, user);
120  return false;
121  break;
122  return false;
123  break;
124  }
125  }
126 
127  if ((c == 1) || (c == -1)) {
128  // standalone param or error
129  usage(argv[0], NULL, opath, user);
130  std::cerr << "ERROR: " << std::endl;
131  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
132  return false;
133  }
134 
135  return true;
136 }
137 
146 bool compareKeyPoints(const std::vector<cv::KeyPoint> &keypoints1, const std::vector<cv::KeyPoint> &keypoints2)
147 {
148  if (keypoints1.size() != keypoints2.size()) {
149  return false;
150  }
151 
152  for (size_t cpt = 0; cpt < keypoints1.size(); cpt++) {
153  if (!vpMath::equal(keypoints1[cpt].angle, keypoints2[cpt].angle, std::numeric_limits<float>::epsilon())) {
154  std::cerr << std::fixed << std::setprecision(9) << "keypoints1[cpt].angle=" << keypoints1[cpt].angle
155  << " ; keypoints2[cpt].angle=" << keypoints2[cpt].angle << std::endl;
156  return false;
157  }
158 
159  if (keypoints1[cpt].class_id != keypoints2[cpt].class_id) {
160  std::cerr << "keypoints1[cpt].class_id=" << keypoints1[cpt].class_id
161  << " ; keypoints2[cpt].class_id=" << keypoints2[cpt].class_id << std::endl;
162  return false;
163  }
164 
165  if (keypoints1[cpt].octave != keypoints2[cpt].octave) {
166  std::cerr << "keypoints1[cpt].octave=" << keypoints1[cpt].octave
167  << " ; keypoints2[cpt].octave=" << keypoints2[cpt].octave << std::endl;
168  return false;
169  }
170 
171  if (!vpMath::equal(keypoints1[cpt].pt.x, keypoints2[cpt].pt.x, std::numeric_limits<float>::epsilon())) {
172  std::cerr << std::fixed << std::setprecision(9) << "keypoints1[cpt].pt.x=" << keypoints1[cpt].pt.x
173  << " ; keypoints2[cpt].pt.x=" << keypoints2[cpt].pt.x << std::endl;
174  return false;
175  }
176 
177  if (!vpMath::equal(keypoints1[cpt].pt.y, keypoints2[cpt].pt.y, std::numeric_limits<float>::epsilon())) {
178  std::cerr << std::fixed << std::setprecision(9) << "keypoints1[cpt].pt.y=" << keypoints1[cpt].pt.y
179  << " ; keypoints2[cpt].pt.y=" << keypoints2[cpt].pt.y << std::endl;
180  return false;
181  }
182 
183  if (!vpMath::equal(keypoints1[cpt].response, keypoints2[cpt].response, std::numeric_limits<float>::epsilon())) {
184  std::cerr << std::fixed << std::setprecision(9) << "keypoints1[cpt].response=" << keypoints1[cpt].response
185  << " ; keypoints2[cpt].response=" << keypoints2[cpt].response << std::endl;
186  return false;
187  }
188 
189  if (!vpMath::equal(keypoints1[cpt].size, keypoints2[cpt].size, std::numeric_limits<float>::epsilon())) {
190  std::cerr << std::fixed << std::setprecision(9) << "keypoints1[cpt].size=" << keypoints1[cpt].size
191  << " ; keypoints2[cpt].size=" << keypoints2[cpt].size << std::endl;
192  return false;
193  }
194  }
195 
196  return true;
197 }
198 
207 bool compareDescriptors(const cv::Mat &descriptors1, const cv::Mat &descriptors2)
208 {
209  if (descriptors1.rows != descriptors2.rows || descriptors1.cols != descriptors2.cols ||
210  descriptors1.type() != descriptors2.type()) {
211  return false;
212  }
213 
214  for (int i = 0; i < descriptors1.rows; i++) {
215  for (int j = 0; j < descriptors1.cols; j++) {
216  switch (descriptors1.type()) {
217  case CV_8U:
218  if (descriptors1.at<unsigned char>(i, j) != descriptors2.at<unsigned char>(i, j)) {
219  std::cerr << "descriptors1.at<unsigned char>(i,j)=" << descriptors1.at<unsigned char>(i, j)
220  << " ; descriptors2.at<unsigned char>(i,j)=" << descriptors2.at<unsigned char>(i, j) << std::endl;
221  return false;
222  }
223  break;
224 
225  case CV_8S:
226  if (descriptors1.at<char>(i, j) != descriptors2.at<char>(i, j)) {
227  std::cerr << "descriptors1.at<char>(i,j)=" << descriptors1.at<char>(i, j)
228  << " ; descriptors2.at<char>(i,j)=" << descriptors2.at<char>(i, j) << std::endl;
229  return false;
230  }
231  break;
232 
233  case CV_16U:
234  if (descriptors1.at<unsigned short>(i, j) != descriptors2.at<unsigned short>(i, j)) {
235  std::cerr << "descriptors1.at<unsigned short>(i,j)=" << descriptors1.at<unsigned short>(i, j)
236  << " ; descriptors2.at<unsigned short>(i,j)=" << descriptors2.at<unsigned short>(i, j) << std::endl;
237  return false;
238  }
239  break;
240 
241  case CV_16S:
242  if (descriptors1.at<short>(i, j) != descriptors2.at<short>(i, j)) {
243  std::cerr << "descriptors1.at<short>(i,j)=" << descriptors1.at<short>(i, j)
244  << " ; descriptors2.at<short>(i,j)=" << descriptors2.at<short>(i, j) << std::endl;
245  return false;
246  }
247  break;
248 
249  case CV_32S:
250  if (descriptors1.at<int>(i, j) != descriptors2.at<int>(i, j)) {
251  std::cerr << "descriptors1.at<int>(i,j)=" << descriptors1.at<int>(i, j)
252  << " ; descriptors2.at<int>(i,j)=" << descriptors2.at<int>(i, j) << std::endl;
253  return false;
254  }
255  break;
256 
257  case CV_32F:
258  if (!vpMath::equal(descriptors1.at<float>(i, j), descriptors2.at<float>(i, j),
259  std::numeric_limits<float>::epsilon())) {
260  std::cerr << std::fixed << std::setprecision(9)
261  << "descriptors1.at<float>(i,j)=" << descriptors1.at<float>(i, j)
262  << " ; descriptors2.at<float>(i,j)=" << descriptors2.at<float>(i, j) << std::endl;
263  return false;
264  }
265  break;
266 
267  case CV_64F:
268  if (!vpMath::equal(descriptors1.at<double>(i, j), descriptors2.at<double>(i, j),
269  std::numeric_limits<double>::epsilon())) {
270  std::cerr << std::fixed << std::setprecision(17)
271  << "descriptors1.at<double>(i,j)=" << descriptors1.at<double>(i, j)
272  << " ; descriptors2.at<double>(i,j)=" << descriptors2.at<double>(i, j) << std::endl;
273  return false;
274  }
275  break;
276 
277  default:
278  return false;
279  break;
280  }
281  }
282  }
283 
284  return true;
285 }
286 
292 int main(int argc, const char **argv)
293 {
294  try {
295  std::string env_ipath;
296  std::string opt_opath;
297  std::string username;
298  std::string opath;
299  std::string filename;
300 
301  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
302  // environment variable value
303  env_ipath = vpIoTools::getViSPImagesDataPath();
304 
305  if (env_ipath.empty()) {
306  throw vpException(vpException::ioError, "Please set the VISP_INPUT_IMAGE_PATH environment variable value.");
307  }
308 
309 // Set the default output path
310 #if defined(_WIN32)
311  opt_opath = "C:/temp";
312 #else
313  opt_opath = "/tmp";
314 #endif
315 
316  // Get the user login name
317  vpIoTools::getUserName(username);
318 
319  // Read the command line options
320  if (getOptions(argc, argv, opt_opath, username) == false) {
321  throw vpException(vpException::fatalError, "getOptions(argc, argv, opt_opath, username) == false");
322  }
323 
324  // Get the option values
325  if (!opt_opath.empty()) {
326  opath = opt_opath;
327  }
328 
329  // Append to the output path string, the login name of the user
330  opath = vpIoTools::createFilePath(opath, username);
331 
333 
334  // Set the path location of the image sequence
335  std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
336 
337  // Build the name of the image files
338  std::string img_filename = vpIoTools::createFilePath(dirname, "/Klimt.ppm");
339  vpImageIo::read(I, img_filename);
340 
341  vpKeyPoint keyPoints;
342 
343  // Test with binary descriptor
344  {
345  std::string keypointName = "ORB";
346  keyPoints.setDetector(keypointName);
347  keyPoints.setExtractor(keypointName);
348 
349  keyPoints.buildReference(I);
350 
351  std::vector<cv::KeyPoint> trainKeyPoints;
352  keyPoints.getTrainKeyPoints(trainKeyPoints);
353  cv::Mat trainDescriptors = keyPoints.getTrainDescriptors();
354  if (trainKeyPoints.empty() || trainDescriptors.empty() || (int)trainKeyPoints.size() != trainDescriptors.rows) {
355  throw vpException(vpException::fatalError, "Problem when detecting "
356  "keypoints or when "
357  "computing descriptors !");
358  }
359 
360  // Save in binary with training images
361  filename = vpIoTools::createFilePath(opath, "bin_with_img");
362  vpIoTools::makeDirectory(filename);
363  filename = vpIoTools::createFilePath(filename, "test_save_in_bin_with_img.bin");
364  keyPoints.saveLearningData(filename, true, true);
365 
366  // Test if save is ok
367  if (!vpIoTools::checkFilename(filename)) {
368  std::stringstream ss;
369  ss << "Problem when saving file=" << filename;
370  throw vpException(vpException::ioError, ss.str().c_str());
371  }
372 
373  // Test if read is ok
374  vpKeyPoint read_keypoint1;
375  read_keypoint1.loadLearningData(filename, true);
376  std::vector<cv::KeyPoint> trainKeyPoints_read;
377  read_keypoint1.getTrainKeyPoints(trainKeyPoints_read);
378  cv::Mat trainDescriptors_read = read_keypoint1.getTrainDescriptors();
379 
380  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
381  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved "
382  "in binary with train images saved !");
383  }
384 
385  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
386  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
387  "learning file saved in "
388  "binary with train images saved !");
389  }
390 
391  // Save in binary with no training images
392  filename = vpIoTools::createFilePath(opath, "bin_without_img");
393  vpIoTools::makeDirectory(filename);
394  filename = vpIoTools::createFilePath(filename, "test_save_in_bin_without_img.bin");
395  keyPoints.saveLearningData(filename, true, false);
396 
397  // Test if save is ok
398  if (!vpIoTools::checkFilename(filename)) {
399  std::stringstream ss;
400  ss << "Problem when saving file=" << filename;
401  throw vpException(vpException::ioError, ss.str().c_str());
402  }
403 
404  // Test if read is ok
405  vpKeyPoint read_keypoint2;
406  read_keypoint2.loadLearningData(filename, true);
407  trainKeyPoints_read.clear();
408  read_keypoint2.getTrainKeyPoints(trainKeyPoints_read);
409  trainDescriptors_read = read_keypoint2.getTrainDescriptors();
410 
411  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
412  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
413  "binary without train images !");
414  }
415 
416  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
417  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
418  "learning file saved in "
419  "binary without train images !");
420  }
421 
422 #if defined(VISP_HAVE_XML2)
423  // Save in xml with training images
424  filename = vpIoTools::createFilePath(opath, "xml_with_img");
425  vpIoTools::makeDirectory(filename);
426  filename = vpIoTools::createFilePath(filename, "test_save_in_xml_with_img.xml");
427  keyPoints.saveLearningData(filename, false, true);
428 
429  // Test if save is ok
430  if (!vpIoTools::checkFilename(filename)) {
431  std::stringstream ss;
432  ss << "Problem when saving file=" << filename;
433  throw vpException(vpException::ioError, ss.str().c_str());
434  }
435 
436  // Test if read is ok
437  vpKeyPoint read_keypoint3;
438  read_keypoint3.loadLearningData(filename, false);
439  trainKeyPoints_read.clear();
440  read_keypoint3.getTrainKeyPoints(trainKeyPoints_read);
441  trainDescriptors_read = read_keypoint3.getTrainDescriptors();
442 
443  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
444  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
445  "xml with train images saved !");
446  }
447 
448  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
449  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
450  "learning file saved in "
451  "xml with train images saved !");
452  }
453 
454  // Save in xml without training images
455  filename = vpIoTools::createFilePath(opath, "xml_without_img");
456  vpIoTools::makeDirectory(filename);
457  filename = vpIoTools::createFilePath(filename, "test_save_in_xml_without_img.xml");
458  keyPoints.saveLearningData(filename, false, false);
459 
460  // Test if save is ok
461  if (!vpIoTools::checkFilename(filename)) {
462  std::stringstream ss;
463  ss << "Problem when saving file=" << filename;
464  throw vpException(vpException::ioError, ss.str().c_str());
465  }
466 
467  // Test if read is ok
468  vpKeyPoint read_keypoint4;
469  read_keypoint4.loadLearningData(filename, false);
470  trainKeyPoints_read.clear();
471  read_keypoint4.getTrainKeyPoints(trainKeyPoints_read);
472  trainDescriptors_read = read_keypoint4.getTrainDescriptors();
473 
474  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
475  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
476  "xml without train images saved !");
477  }
478 
479  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
480  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
481  "learning file saved in "
482  "xml without train images saved !");
483  }
484 #endif
485 
486  std::cout << "Saving / loading learning files with binary descriptor are ok !" << std::endl;
487  }
488 
489 // Test with floating point descriptor
490 #if defined(VISP_HAVE_OPENCV_NONFREE) || \
491  ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(VISP_HAVE_OPENCV_XFEATURES2D))
492  {
493  std::string keypointName = "SIFT";
494  keyPoints.setDetector(keypointName);
495  keyPoints.setExtractor(keypointName);
496 
497  keyPoints.buildReference(I);
498 
499  std::vector<cv::KeyPoint> trainKeyPoints;
500  keyPoints.getTrainKeyPoints(trainKeyPoints);
501  cv::Mat trainDescriptors = keyPoints.getTrainDescriptors();
502  if (trainKeyPoints.empty() || trainDescriptors.empty() || (int)trainKeyPoints.size() != trainDescriptors.rows) {
503  throw vpException(vpException::fatalError, "Problem when detecting keypoints or when "
504  "computing descriptors (SIFT) !");
505  }
506 
507  // Save in binary with training images
508  filename = vpIoTools::createFilePath(opath, "bin_with_img");
509  vpIoTools::makeDirectory(filename);
510  filename = vpIoTools::createFilePath(filename, "test_save_in_bin_with_img.bin");
511  keyPoints.saveLearningData(filename, true, true);
512 
513  // Test if save is ok
514  if (!vpIoTools::checkFilename(filename)) {
515  std::stringstream ss;
516  ss << "Problem when saving file=" << filename;
517  throw vpException(vpException::ioError, ss.str().c_str());
518  }
519 
520  // Test if read is ok
521  vpKeyPoint read_keypoint1;
522  read_keypoint1.loadLearningData(filename, true);
523  std::vector<cv::KeyPoint> trainKeyPoints_read;
524  read_keypoint1.getTrainKeyPoints(trainKeyPoints_read);
525  cv::Mat trainDescriptors_read = read_keypoint1.getTrainDescriptors();
526 
527  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
528  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
529  "binary with train images saved !");
530  }
531 
532  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
533  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
534  "learning file saved in "
535  "binary with train images saved !");
536  }
537 
538  // Save in binary with no training images
539  filename = vpIoTools::createFilePath(opath, "bin_without_img");
540  vpIoTools::makeDirectory(filename);
541  filename = vpIoTools::createFilePath(filename, "test_save_in_bin_without_img.bin");
542  keyPoints.saveLearningData(filename, true, false);
543 
544  // Test if save is ok
545  if (!vpIoTools::checkFilename(filename)) {
546  std::stringstream ss;
547  ss << "Problem when saving file=" << filename;
548  throw vpException(vpException::ioError, ss.str().c_str());
549  }
550 
551  // Test if read is ok
552  vpKeyPoint read_keypoint2;
553  read_keypoint2.loadLearningData(filename, true);
554  trainKeyPoints_read.clear();
555  read_keypoint2.getTrainKeyPoints(trainKeyPoints_read);
556  trainDescriptors_read = read_keypoint2.getTrainDescriptors();
557 
558  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
559  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
560  "binary without train images saved !");
561  }
562 
563  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
564  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
565  "learning file saved in "
566  "binary without train images saved !");
567  }
568 
569 #if defined(VISP_HAVE_XML2)
570  // Save in xml with training images
571  filename = vpIoTools::createFilePath(opath, "xml_with_img");
572  vpIoTools::makeDirectory(filename);
573  filename = vpIoTools::createFilePath(filename, "test_save_in_xml_with_img.xml");
574  keyPoints.saveLearningData(filename, false, true);
575 
576  // Test if save is ok
577  if (!vpIoTools::checkFilename(filename)) {
578  std::stringstream ss;
579  ss << "Problem when saving file=" << filename;
580  throw vpException(vpException::ioError, ss.str().c_str());
581  }
582 
583  // Test if read is ok
584  vpKeyPoint read_keypoint3;
585  read_keypoint3.loadLearningData(filename, false);
586  trainKeyPoints_read.clear();
587  read_keypoint3.getTrainKeyPoints(trainKeyPoints_read);
588  trainDescriptors_read = read_keypoint3.getTrainDescriptors();
589 
590  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
591  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
592  "xml with train images saved !");
593  }
594 
595  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
596  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
597  "learning file saved in "
598  "xml with train images saved !");
599  }
600 
601  // Save in xml without training images
602  filename = vpIoTools::createFilePath(opath, "xml_without_img");
603  vpIoTools::makeDirectory(filename);
604  filename = vpIoTools::createFilePath(filename, "test_save_in_xml_without_img.xml");
605  keyPoints.saveLearningData(filename, false, false);
606 
607  // Test if save is ok
608  if (!vpIoTools::checkFilename(filename)) {
609  std::stringstream ss;
610  ss << "Problem when saving file=" << filename;
611  throw vpException(vpException::ioError, ss.str().c_str());
612  }
613 
614  // Test if read is ok
615  vpKeyPoint read_keypoint4;
616  read_keypoint4.loadLearningData(filename, false);
617  trainKeyPoints_read.clear();
618  read_keypoint4.getTrainKeyPoints(trainKeyPoints_read);
619  trainDescriptors_read = read_keypoint4.getTrainDescriptors();
620 
621  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
622  throw vpException(vpException::fatalError, "Problem with trainKeyPoints when reading learning file saved in "
623  "xml without train images saved !");
624  }
625 
626  if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
627  throw vpException(vpException::fatalError, "Problem with trainDescriptors when reading "
628  "learning file saved in "
629  "xml without train images saved !");
630  }
631 #endif
632 
633  std::cout << "Saving / loading learning files with floating point "
634  "descriptor are ok !"
635  << std::endl;
636 
637  // Test vpKeyPoint::reset()
638  vpKeyPoint keypoint_reset;
639 
640  keypointName = "ORB";
641  keypoint_reset.setDetector(keypointName);
642  keypoint_reset.setExtractor(keypointName);
643 
644  keypoint_reset.buildReference(I);
645 
646  // reset
647  keypoint_reset.reset();
648 
649  keypointName = "SIFT";
650  keypoint_reset.setDetector(keypointName);
651  keypoint_reset.setExtractor(keypointName);
652 
653  keypoint_reset.buildReference(I);
654 
655  std::vector<cv::KeyPoint> trainKeyPoints_reset;
656  keypoint_reset.getTrainKeyPoints(trainKeyPoints_reset);
657  cv::Mat trainDescriptors_reset = keypoint_reset.getTrainDescriptors();
658 
659  // If reset is ok, we should get the same keypoints and the same
660  // descriptors
661  if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_reset)) {
662  throw vpException(vpException::fatalError, "Problem with vpKeyPoint::reset() and trainKeyPoints !");
663  }
664 
665  if (!compareDescriptors(trainDescriptors, trainDescriptors_reset)) {
666  throw vpException(vpException::fatalError, "Problem with vpKeyPoint::reset() and trainDescriptors !");
667  }
668 
669  std::cout << "vpKeyPoint::reset() is ok with trainKeyPoints and "
670  "trainDescriptors !"
671  << std::endl;
672  }
673 #endif
674 
675  } catch (vpException &e) {
676  std::cerr << e.what() << std::endl;
677  return -1;
678  }
679 
680  std::cout << "Saving / loading learning files are ok !" << std::endl;
681  std::cout << "testKeyPoint-7 is ok !" << std::endl;
682  return 0;
683 }
684 #else
685 int main()
686 {
687  std::cerr << "You need OpenCV library." << std::endl;
688 
689  return 0;
690 }
691 
692 #endif
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1214
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:290
error that can be emited by ViSP classes.
Definition: vpException.h:71
cv::Mat getTrainDescriptors() const
Definition: vpKeyPoint.h:649
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:499
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:577
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1439
void getTrainKeyPoints(std::vector< cv::KeyPoint > &keyPoints) const
static std::string getUserName()
Definition: vpIoTools.cpp:202
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:729
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:470
const char * what() const
void loadLearningData(const std::string &filename, const bool binaryMode=false, const bool append=false)
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
void saveLearningData(const std::string &filename, const bool binaryMode=false, const bool saveTrainingImages=true)
void reset()
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:787