Visual Servoing Platform  version 3.0.0
tutorial-matching-surf-homography-deprecated.cpp
1 #include <visp3/gui/vpDisplayOpenCV.h>
3 #include <visp3/vision/vpHomography.h>
4 #include <visp3/vision/vpKeyPointSurf.h>
5 #include <visp3/core/vpPixelMeterConversion.h>
6 #include <visp3/io/vpVideoReader.h>
7 
8 int main(int argc, const char **argv)
9 {
10 #if defined(VISP_HAVE_OPENCV_NONFREE) && (VISP_HAVE_OPENCV_VERSION < 0x030000)
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  vpKeyPointSurf surf;
30  surf.buildReference(I);
31 
33  Idisp.resize(I.getHeight(), 2*I.getWidth());
34  Idisp.insert(I, vpImagePoint(0, 0));
35  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
36 
37  vpDisplayOpenCV d(Idisp, 0, 0, "Homography from matched Surf keypoints");
38  vpDisplay::display(Idisp);
39  vpDisplay::flush(Idisp);
40 
42  vpImagePoint corner_ref[4];
43  corner_ref[0].set_ij(115, 64);
44  corner_ref[1].set_ij( 83, 253);
45  corner_ref[2].set_ij(282, 307);
46  corner_ref[3].set_ij(330, 72);
49  for (unsigned int i=0; i<4; i++) {
50  vpDisplay::displayCross(Idisp, corner_ref[i], 12, vpColor::red);
51  }
52  vpDisplay::flush(Idisp);
54 
56  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
58 
59  vpHomography curHref;
60  while ( ! reader.end() )
61  {
62  reader.acquire(I);
63  Idisp.insert(I, vpImagePoint(0, I.getWidth()));
64  vpDisplay::display(Idisp);
66 
68  unsigned int nbMatch = surf.matchPoint(I);
70 
71  std::vector<vpImagePoint> iPref(nbMatch), iPcur(nbMatch); // Coordinates in pixels (for display only)
73  std::vector<double> mPref_x(nbMatch), mPref_y(nbMatch);
74  std::vector<double> mPcur_x(nbMatch), mPcur_y(nbMatch);
75  std::vector<bool> inliers(nbMatch);
77 
78  for (unsigned int i = 0; i < nbMatch; i++) {
79  vpImagePoint matched_ref, matched_cur;
80  surf.getMatchedPoints(i, matched_ref, matched_cur);
82  vpPixelMeterConversion::convertPoint(cam, matched_ref, mPref_x[i], mPref_y[i]);
83  vpPixelMeterConversion::convertPoint(cam, matched_cur, mPcur_x[i], mPcur_y[i]);
85 
86  // Store the image coordinates in pixel of the matched points
87  iPref[i] = matched_ref;
88  iPcur[i] = matched_cur;
89  }
90 
92  double residual;
93  if (method == 0)
94  vpHomography::ransac(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
95  (unsigned int)mPref_x.size()/2, 2.0/cam.get_px(), true);
96  else
97  vpHomography::robust(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
98  0.4, 4, true);
100 
102  vpImagePoint corner_cur[4];
103  for (int i=0; i< 4; i++) {
104  corner_cur[i] = vpHomography::project(cam, curHref, corner_ref[i]);
105  }
107 
109  vpImagePoint offset(0, I.getWidth());
110  for (int i=0; i< 4; i++) {
112  corner_cur[i] + offset,
113  corner_cur[(i+1)%4] + offset,
114  vpColor::blue, 3);
115  }
117 
119  for (unsigned int i = 0; i < nbMatch; i++) {
120  if(inliers[i] == true)
121  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::green);
122  else
123  vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::red);
124  }
126 
127  vpDisplay::flush(Idisp);
128 
129  if (vpDisplay::getClick(Idisp, false))
130  break;
131  }
132 
133  vpDisplay::getClick(Idisp);
134 #else
135  (void)argc; (void)argv;
136 #endif
137  return 0;
138 }
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...
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:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
Class that implements the SURF key points and technics thanks to 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 without initializing it.
Definition: vpImage.h:616
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:935
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:88
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
static const vpColor white
Definition: vpColor.h:158
void set_ij(const double ii, const double jj)
Definition: vpImagePoint.h:176
static const vpColor blue
Definition: vpColor.h:169