Test keypoint matching with mostly OpenCV functions calls to detect potential memory leaks in testKeyPoint.cpp.
#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <visp3/core/vpImage.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpParseArgv.h>
#define GETOPTARGS "cdh"
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
Test keypoints matching.\n\
\n\
SYNOPSIS\n\
%s [-c] [-d] [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: \n\
\n\
-c\n\
Disable the mouse click. Useful to automate the \n\
execution of this program without human intervention.\n\
\n\
-d \n\
Turn off the display.\n\
\n\
-h\n\
Print the help.\n");
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
{
const char *optarg_;
int c;
switch (c) {
case 'c': click_allowed = false; break;
case 'd': display = false; break;
case 'h': usage(argv[0], NULL); return false; break;
default:
usage(argv[0], optarg_);
return false; break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
int main(int argc, const char ** argv) {
try {
std::string env_ipath;
bool opt_click_allowed = true;
bool opt_display = true;
if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
exit (-1);
}
if(env_ipath.empty()) {
std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment variable value." << std::endl;
return -1;
}
cv::Ptr<cv::FeatureDetector> detector;
cv::Ptr<cv::DescriptorExtractor> extractor;
cv::Ptr<cv::DescriptorMatcher> matcher;
#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
detector = cv::ORB::create();
extractor = cv::ORB::create();
#else
detector = cv::FeatureDetector::create("ORB");
extractor = cv::DescriptorExtractor::create("ORB");
#endif
matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
std::vector<cv::KeyPoint> trainKeyPoints;
cv::Mat matImg, trainDescriptors;
detector->detect(matImg, trainKeyPoints);
extractor->compute(matImg, trainKeyPoints, trainDescriptors);
#if defined VISP_HAVE_X11
#elif defined VISP_HAVE_GTK
#elif defined VISP_HAVE_GDI
#else
#endif
if (opt_display) {
display.
init(Imatch, 0, 0,
"ORB keypoints matching");
}
bool opt_click = false;
if(opt_display) {
}
std::vector<cv::KeyPoint> queryKeyPoints;
detector->detect(matImg, queryKeyPoints);
cv::Mat queryDescriptors;
extractor->compute(matImg, queryKeyPoints, queryDescriptors);
std::vector<std::vector<cv::DMatch> > knn_matches;
std::vector<cv::DMatch> matches;
matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
for(std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end(); ++it) {
if(it->size() > 1) {
double ratio = (*it)[0].distance / (*it)[1].distance;
if(ratio < 0.85) {
matches.push_back((*it)[0]);
}
}
}
if(opt_display) {
for(std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
vpImagePoint leftPt(trainKeyPoints[(
size_t) it->trainIdx].pt.y, trainKeyPoints[(
size_t) it->trainIdx].pt.x);
vpImagePoint rightPt(queryKeyPoints[(
size_t) it->queryIdx].pt.y, queryKeyPoints[(
size_t) it->queryIdx].pt.x
}
}
if (opt_click_allowed && opt_display) {
if(opt_click) {
opt_click = false;
}
} else {
opt_click = true;
}
break;
}
}
}
}
}
std::cerr << e.
what() << std::endl;
return -1;
}
std::cout << "testKeyPoint-3 is ok !" << std::endl;
return 0;
}
#else
int main() {
std::cerr << "You need OpenCV library." << std::endl;
return 0;
}
#endif