1 //! \example tutorial-matching-surf-homography-deprecated.cpp
2 #include <visp3/core/vpPixelMeterConversion.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/io/vpVideoReader.h>
5 #include <visp3/vision/vpHomography.h>
6 #include <visp3/vision/vpKeyPointSurf.h>
7 
main(int argc,const char ** argv)8 int main(int argc, const char **argv)
9 {
10 #if defined(VISP_HAVE_OPENCV_NONFREE) && (VISP_HAVE_OPENCV_VERSION < 0x030000)
11   //! [Select method]
12   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;
21   //! [Select method]
22 
23   vpImage<unsigned char> I;
24 
25   vpVideoReader reader;
26   reader.setFileName("video-postcard.mp4");
27   reader.acquire(I);
28 
29   vpKeyPointSurf surf;
30   surf.buildReference(I);
31 
32   vpImage<unsigned char> Idisp;
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 
41   //! [Set coordinates]
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);
47   //! [Set coordinates]
48   //! [Display]
49   for (unsigned int i = 0; i < 4; i++) {
50     vpDisplay::displayCross(Idisp, corner_ref[i], 12, vpColor::red);
51   }
52   vpDisplay::flush(Idisp);
53   //! [Display]
54 
55   //! [Camera]
56   vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
57   //! [Camera]
58 
59   vpHomography curHref;
60   while (!reader.end()) {
61     reader.acquire(I);
62     Idisp.insert(I, vpImagePoint(0, I.getWidth()));
63     vpDisplay::display(Idisp);
64     vpDisplay::displayLine(Idisp, vpImagePoint(0, I.getWidth()), vpImagePoint(I.getHeight(), I.getWidth()),
65                            vpColor::white, 2);
66 
67     //! [Matching]
68     unsigned int nbMatch = surf.matchPoint(I);
69     //! [Matching]
70 
71     std::vector<vpImagePoint> iPref(nbMatch),
72         iPcur(nbMatch); // Coordinates in pixels (for display only)
73     //! [Allocation]
74     std::vector<double> mPref_x(nbMatch), mPref_y(nbMatch);
75     std::vector<double> mPcur_x(nbMatch), mPcur_y(nbMatch);
76     std::vector<bool> inliers(nbMatch);
77     //! [Allocation]
78 
79     for (unsigned int i = 0; i < nbMatch; i++) {
80       vpImagePoint matched_ref, matched_cur;
81       surf.getMatchedPoints(i, matched_ref, matched_cur);
82       //! [Pixel conversion]
83       vpPixelMeterConversion::convertPoint(cam, matched_ref, mPref_x[i], mPref_y[i]);
84       vpPixelMeterConversion::convertPoint(cam, matched_cur, mPcur_x[i], mPcur_y[i]);
85       //! [Pixel conversion]
86 
87       // Store the image coordinates in pixel of the matched points
88       iPref[i] = matched_ref;
89       iPcur[i] = matched_cur;
90     }
91 
92     //! [Homography estimation]
93     double residual;
94     if (method == 0)
95       vpHomography::ransac(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual,
96                            (unsigned int)mPref_x.size() / 2, 2.0 / cam.get_px(), true);
97     else
98       vpHomography::robust(mPref_x, mPref_y, mPcur_x, mPcur_y, curHref, inliers, residual, 0.4, 4, true);
99     //! [Homography estimation]
100 
101     //! [Projection]
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     }
106     //! [Projection]
107 
108     //! [Display contour]
109     vpImagePoint offset(0, I.getWidth());
110     for (int i = 0; i < 4; i++) {
111       vpDisplay::displayLine(Idisp, corner_cur[i] + offset, corner_cur[(i + 1) % 4] + offset, vpColor::blue, 3);
112     }
113     //! [Display contour]
114 
115     //! [Display matches]
116     for (unsigned int i = 0; i < nbMatch; i++) {
117       if (inliers[i] == true)
118         vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::green);
119       else
120         vpDisplay::displayLine(Idisp, iPref[i], iPcur[i] + offset, vpColor::red);
121     }
122     //! [Display matches]
123 
124     vpDisplay::flush(Idisp);
125 
126     if (vpDisplay::getClick(Idisp, false))
127       break;
128   }
129 
130   vpDisplay::getClick(Idisp);
131 #else
132   (void)argc;
133   (void)argv;
134 #endif
135   return 0;
136 }
137