Visual Servoing Platform  version 3.6.1 under development (2024-06-19)
tutorial-matching-keypoint-SIFT.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/io/vpImageIo.h>
5 #include <visp3/io/vpVideoReader.h>
7 #include <visp3/vision/vpKeyPoint.h>
9 
10 int main()
11 {
13 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D) && \
14  (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
15  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400))
17 #ifdef ENABLE_VISP_NAMESPACE
18  using namespace VISP_NAMESPACE_NAME;
19 #endif
21 
22  vpVideoReader reader;
23  reader.setFileName("video-postcard.mp4");
24  reader.acquire(I);
25 
27  const std::string detectorName = "SIFT";
28  const std::string extractorName = "SIFT";
29  // Use L2 distance with a matching done using FLANN (Fast Library for
30  // Approximate Nearest Neighbors)
31  const std::string matcherName = "FlannBased";
33  vpKeyPoint keypoint(detectorName, extractorName, matcherName, filterType);
35 
37  std::cout << "Reference keypoints=" << keypoint.buildReference(I) << std::endl;
39 
42  Idisp.resize(I.getHeight(), 2 * I.getWidth());
43  Idisp.insert(I, vpImagePoint(0, 0));
44  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
47  vpDisplayOpenCV d(Idisp, 0, 0, "Matching keypoints with SIFT keypoints");
48  vpDisplay::display(Idisp);
49  vpDisplay::flush(Idisp);
51 
52  while (!reader.end()) {
54  reader.acquire(I);
55  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
57 
59  vpDisplay::display(Idisp);
61  vpColor::white, 2);
63 
64  vpChrono chrono;
65  chrono.start();
67  unsigned int nbMatch = keypoint.matchPoint(I);
69  chrono.stop();
70  std::ostringstream oss;
71  oss << "Computation time: " << chrono.getDurationMs() << " ms";
72  vpDisplay::displayText(Idisp, vpImagePoint(20, 20), oss.str(), vpColor::red);
73 
74  std::cout << "Matches=" << nbMatch << std::endl;
75 
77  vpImagePoint iPref, iPcur;
78  for (unsigned int i = 0; i < nbMatch; i++) {
79  keypoint.getMatchedPoints(i, iPref, iPcur);
82  vpDisplay::displayLine(Idisp, iPref, iPcur + vpImagePoint(0, I.getWidth()), vpColor::green);
84  }
86  vpDisplay::flush(Idisp);
88 
89  if (vpDisplay::getClick(Idisp, false))
90  break;
91  }
92 
93  vpDisplay::getClick(Idisp);
94 #endif
95 
96  return EXIT_SUCCESS;
97 }
void start(bool reset=true)
Definition: vpTime.cpp:400
void stop()
Definition: vpTime.cpp:415
double getDurationMs()
Definition: vpTime.cpp:389
static const vpColor white
Definition: vpColor.h:208
static const vpColor red
Definition: vpColor.h:213
static const vpColor green
Definition: vpColor.h:216
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:246
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:799
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1610
unsigned int getHeight() const
Definition: vpImage.h:185
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:213
vpFilterMatchingType
Definition: vpKeyPoint.h:217
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:222
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 setFileName(const std::string &filename)