ViSP  2.9.0
tutorial-matching-surf-homography.cpp
1 
2 #include <visp/vpDisplayOpenCV.h>
3 #include <visp/vpHomography.h>
4 #include <visp/vpKeyPointSurf.h>
5 #include <visp/vpPixelMeterConversion.h>
6 #include <visp/vpVideoReader.h>
7 
8 int main(int argc, const char **argv)
9 {
10 #if defined(VISP_HAVE_OPENCV_NONFREE) && defined(VISP_HAVE_FFMPEG)
11  int method = 0;
12 
13  if (argc > 1)
14  method = atoi(argv[1]);
15 
16  if (method == 0)
17  std::cout << "Uses Ransac to estimate the homography" << std::endl;
18  else
19  std::cout << "Uses a robust scheme to estimate the homography" << std::endl;
20 
22 
23  vpVideoReader reader;
24  reader.setFileName("video-postcard.mpeg");
25  reader.acquire(I);
26 
27  vpKeyPointSurf surf;
28  surf.buildReference(I);
29 
31  Idisp.resize(I.getHeight(), 2*I.getWidth());
32  Idisp.insert(I, vpImagePoint(0, 0));
33  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
34 
35  vpDisplayOpenCV d(Idisp, 0, 0, "Homography from matched Surf keypoints") ;
36  vpDisplay::display(Idisp);
37  vpDisplay::flush(Idisp);
38 
39  vpImagePoint corner_ref[4];
40  corner_ref[0].set_ij(115, 64);
41  corner_ref[1].set_ij( 83, 253);
42  corner_ref[2].set_ij(282, 307);
43  corner_ref[3].set_ij(330, 72);
44  for (unsigned int i=0; i<4; i++) {
45  vpDisplay::displayCross(Idisp, corner_ref[i], 12, vpColor::red);
46  }
47  vpDisplay::flush(Idisp);
48 
49  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
50 
51  vpHomography curHref;
52 
53  while ( ! reader.end() )
54  {
55  reader.acquire(I);
56  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
57  vpDisplay::display(Idisp);
59 
60  unsigned int nbMatch = surf.matchPoint(I);
61 
62  std::vector<vpImagePoint> iPref(nbMatch), iPcur(nbMatch); // Coordinates in pixels (for display only)
63  std::vector<double> mPref_x(nbMatch), mPref_y(nbMatch);
64  std::vector<double> mPcur_x(nbMatch), mPcur_y(nbMatch);
65  std::vector<bool> inliers(nbMatch);
66 
67  for (unsigned int i = 0; i < nbMatch; i++) {
68  vpImagePoint matched_ref, matched_cur;
69  surf.getMatchedPoints(i, matched_ref, matched_cur);
70  vpPixelMeterConversion::convertPoint(cam, matched_ref, mPref_x[i], mPref_y[i]);
71  vpPixelMeterConversion::convertPoint(cam, matched_cur, mPcur_x[i], mPcur_y[i]);
72 
73  // Store the image coordinates in pixel of the matched points
74  iPref[i] = matched_ref;
75  iPcur[i] = matched_cur;
76  }
77 
78  double residual;
79  if (method == 0)
80  vpHomography::ransac(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
81  mPref_x.size()/2, 2.0/cam.get_px(), true);
82  else
83  vpHomography::robust(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
84  0.4, 4, true);
85 
86  vpImagePoint corner_cur[4];
87  for (int i=0; i< 4; i++) {
88  corner_cur[i] = vpHomography::project(cam, curHref, corner_ref[i]);
89  }
90 
91  vpImagePoint offset(0, I.getWidth());
92  for (int i=0; i< 4; i++) {
94  corner_cur[i] + offset,
95  corner_cur[(i+1)%4] + offset,
96  vpColor::blue, 3);
97  }
98 
99  for (unsigned int i = 0; i < nbMatch; i++) {
100  if(inliers[i] == true)
101  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::green);
102  else
103  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::red);
104  }
105 
106  vpDisplay::flush(Idisp);
107 
108  if (vpDisplay::getClick(Idisp, false))
109  break;
110  }
111 
112  vpDisplay::getClick(Idisp);
113 #else
114  (void)argc; (void)argv;
115 #endif
116  return 0;
117 }
static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa)
unsigned int getWidth() const
Definition: vpImage.h:159
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
unsigned int matchPoint(const vpImage< unsigned char > &I)
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...
void getMatchedPoints(const unsigned int index, vpImagePoint &referencePoint, vpImagePoint &currentPoint)
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static const vpColor red
Definition: vpColor.h:167
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
Class that implements the SURF key points and technics thanks to the OpenCV library.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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
Definition: vpImage.h:532
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)
unsigned int buildReference(const vpImage< unsigned char > &I)
void insert(const vpImage< Type > &src, const vpImagePoint topLeft)
Definition: vpImage.h:816
unsigned int getHeight() const
Definition: vpImage.h:150
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:92
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:180
static const vpColor blue
Definition: vpColor.h:173