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;
143 template<
typename Type>
144 void run_test(
const std::string &env_ipath,
bool opt_click_allowed,
bool opt_display,
156 cv::Ptr<cv::FeatureDetector> detector;
157 cv::Ptr<cv::DescriptorExtractor> extractor;
158 cv::Ptr<cv::DescriptorMatcher> matcher;
160 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
161 detector = cv::ORB::create();
162 extractor = cv::ORB::create();
164 detector = cv::FeatureDetector::create(
"ORB");
165 extractor = cv::DescriptorExtractor::create(
"ORB");
167 matcher = cv::DescriptorMatcher::create(
"BruteForce-Hamming");
169 std::vector<cv::KeyPoint> trainKeyPoints;
170 cv::Mat matImg, trainDescriptors;
172 detector->
detect(matImg, trainKeyPoints);
173 extractor->compute(matImg, trainKeyPoints, trainDescriptors);
183 #if defined VISP_HAVE_X11
185 #elif defined VISP_HAVE_GTK
187 #elif defined VISP_HAVE_GDI
195 display.init(Imatch, 0, 0,
"ORB keypoints matching");
198 bool opt_click =
false;
209 std::vector<cv::KeyPoint> queryKeyPoints;
210 detector->
detect(matImg, queryKeyPoints);
212 cv::Mat queryDescriptors;
213 extractor->compute(matImg, queryKeyPoints, queryDescriptors);
215 std::vector<std::vector<cv::DMatch> > knn_matches;
216 std::vector<cv::DMatch> matches;
217 matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
218 for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
220 if (it->size() > 1) {
221 double ratio = (*it)[0].distance / (*it)[1].distance;
223 matches.push_back((*it)[0]);
229 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
230 vpImagePoint leftPt(trainKeyPoints[(
size_t)it->trainIdx].pt.y, trainKeyPoints[(
size_t)it->trainIdx].pt.x);
231 vpImagePoint rightPt(queryKeyPoints[(
size_t)it->queryIdx].pt.y,
232 queryKeyPoints[(
size_t)it->queryIdx].pt.x + Iref.
getWidth());
240 if (opt_click_allowed && opt_display) {
266 int main(
int argc,
const char **argv)
269 std::string env_ipath;
270 bool opt_click_allowed =
true;
271 bool opt_display =
true;
274 if (getOptions(argc, argv, opt_click_allowed, opt_display) ==
false) {
282 if (env_ipath.empty()) {
283 std::cerr <<
"Please set the VISP_INPUT_IMAGE_PATH environment "
292 std::cout <<
"-- Test on gray level images" << std::endl;
293 run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
299 std::cout <<
"-- Test on color images" << std::endl;
300 run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
304 std::cerr << e.
what() << std::endl;
308 std::cout <<
"testKeyPoint-3 is ok !" << std::endl;
314 std::cerr <<
"You need OpenCV library." << std::endl;