42 #include <visp3/core/vpConfig.h>
44 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
46 #include <opencv2/core/core.hpp>
47 #include <opencv2/features2d/features2d.hpp>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayGTK.h>
52 #include <visp3/gui/vpDisplayOpenCV.h>
53 #include <visp3/gui/vpDisplayX.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/io/vpVideoReader.h>
59 #define GETOPTARGS "cdh"
61 void usage(
const char *name,
const char *badparam);
62 bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display);
71 void usage(
const char *name,
const char *badparam)
74 Test keypoints matching.\n\
77 %s [-c] [-d] [-h]\n", name);
83 Disable the mouse click. Useful to automate the \n\
84 execution of this program without human intervention.\n\
87 Turn off the display.\n\
93 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
107 bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display)
115 click_allowed =
false;
121 usage(argv[0], NULL);
126 usage(argv[0], optarg_);
132 if ((c == 1) || (c == -1)) {
134 usage(argv[0], NULL);
135 std::cerr <<
"ERROR: " << std::endl;
136 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
149 int main(
int argc,
const char **argv)
152 std::string env_ipath;
153 bool opt_click_allowed =
true;
154 bool opt_display =
true;
157 if (getOptions(argc, argv, opt_click_allowed, opt_display) ==
false) {
165 if (env_ipath.empty()) {
166 std::cerr <<
"Please set the VISP_INPUT_IMAGE_PATH environment "
183 cv::Ptr<cv::FeatureDetector> detector;
184 cv::Ptr<cv::DescriptorExtractor> extractor;
185 cv::Ptr<cv::DescriptorMatcher> matcher;
187 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
188 detector = cv::ORB::create();
189 extractor = cv::ORB::create();
191 detector = cv::FeatureDetector::create(
"ORB");
192 extractor = cv::DescriptorExtractor::create(
"ORB");
194 matcher = cv::DescriptorMatcher::create(
"BruteForce-Hamming");
196 std::vector<cv::KeyPoint> trainKeyPoints;
197 cv::Mat matImg, trainDescriptors;
199 detector->detect(matImg, trainKeyPoints);
200 extractor->compute(matImg, trainKeyPoints, trainDescriptors);
210 #if defined VISP_HAVE_X11
212 #elif defined VISP_HAVE_GTK
214 #elif defined VISP_HAVE_GDI
222 display.init(Imatch, 0, 0,
"ORB keypoints matching");
225 bool opt_click =
false;
236 std::vector<cv::KeyPoint> queryKeyPoints;
237 detector->detect(matImg, queryKeyPoints);
239 cv::Mat queryDescriptors;
240 extractor->compute(matImg, queryKeyPoints, queryDescriptors);
242 std::vector<std::vector<cv::DMatch> > knn_matches;
243 std::vector<cv::DMatch> matches;
244 matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
245 for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
247 if (it->size() > 1) {
248 double ratio = (*it)[0].distance / (*it)[1].distance;
250 matches.push_back((*it)[0]);
256 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
257 vpImagePoint leftPt(trainKeyPoints[(
size_t)it->trainIdx].pt.y, trainKeyPoints[(
size_t)it->trainIdx].pt.x);
258 vpImagePoint rightPt(queryKeyPoints[(
size_t)it->queryIdx].pt.y,
259 queryKeyPoints[(
size_t)it->queryIdx].pt.x + Iref.
getWidth());
267 if (opt_click_allowed && opt_display) {
287 std::cerr << e.
what() << std::endl;
291 std::cout <<
"testKeyPoint-3 is ok !" << std::endl;
297 std::cerr <<
"You need OpenCV library." << std::endl;