1
+ #include < iostream>
2
+ #include < opencv2\opencv.hpp>
3
+ #include < opencv2\features2d.hpp>
4
+ #include < opencv2\imgproc.hpp>
5
+
6
+ using namespace cv ;
7
+ using namespace std ;
8
+
9
+ int main (int argc, char ** argv )
10
+ {
11
+ Mat imageA = imread (" data/images/puente-americas-1.jpg" /* , IMREAD_GRAYSCALE*/ );
12
+ Mat imageB = imread (" data/images/puente-americas-2.jpg" /* , IMREAD_GRAYSCALE*/ );
13
+
14
+ if (imageA.empty () || imageB.empty ())
15
+ {
16
+ printf (" No image data." );
17
+ getchar ();
18
+
19
+ return -1 ;
20
+ }
21
+
22
+ Ptr <Feature2D> detect = BRISK::create ();
23
+
24
+ vector<KeyPoint> kpA, kpB;
25
+ Mat descA, descB;
26
+
27
+ detect->detectAndCompute (imageA, noArray (), kpA, descA);
28
+ detect->detectAndCompute (imageB, noArray (), kpB, descB);
29
+
30
+ vector<DMatch> matches;
31
+
32
+ Ptr <DescriptorMatcher> matcher = BFMatcher::create (NORM_HAMMING, true );
33
+ matcher->match (descA, descB, matches);
34
+
35
+ sort (matches.begin (), matches.end ());
36
+ matches.erase (matches.begin () + 30 , matches.end ());
37
+
38
+
39
+ // Visualizar pareo de puntos
40
+ Scalar color = Scalar::all (-1 );
41
+ int flags = DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS;
42
+
43
+ Mat result;
44
+ drawMatches (imageA, kpA, imageB, kpB, matches, result, color, color, vector<char >(), flags);
45
+ // Fin de visualizacion
46
+
47
+ vector<Point2f> pts1, pts2;
48
+ for (auto & m : matches) {
49
+ pts1.push_back (kpA[m.queryIdx ].pt );
50
+ pts2.push_back (kpB[m.trainIdx ].pt );
51
+ }
52
+
53
+ Mat h = findHomography (pts2, pts1, noArray (), CV_RANSAC);
54
+
55
+ cout << " Homography Mat: " << endl << h << endl;
56
+
57
+ vector<Point2f> box;
58
+ box.push_back (Point2f (0 , 0 ));
59
+ box.push_back (Point2f (imageB.cols , 0 ));
60
+ box.push_back (Point2f (imageB.cols , imageB.rows ));
61
+ box.push_back (Point2f (0 , imageB.rows ));
62
+
63
+ vector<Point2f> box_dst;
64
+ perspectiveTransform (box, box_dst, h);
65
+
66
+ Rect rc = boundingRect (box_dst);
67
+
68
+ Mat dst;
69
+
70
+ warpPerspective (imageB, dst, h, Size (rc.width + rc.x , rc.height + rc.y ));
71
+ imageA.copyTo (dst (Rect (Point (0 , 0 ), imageA.size ())));
72
+
73
+ // rectangle(dst, rc, Scalar(0, 255, 0));
74
+ // rectangle(dst, Rect(0, 0, imageA.cols, imageA.rows), Scalar(0, 255, 0));
75
+
76
+ // for (auto& dm : matches)
77
+ // circle(dst, kpA[dm.queryIdx].pt, 5, Scalar(0, 0, 255), 1, LINE_AA);
78
+
79
+ imshow (" Puenta de las Americas" , dst);
80
+
81
+ imshow (" OpenCV :: Match Keypoints" , result);
82
+ waitKey (0 );
83
+
84
+ return 0 ;
85
+ }
0 commit comments