37 #include <visp3/core/vpConfig.h>
39 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D)
41 #include <opencv2/features2d/features2d.hpp>
43 #include <visp3/core/vpImage.h>
44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/gui/vpDisplayGDI.h>
46 #include <visp3/gui/vpDisplayGTK.h>
47 #include <visp3/gui/vpDisplayOpenCV.h>
48 #include <visp3/gui/vpDisplayX.h>
49 #include <visp3/io/vpImageIo.h>
50 #include <visp3/io/vpParseArgv.h>
51 #include <visp3/io/vpVideoReader.h>
54 #define GETOPTARGS "cdh"
56 void usage(
const char *name,
const char *badparam);
57 bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display);
66 void usage(
const char *name,
const char *badparam)
69 Test keypoints matching.\n\
79 Disable the mouse click. Useful to automate the \n\
80 execution of this program without human intervention.\n\
83 Turn off the display.\n\
89 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
103 bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display)
111 click_allowed =
false;
117 usage(argv[0],
nullptr);
122 usage(argv[0], optarg_);
128 if ((c == 1) || (c == -1)) {
130 usage(argv[0],
nullptr);
131 std::cerr <<
"ERROR: " << std::endl;
132 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
139 template <
typename Type>
140 void run_test(
const std::string &env_ipath,
bool opt_click_allowed,
bool opt_display,
vpImage<Type> &Iref,
143 #if VISP_HAVE_DATASET_VERSION >= 0x030600
144 std::string ext(
"png");
146 std::string ext(
"pgm");
157 cv::Ptr<cv::FeatureDetector> detector;
158 cv::Ptr<cv::DescriptorExtractor> extractor;
159 cv::Ptr<cv::DescriptorMatcher> matcher;
161 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
162 detector = cv::ORB::create();
163 extractor = cv::ORB::create();
165 detector = cv::FeatureDetector::create(
"ORB");
166 extractor = cv::DescriptorExtractor::create(
"ORB");
168 matcher = cv::DescriptorMatcher::create(
"BruteForce-Hamming");
170 std::vector<cv::KeyPoint> trainKeyPoints;
171 cv::Mat matImg, trainDescriptors;
173 detector->detect(matImg, trainKeyPoints);
174 extractor->compute(matImg, trainKeyPoints, trainDescriptors);
184 #if defined(VISP_HAVE_X11)
186 #elif defined(VISP_HAVE_GTK)
188 #elif defined(VISP_HAVE_GDI)
190 #elif defined(HAVE_OPENCV_HIGHGUI)
196 display.init(Imatch, 0, 0,
"ORB keypoints matching");
199 bool opt_click =
false;
210 std::vector<cv::KeyPoint> queryKeyPoints;
211 detector->detect(matImg, queryKeyPoints);
213 cv::Mat queryDescriptors;
214 extractor->compute(matImg, queryKeyPoints, queryDescriptors);
216 std::vector<std::vector<cv::DMatch> > knn_matches;
217 std::vector<cv::DMatch> matches;
218 matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
219 for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
221 if (it->size() > 1) {
222 double ratio = (*it)[0].distance / (*it)[1].distance;
224 matches.push_back((*it)[0]);
230 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
231 vpImagePoint leftPt(trainKeyPoints[(
size_t)it->trainIdx].pt.y, trainKeyPoints[(
size_t)it->trainIdx].pt.x);
232 vpImagePoint rightPt(queryKeyPoints[(
size_t)it->queryIdx].pt.y,
233 queryKeyPoints[(
size_t)it->queryIdx].pt.x + Iref.
getWidth());
241 if (opt_click_allowed && opt_display) {
267 int main(
int argc,
const char **argv)
270 std::string env_ipath;
271 bool opt_click_allowed =
true;
272 bool opt_display =
true;
275 if (getOptions(argc, argv, opt_click_allowed, opt_display) ==
false) {
283 if (env_ipath.empty()) {
284 std::cerr <<
"Please set the VISP_INPUT_IMAGE_PATH environment "
293 std::cout <<
"-- Test on gray level images" << std::endl;
294 run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
300 std::cout <<
"-- Test on color images" << std::endl;
301 run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
305 std::cerr << e.
what() << std::endl;
309 std::cout <<
"testKeyPoint-3 is ok !" << std::endl;
316 std::cerr <<
"You need OpenCV library." << std::endl;
static const vpColor green
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
const char * what() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.