Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
tutorial-matching-keypoint-SIFT.cpp
1 #include <visp3/gui/vpDisplayOpenCV.h>
3 #include <visp3/io/vpImageIo.h>
4 #include <visp3/io/vpVideoReader.h>
6 #include <visp3/vision/vpKeyPoint.h>
8 
9 int main()
10 {
12 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D) && \
13  (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
14  (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400))
17 
18  vpVideoReader reader;
19  reader.setFileName("video-postcard.mp4");
20  reader.acquire(I);
21 
23  const std::string detectorName = "SIFT";
24  const std::string extractorName = "SIFT";
25  // Use L2 distance with a matching done using FLANN (Fast Library for
26  // Approximate Nearest Neighbors)
27  const std::string matcherName = "FlannBased";
29  vpKeyPoint keypoint(detectorName, extractorName, matcherName, filterType);
31 
33  std::cout << "Reference keypoints=" << keypoint.buildReference(I) << std::endl;
35 
38  Idisp.resize(I.getHeight(), 2 * I.getWidth());
39  Idisp.insert(I, vpImagePoint(0, 0));
40  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
43  vpDisplayOpenCV d(Idisp, 0, 0, "Matching keypoints with SIFT keypoints");
44  vpDisplay::display(Idisp);
45  vpDisplay::flush(Idisp);
47 
48  while (!reader.end()) {
50  reader.acquire(I);
51  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
53 
55  vpDisplay::display(Idisp);
57  vpColor::white, 2);
59 
60  vpChrono chrono;
61  chrono.start();
63  unsigned int nbMatch = keypoint.matchPoint(I);
65  chrono.stop();
66  std::ostringstream oss;
67  oss << "Computation time: " << chrono.getDurationMs() << " ms";
68  vpDisplay::displayText(Idisp, vpImagePoint(20, 20), oss.str(), vpColor::red);
69 
70  std::cout << "Matches=" << nbMatch << std::endl;
71 
73  vpImagePoint iPref, iPcur;
74  for (unsigned int i = 0; i < nbMatch; i++) {
75  keypoint.getMatchedPoints(i, iPref, iPcur);
78  vpDisplay::displayLine(Idisp, iPref, iPcur + vpImagePoint(0, I.getWidth()), vpColor::green);
80  }
82  vpDisplay::flush(Idisp);
84 
85  if (vpDisplay::getClick(Idisp, false))
86  break;
87  }
88 
89  vpDisplay::getClick(Idisp);
90 #endif
91 
92  return EXIT_SUCCESS;
93 }
void start(bool reset=true)
Definition: vpTime.cpp:399
void stop()
Definition: vpTime.cpp:414
double getDurationMs()
Definition: vpTime.cpp:388
static const vpColor white
Definition: vpColor.h:206
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
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:245
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:783
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1533
unsigned int getHeight() const
Definition: vpImage.h:184
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:212
vpFilterMatchingType
Definition: vpKeyPoint.h:216
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:221
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)