42 #include <visp3/core/vpConfig.h>
44 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
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>
54 #define GETOPTARGS "cdo:h"
64 void usage(
const char *name,
const char *badparam,
const std::string &opath,
const std::string &user)
67 Test save / load learning files for vpKeyPoint class.\n\
70 %s [-c] [-d] [-h]\n", name);
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\
82 Print the help.\n", opath.c_str(), user.c_str());
85 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
99 bool getOptions(
int argc,
const char **argv, std::string &opath,
const std::string &user)
114 usage(argv[0], NULL, opath, user);
119 usage(argv[0], optarg_, opath, user);
127 if ((c == 1) || (c == -1)) {
129 usage(argv[0], NULL, opath, user);
130 std::cerr <<
"ERROR: " << std::endl;
131 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
146 bool compareKeyPoints(
const std::vector<cv::KeyPoint> &keypoints1,
const std::vector<cv::KeyPoint> &keypoints2)
148 if (keypoints1.size() != keypoints2.size()) {
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;
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;
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;
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;
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;
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;
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;
207 bool compareDescriptors(
const cv::Mat &descriptors1,
const cv::Mat &descriptors2)
209 if (descriptors1.rows != descriptors2.rows || descriptors1.cols != descriptors2.cols ||
210 descriptors1.type() != descriptors2.type()) {
214 for (
int i = 0; i < descriptors1.rows; i++) {
215 for (
int j = 0; j < descriptors1.cols; j++) {
216 switch (descriptors1.type()) {
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;
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;
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;
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;
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;
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;
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;
292 int main(
int argc,
const char **argv)
295 std::string env_ipath;
296 std::string opt_opath;
297 std::string username;
299 std::string filename;
305 if (env_ipath.empty()) {
311 opt_opath =
"C:/temp";
320 if (getOptions(argc, argv, opt_opath, username) ==
false) {
325 if (!opt_opath.empty()) {
345 std::string keypointName =
"ORB";
351 std::vector<cv::KeyPoint> trainKeyPoints;
354 if (trainKeyPoints.empty() || trainDescriptors.empty() || (int)trainKeyPoints.size() != trainDescriptors.rows) {
357 "computing descriptors !");
368 std::stringstream ss;
369 ss <<
"Problem when saving file=" << filename;
376 std::vector<cv::KeyPoint> trainKeyPoints_read;
380 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
382 "in binary with train images saved !");
385 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
387 "learning file saved in "
388 "binary with train images saved !");
399 std::stringstream ss;
400 ss <<
"Problem when saving file=" << filename;
407 trainKeyPoints_read.clear();
411 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
413 "binary without train images !");
416 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
418 "learning file saved in "
419 "binary without train images !");
422 #if defined(VISP_HAVE_XML2)
431 std::stringstream ss;
432 ss <<
"Problem when saving file=" << filename;
439 trainKeyPoints_read.clear();
443 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
445 "xml with train images saved !");
448 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
450 "learning file saved in "
451 "xml with train images saved !");
462 std::stringstream ss;
463 ss <<
"Problem when saving file=" << filename;
470 trainKeyPoints_read.clear();
474 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
476 "xml without train images saved !");
479 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
481 "learning file saved in "
482 "xml without train images saved !");
486 std::cout <<
"Saving / loading learning files with binary descriptor are ok !" << std::endl;
490 #if defined(VISP_HAVE_OPENCV_NONFREE) || \
491 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(VISP_HAVE_OPENCV_XFEATURES2D))
493 std::string keypointName =
"SIFT";
499 std::vector<cv::KeyPoint> trainKeyPoints;
502 if (trainKeyPoints.empty() || trainDescriptors.empty() || (int)trainKeyPoints.size() != trainDescriptors.rows) {
504 "computing descriptors (SIFT) !");
515 std::stringstream ss;
516 ss <<
"Problem when saving file=" << filename;
523 std::vector<cv::KeyPoint> trainKeyPoints_read;
527 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
529 "binary with train images saved !");
532 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
534 "learning file saved in "
535 "binary with train images saved !");
546 std::stringstream ss;
547 ss <<
"Problem when saving file=" << filename;
554 trainKeyPoints_read.clear();
558 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
560 "binary without train images saved !");
563 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
565 "learning file saved in "
566 "binary without train images saved !");
569 #if defined(VISP_HAVE_XML2)
578 std::stringstream ss;
579 ss <<
"Problem when saving file=" << filename;
586 trainKeyPoints_read.clear();
590 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
592 "xml with train images saved !");
595 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
597 "learning file saved in "
598 "xml with train images saved !");
609 std::stringstream ss;
610 ss <<
"Problem when saving file=" << filename;
617 trainKeyPoints_read.clear();
621 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_read)) {
623 "xml without train images saved !");
626 if (!compareDescriptors(trainDescriptors, trainDescriptors_read)) {
628 "learning file saved in "
629 "xml without train images saved !");
633 std::cout <<
"Saving / loading learning files with floating point "
634 "descriptor are ok !"
640 keypointName =
"ORB";
647 keypoint_reset.
reset();
649 keypointName =
"SIFT";
655 std::vector<cv::KeyPoint> trainKeyPoints_reset;
661 if (!compareKeyPoints(trainKeyPoints, trainKeyPoints_reset)) {
665 if (!compareDescriptors(trainDescriptors, trainDescriptors_reset)) {
669 std::cout <<
"vpKeyPoint::reset() is ok with trainKeyPoints and "
676 std::cerr << e.
what() << std::endl;
680 std::cout <<
"Saving / loading learning files are ok !" << std::endl;
681 std::cout <<
"testKeyPoint-7 is ok !" << std::endl;
687 std::cerr <<
"You need OpenCV library." << std::endl;