Visual Servoing Platform  version 3.6.1 under development (2025-02-01)
tutorial-matching-keypoint-homography.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpPixelMeterConversion.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/io/vpVideoReader.h>
6 #include <visp3/vision/vpHomography.h>
7 #include <visp3/vision/vpKeyPoint.h>
8 
9 int main(int argc, const char **argv)
10 {
11 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && \
12  ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
13  ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES))
14 
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
19  int method = 0;
20 
21  if (argc > 1)
22  method = atoi(argv[1]);
23 
24  if (method == 0)
25  std::cout << "Uses Ransac to estimate the homography" << std::endl;
26  else
27  std::cout << "Uses a robust scheme to estimate the homography" << std::endl;
29 
31 
32  vpVideoReader reader;
33  reader.setFileName("video-postcard.mp4");
34  reader.acquire(I);
35 
36  const std::string detectorName = "ORB";
37  const std::string extractorName = "ORB";
38  // Hamming distance must be used with ORB
39  const std::string matcherName = "BruteForce-Hamming";
41  vpKeyPoint keypoint(detectorName, extractorName, matcherName, filterType);
42  keypoint.buildReference(I);
43 
45  Idisp.resize(I.getHeight(), 2 * I.getWidth());
46  Idisp.insert(I, vpImagePoint(0, 0));
47  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
48 
49  vpDisplayOpenCV d(Idisp, 0, 0, "Homography from matched keypoints");
50  vpDisplay::display(Idisp);
51  vpDisplay::flush(Idisp);
52 
54  vpImagePoint corner_ref[4];
55  corner_ref[0].set_ij(115, 64);
56  corner_ref[1].set_ij(83, 253);
57  corner_ref[2].set_ij(282, 307);
58  corner_ref[3].set_ij(330, 72);
61  for (unsigned int i = 0; i < 4; i++) {
62  vpDisplay::displayCross(Idisp, corner_ref[i], 12, vpColor::red);
63  }
64  vpDisplay::flush(Idisp);
66 
68  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
70 
71  vpHomography curHref;
72 
73  while (!reader.end()) {
74  reader.acquire(I);
75  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
76  vpDisplay::display(Idisp);
78  vpColor::white, 2);
79 
81  unsigned int nbMatch = keypoint.matchPoint(I);
82  std::cout << "Nb matches: " << nbMatch << std::endl;
84 
85  std::vector<vpImagePoint> iPref(nbMatch), iPcur(nbMatch); // Coordinates in pixels (for display only)
87  std::vector<double> mPref_x(nbMatch), mPref_y(nbMatch);
88  std::vector<double> mPcur_x(nbMatch), mPcur_y(nbMatch);
89  std::vector<bool> inliers(nbMatch);
91 
92  for (unsigned int i = 0; i < nbMatch; i++) {
93  keypoint.getMatchedPoints(i, iPref[i], iPcur[i]);
95  vpPixelMeterConversion::convertPoint(cam, iPref[i], mPref_x[i], mPref_y[i]);
96  vpPixelMeterConversion::convertPoint(cam, iPcur[i], mPcur_x[i], mPcur_y[i]);
98  }
99 
101  try {
102  double residual;
103  if (method == 0)
104  vpHomography::ransac(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
105  (unsigned int)(mPref_x.size() * 0.25), 2.0 / cam.get_px(), true);
106  else
107  vpHomography::robust(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual, 0.4, 4, true);
108  }
109  catch (...) {
110  std::cout << "Cannot compute homography from matches..." << std::endl;
111  }
112 
114 
116  vpImagePoint corner_cur[4];
117  for (int i = 0; i < 4; i++) {
118  corner_cur[i] = vpHomography::project(cam, curHref, corner_ref[i]);
119  }
121 
123  vpImagePoint offset(0, I.getWidth());
124  for (int i = 0; i < 4; i++) {
125  vpDisplay::displayLine(Idisp, corner_cur[i] + offset, corner_cur[(i + 1) % 4] + offset, vpColor::blue, 3);
126  }
128 
130  for (unsigned int i = 0; i < nbMatch; i++) {
131  if (inliers[i] == true)
132  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::green);
133  else
134  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::red);
135  }
137 
138  vpDisplay::flush(Idisp);
139 
140  if (vpDisplay::getClick(Idisp, false))
141  break;
142  }
143 
144  vpDisplay::getClick(Idisp);
145 #else
146  (void)argc;
147  (void)argv;
148 #endif
149  return EXIT_SUCCESS;
150 }
Generic class defining intrinsic camera parameters.
static const vpColor white
Definition: vpColor.h:193
static const vpColor red
Definition: vpColor.h:198
static const vpColor blue
Definition: vpColor.h:204
static const vpColor green
Definition: vpColor.h:201
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 displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa)
static void robust(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, std::vector< bool > &inliers, double &residual, double weights_threshold=0.4, unsigned int niter=4, bool normalization=true)
static bool ransac(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, std::vector< bool > &inliers, double &residual, unsigned int nbInliersConsensus, double threshold, bool normalization=true)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_ij(double ii, double jj)
Definition: vpImagePoint.h:320
unsigned int getWidth() const
Definition: vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:544
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:639
unsigned int getHeight() const
Definition: vpImage.h:181
Class that allows keypoints 2D features detection (and descriptors extraction) and matching thanks to...
Definition: vpKeyPoint.h:267
vpFilterMatchingType
Definition: vpKeyPoint.h:271
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:276
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
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)