Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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 (VISP_HAVE_OPENCV_VERSION >= 0x020101) && \
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 0;
93 }
double getDurationMs()
Definition: vpTime.cpp:392
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:800
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
void start(bool reset=true)
Definition: vpTime.cpp:409
void stop()
Definition: vpTime.cpp:424
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void acquire(vpImage< vpRGBa > &I)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1115
unsigned int getHeight() const
Definition: vpImage.h:188
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:222
vpFilterMatchingType
Definition: vpKeyPoint.h:227
void setFileName(const std::string &filename)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
static const vpColor white
Definition: vpColor.h:212
unsigned int getWidth() const
Definition: vpImage.h:246
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)