-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_matcher.cpp
116 lines (75 loc) · 2.4 KB
/
feature_matcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "feature_matcher.h"
fm::feature_matcher::feature_matcher()
{
}
fm::feature_matcher::~feature_matcher()
{
}
const bool fm::feature_matcher::ORB(const cv::Mat & in1, const cv::Mat & in2, cv::Mat * out1, fm::matcher matcher)
{
cv::Ptr<cv::ORB> detector = cv::ORB::create();
std::vector<cv::KeyPoint> key_points1, key_points2;
cv::Mat descriptors1, descriptors2;
detector->detectAndCompute(in1, cv::Mat(), key_points1, descriptors1);
detector->detectAndCompute(in2, cv::Mat(), key_points2, descriptors2);
switch (matcher) {
case fm::BF:
cv::BFMatcher matcher(cv::NORM_HAMMING);
std::vector<cv::DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
double min_dist = 100, max_dist = 0;
for (int i = 0; i < descriptors1.rows; i++) {
double dist = matches[i].distance;
if (dist > max_dist) {
max_dist = dist;
}
if (dist < min_dist) {
min_dist = dist;
}
}
std::vector<cv::DMatch> good_matches;
for (int i = 0; i < descriptors1.rows; i++) {
if (matches[i].distance <= 0.18* max_dist) {
good_matches.push_back(matches[i]);
}
}
cv::drawMatches(in1, key_points1, in2, key_points2, good_matches, *out1);
break;
}
return false;
}
const bool fm::feature_matcher::BRISK(const cv::Mat & in1, const cv::Mat & in2, cv::Mat * out1, fm::matcher matcher)
{
cv::Ptr<cv::BRISK> detector = cv::BRISK::create();
std::vector<cv::KeyPoint> key_points1, key_points2;
cv::Mat descriptors1, descriptors2;
detector->detectAndCompute(in1, cv::Mat(), key_points1, descriptors1);
detector->detectAndCompute(in2, cv::Mat(), key_points2, descriptors2);
switch (matcher) {
case fm::BF:
cv::BFMatcher matcher(cv::NORM_HAMMING);
std::vector<cv::DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
double min_dist = 100, max_dist = 0;
for (int i = 0; i < descriptors1.rows; i++) {
double dist = matches[i].distance;
if (dist > max_dist) {
max_dist = dist;
}
if (dist < min_dist) {
min_dist = dist;
}
}
std::vector<cv::DMatch> good_matches;
for (int i = 0; i < descriptors1.rows; i++) {
if (matches[i].distance <= 0.18* max_dist) {
good_matches.push_back(matches[i]);
}
}
cv::drawMatches(in1, key_points1, in2, key_points2,
good_matches, *out1, cv::Scalar(0,0,255), cv::Scalar(0,0,255),
std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
break;
}
return false;
}