1 #include <iostream>
2
3 #include "opencv2/opencv_modules.hpp"
4
5 #ifdef HAVE_OPENCV_XFEATURES2D
6
7 #include <opencv2/features2d.hpp>
8 #include <opencv2/xfeatures2d.hpp>
9 #include <opencv2/imgcodecs.hpp>
10 #include <opencv2/opencv.hpp>
11 #include <vector>
12
13 // If you find this code useful, please add a reference to the following paper in your work:
14 // Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015
15
16 using namespace std;
17 using namespace cv;
18
19 const float inlier_threshold = 2.5f; // Distance threshold to identify inliers
20 const float nn_match_ratio = 0.8f; // Nearest neighbor matching ratio
21
main(void)22 int main(void)
23 {
24 Mat img1 = imread("../data/graf1.png", IMREAD_GRAYSCALE);
25 Mat img2 = imread("../data/graf3.png", IMREAD_GRAYSCALE);
26
27
28 Mat homography;
29 FileStorage fs("../data/H1to3p.xml", FileStorage::READ);
30
31 fs.getFirstTopLevelNode() >> homography;
32
33 vector<KeyPoint> kpts1, kpts2;
34 Mat desc1, desc2;
35
36 Ptr<cv::ORB> orb_detector = cv::ORB::create(10000);
37
38 Ptr<xfeatures2d::LATCH> latch = xfeatures2d::LATCH::create();
39
40
41 orb_detector->detect(img1, kpts1);
42 latch->compute(img1, kpts1, desc1);
43
44 orb_detector->detect(img2, kpts2);
45 latch->compute(img2, kpts2, desc2);
46
47 BFMatcher matcher(NORM_HAMMING);
48 vector< vector<DMatch> > nn_matches;
49 matcher.knnMatch(desc1, desc2, nn_matches, 2);
50
51 vector<KeyPoint> matched1, matched2, inliers1, inliers2;
52 vector<DMatch> good_matches;
53 for (size_t i = 0; i < nn_matches.size(); i++) {
54 DMatch first = nn_matches[i][0];
55 float dist1 = nn_matches[i][0].distance;
56 float dist2 = nn_matches[i][1].distance;
57
58 if (dist1 < nn_match_ratio * dist2) {
59 matched1.push_back(kpts1[first.queryIdx]);
60 matched2.push_back(kpts2[first.trainIdx]);
61 }
62 }
63
64 for (unsigned i = 0; i < matched1.size(); i++) {
65 Mat col = Mat::ones(3, 1, CV_64F);
66 col.at<double>(0) = matched1[i].pt.x;
67 col.at<double>(1) = matched1[i].pt.y;
68
69 col = homography * col;
70 col /= col.at<double>(2);
71 double dist = sqrt(pow(col.at<double>(0) - matched2[i].pt.x, 2) +
72 pow(col.at<double>(1) - matched2[i].pt.y, 2));
73
74 if (dist < inlier_threshold) {
75 int new_i = static_cast<int>(inliers1.size());
76 inliers1.push_back(matched1[i]);
77 inliers2.push_back(matched2[i]);
78 good_matches.push_back(DMatch(new_i, new_i, 0));
79 }
80 }
81
82 Mat res;
83 drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
84 imwrite("../../samples/data/latch_res.png", res);
85
86
87 double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
88 cout << "LATCH Matching Results" << endl;
89 cout << "*******************************" << endl;
90 cout << "# Keypoints 1: \t" << kpts1.size() << endl;
91 cout << "# Keypoints 2: \t" << kpts2.size() << endl;
92 cout << "# Matches: \t" << matched1.size() << endl;
93 cout << "# Inliers: \t" << inliers1.size() << endl;
94 cout << "# Inliers Ratio: \t" << inlier_ratio << endl;
95 cout << endl;
96 return 0;
97 }
98
99 #else
100
main()101 int main()
102 {
103 std::cerr << "OpenCV was built without xfeatures2d module" << std::endl;
104 return 0;
105 }
106
107 #endif
108