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