Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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) && defined(HAVE_OPENCV_FEATURES2D)
12 #ifdef ENABLE_VISP_NAMESPACE
13  using namespace VISP_NAMESPACE_NAME;
14 #endif
16  int method = 0;
17 
18  if (argc > 1)
19  method = atoi(argv[1]);
20 
21  if (method == 0)
22  std::cout << "Uses Ransac to estimate the homography" << std::endl;
23  else
24  std::cout << "Uses a robust scheme to estimate the homography" << std::endl;
26 
28 
29  vpVideoReader reader;
30  reader.setFileName("video-postcard.mp4");
31  reader.acquire(I);
32 
33  const std::string detectorName = "ORB";
34  const std::string extractorName = "ORB";
35  // Hamming distance must be used with ORB
36  const std::string matcherName = "BruteForce-Hamming";
38  vpKeyPoint keypoint(detectorName, extractorName, matcherName, filterType);
39  keypoint.buildReference(I);
40 
42  Idisp.resize(I.getHeight(), 2 * I.getWidth());
43  Idisp.insert(I, vpImagePoint(0, 0));
44  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
45 
46  vpDisplayOpenCV d(Idisp, 0, 0, "Homography from matched keypoints");
47  vpDisplay::display(Idisp);
48  vpDisplay::flush(Idisp);
49 
51  vpImagePoint corner_ref[4];
52  corner_ref[0].set_ij(115, 64);
53  corner_ref[1].set_ij(83, 253);
54  corner_ref[2].set_ij(282, 307);
55  corner_ref[3].set_ij(330, 72);
58  for (unsigned int i = 0; i < 4; i++) {
59  vpDisplay::displayCross(Idisp, corner_ref[i], 12, vpColor::red);
60  }
61  vpDisplay::flush(Idisp);
63 
65  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
67 
68  vpHomography curHref;
69 
70  while (!reader.end()) {
71  reader.acquire(I);
72  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
73  vpDisplay::display(Idisp);
75  vpColor::white, 2);
76 
78  unsigned int nbMatch = keypoint.matchPoint(I);
79  std::cout << "Nb matches: " << nbMatch << std::endl;
81 
82  std::vector<vpImagePoint> iPref(nbMatch),
83  iPcur(nbMatch); // Coordinates in pixels (for display only)
85  std::vector<double> mPref_x(nbMatch), mPref_y(nbMatch);
86  std::vector<double> mPcur_x(nbMatch), mPcur_y(nbMatch);
87  std::vector<bool> inliers(nbMatch);
89 
90  for (unsigned int i = 0; i < nbMatch; i++) {
91  keypoint.getMatchedPoints(i, iPref[i], iPcur[i]);
93  vpPixelMeterConversion::convertPoint(cam, iPref[i], mPref_x[i], mPref_y[i]);
94  vpPixelMeterConversion::convertPoint(cam, iPcur[i], mPcur_x[i], mPcur_y[i]);
96  }
97 
99  try {
100  double residual;
101  if (method == 0)
102  vpHomography::ransac(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
103  (unsigned int)(mPref_x.size() * 0.25), 2.0 / cam.get_px(), true);
104  else
105  vpHomography::robust(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual, 0.4, 4, true);
106  }
107  catch (...) {
108  std::cout << "Cannot compute homography from matches..." << std::endl;
109  }
110 
112 
114  vpImagePoint corner_cur[4];
115  for (int i = 0; i < 4; i++) {
116  corner_cur[i] = vpHomography::project(cam, curHref, corner_ref[i]);
117  }
119 
121  vpImagePoint offset(0, I.getWidth());
122  for (int i = 0; i < 4; i++) {
123  vpDisplay::displayLine(Idisp, corner_cur[i] + offset, corner_cur[(i + 1) % 4] + offset, vpColor::blue, 3);
124  }
126 
128  for (unsigned int i = 0; i < nbMatch; i++) {
129  if (inliers[i] == true)
130  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::green);
131  else
132  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::red);
133  }
135 
136  vpDisplay::flush(Idisp);
137 
138  if (vpDisplay::getClick(Idisp, false))
139  break;
140 }
141 
142  vpDisplay::getClick(Idisp);
143 #else
144  (void)argc;
145  (void)argv;
146 #endif
147  return EXIT_SUCCESS;
148 }
Generic class defining intrinsic camera parameters.
static const vpColor white
Definition: vpColor.h:212
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
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:542
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:637
unsigned int getHeight() const
Definition: vpImage.h:181
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:221
vpFilterMatchingType
Definition: vpKeyPoint.h:225
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:230
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)