ViSP  2.10.0
tutorial-matching-keypoint-homography.cpp
1 #include <visp/vpDisplayOpenCV.h>
3 #include <visp/vpHomography.h>
4 #include <visp/vpKeyPoint.h>
5 #include <visp/vpPixelMeterConversion.h>
6 #include <visp/vpVideoReader.h>
7 
8 int main(int argc, const char **argv)
9 {
10 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
11  int method = 0;
13 
14  if (argc > 1)
15  method = atoi(argv[1]);
16 
17  if (method == 0)
18  std::cout << "Uses Ransac to estimate the homography" << std::endl;
19  else
20  std::cout << "Uses a robust scheme to estimate the homography" << std::endl;
22 
24 
25  vpVideoReader reader;
26  reader.setFileName("video-postcard.mpeg");
27  reader.acquire(I);
28 
29  const std::string detectorName = "ORB";
30  const std::string extractorName = "ORB";
31  //Hamming distance must be used with ORB
32  const std::string matcherName = "BruteForce-Hamming";
34  vpKeyPoint keypoint(detectorName, extractorName, matcherName, filterType);
35  keypoint.buildReference(I);
36 
38  Idisp.resize(I.getHeight(), 2*I.getWidth());
39  Idisp.insert(I, vpImagePoint(0, 0));
40  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
41 
42  vpDisplayOpenCV d(Idisp, 0, 0, "Homography from matched keypoints") ;
43  vpDisplay::display(Idisp);
44  vpDisplay::flush(Idisp);
45 
47  vpImagePoint corner_ref[4];
48  corner_ref[0].set_ij(115, 64);
49  corner_ref[1].set_ij( 83, 253);
50  corner_ref[2].set_ij(282, 307);
51  corner_ref[3].set_ij(330, 72);
54  for (unsigned int i=0; i<4; i++) {
55  vpDisplay::displayCross(Idisp, corner_ref[i], 12, vpColor::red);
56  }
57  vpDisplay::flush(Idisp);
59 
61  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
63 
64  vpHomography curHref;
65 
66  while ( ! reader.end() )
67  {
68  reader.acquire(I);
69  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
70  vpDisplay::display(Idisp);
72 
74  unsigned int nbMatch = keypoint.matchPoint(I);
75  std::cout << "Nb matches: " << nbMatch << std::endl;
77 
78  std::vector<vpImagePoint> iPref(nbMatch), iPcur(nbMatch); // Coordinates in pixels (for display only)
80  std::vector<double> mPref_x(nbMatch), mPref_y(nbMatch);
81  std::vector<double> mPcur_x(nbMatch), mPcur_y(nbMatch);
82  std::vector<bool> inliers(nbMatch);
84 
85  for (unsigned int i = 0; i < nbMatch; i++) {
86  vpImagePoint matched_ref, matched_cur;
87  keypoint.getMatchedPoints(i, matched_ref, matched_cur);
89  vpPixelMeterConversion::convertPoint(cam, matched_ref, mPref_x[i], mPref_y[i]);
90  vpPixelMeterConversion::convertPoint(cam, matched_cur, mPcur_x[i], mPcur_y[i]);
92 
93  // Store the image coordinates in pixel of the matched points
94  iPref[i] = matched_ref;
95  iPcur[i] = matched_cur;
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,
106  0.4, 4, true);
107  } catch(...)
108  {
109  std::cout << "Cannot compute homography from matches..." << std::endl;
110  }
111 
113 
115  vpImagePoint corner_cur[4];
116  for (int i=0; i< 4; i++) {
117  corner_cur[i] = vpHomography::project(cam, curHref, corner_ref[i]);
118  }
120 
122  vpImagePoint offset(0, I.getWidth());
123  for (int i=0; i< 4; i++) {
125  corner_cur[i] + offset,
126  corner_cur[(i+1)%4] + offset,
127  vpColor::blue, 3);
128  }
130 
132  for (unsigned int i = 0; i < nbMatch; i++) {
133  if(inliers[i] == true)
134  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::green);
135  else
136  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::red);
137  }
139 
140  vpDisplay::flush(Idisp);
141 
142  if (vpDisplay::getClick(Idisp, false))
143  break;
144  }
145 
146  vpDisplay::getClick(Idisp);
147 #else
148  (void)argc; (void)argv;
149 #endif
150  return 0;
151 }
static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa)
unsigned int getWidth() const
Definition: vpImage.h:161
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static const vpColor red
Definition: vpColor.h:167
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
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)
void acquire(vpImage< vpRGBa > &I)
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
Definition: vpImage.h:536
void setFileName(const char *filename)
double get_px() const
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 > &inlier, double &residual, double weights_threshold=0.4, unsigned int niter=4, bool normalization=true)
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:212
void insert(const vpImage< Type > &src, const vpImagePoint topLeft)
Definition: vpImage.h:855
vpFilterMatchingType
Definition: vpKeyPoint.h:217
unsigned int getHeight() const
Definition: vpImage.h:152
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
static const vpColor white
Definition: vpColor.h:162
void set_ij(const double ii, const double jj)
Definition: vpImagePoint.h:181
static const vpColor blue
Definition: vpColor.h:173