-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage_pairs.cc
216 lines (159 loc) · 5.73 KB
/
image_pairs.cc
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "image_pairs.h"
#include "image.h"
#include <opencv2/calib3d/calib3d.hpp>
// inspired from persitence.cpp:write( .., vector<Keypoint>& keypoints)
static void write_matches(FileStorage& fs, const std::string& name, const Matches& matches) {
WriteStructContext ws(fs, name, CV_NODE_SEQ + CV_NODE_FLOW);
for (DMatch match : matches) {
write(fs, match.queryIdx);
write(fs, match.trainIdx);
write(fs, match.imgIdx);
write(fs, match.distance);
}
}
// inspired from persitence.cpp:read( .., vector<Keypoint>& keypoints)
static void read_matches(const FileNode& node, Matches& matches) {
matches.resize(0);
FileNodeIterator it = node.begin(), it_end = node.end();
for( ; it != it_end; ) {
DMatch match;
it >> match.queryIdx >> match.trainIdx >> match.imgIdx >> match.distance;
matches.push_back(match);
}
}
void ImagePair::write(FileStorage& fs, bool save_image) const {
LOG(DEBUG) << "Serializing Image Pair";
fs << "{"
<< "F" << F;
write_matches(fs, "matches", matches);
fs << "inliers_count" << (int) inliers_count
<< "inliers" << keypointsInliers;
if (save_image) {
fs << "image1" << *image1
<< "image2" << *image2;
}
fs << "error" << error
<< "}";
}
void ImagePair::read(const FileNode& node, bool load_image) {
int count;
LOG(DEBUG) << "De-serializing Image Pair";
node["F"] >> F;
read_matches(node["matches"], matches);
node["inliers_count"] >> count;
inliers_count = count;
node["inliers"] >> keypointsInliers;
if (load_image) {
image1.reset(new Image());
image2.reset(new Image());
node["image1"] >> *image1;
node["image2"] >> *image2;
}
node["error"] >> error;
}
bool ImagePair::compute_matches() {
ImageFeaturesPtr features1 = image1->get_image_features();
ImageFeaturesPtr features2 = image2->get_image_features();
if (!features1 || !features2) {
LOG(ERROR) << "Unable to load features from images";
return false;
}
match_features(*features1, *features2, matches);
// XX (mtourne): probably dependent on algo
if (matches.size() < MIN_FEATURE_MATCHES) {
LOG(DEBUG) << "Not enough matches: " << matches.size()
<< ", at least " << MIN_FEATURE_MATCHES << "needed.";
return false;
}
return true;
}
bool ImagePair::compute_F_mat() {
// use RANSAC to get F matrix
// return better_matches for all the matches that make sense
ImageFeaturesPtr features1 = image1->get_image_features();
ImageFeaturesPtr features2 = image2->get_image_features();
vector<Point2f> pts1, pts2;
vector<unsigned char> status;
if (!features1 || !features2) {
LOG(ERROR) << "Unable to load features from images";
return false;
}
matches2points(matches, *features1, *features2, pts1, pts2);
F = findFundamentalMat(pts1, pts2, FM_RANSAC, 1, 0.99, status);
// XX (mtourne): stupid vector<unsigned char> to vector<char> conversion
keypointsInliers = vector<char> (status.begin(), status.end());
inliers_count = countNonZero(keypointsInliers);
LOG(DEBUG) << "F matrix: " << endl << F;
LOG(DEBUG) << "Fundamental mat is keeping " << inliers_count << " / "
<< keypointsInliers.size();
if (inliers_count < MIN_INLIERS) {
LOG(DEBUG) << "Not enough inliers: " << inliers_count
<< ", at least " << MIN_INLIERS << "needed.";
return false;
}
return true;
}
bool ImagePair::filterPutativeMatches() {
if (keypointsInliers.size() <= 0) {
LOG(ERROR) << "No keypoint inliers defined";
return false;
}
Matches new_matches;
if (!get_putative_matches(matches, keypointsInliers, new_matches)) {
return false;
}
matches = new_matches;
// remove former keypoints inliers, this is a one time operation
keypointsInliers.clear();
return true;
}
// decompose E into Rotation and Translation
// two methods of choice here : Horn90 and Hartley & Zisserman
// implement HZ using SVD decomposition
static int getRT(Mat& E, vector<Mat> &R, vector<Mat> &T) {
SVD svd(E,SVD::MODIFY_A);
LOG(DEBUG) << "U: " << svd.u << endl
<< "Vt:" << svd.vt << endl
<< "W:" << svd.w << endl;
return 0;
}
int ImagePair::compute_camera_mat() {
Mat K1 = image1->get_camera_matrix();
Mat K2 = image2->get_camera_matrix();
// essential matrix
Mat E = K1.t() * F * K2;
//according to http://en.wikipedia.org/wiki/Essential_matrix#Properties_of_the_essential_matrix
// det(E) == 0
if(fabsf(determinant(E)) > 1e-07) {
LOG(DEBUG) << "det(E) != 0: " << determinant(E);
return 1;
}
// For a given essential matrix E = U diag(1, 1, 0)V'
// and first camera matrix P = [I | 0], there are four
// possible choices for the second camera matrix P
vector<Mat> R(4);
vector<Mat> T(4);
getRT(E, R, T);
return 0;
}
void ImagePair::print_matches() const {
Mat img_gray1 = image1->get_image_gray();
Mat img_gray2 = image2->get_image_gray();
ImageFeaturesPtr features1 = image1->get_image_features();
ImageFeaturesPtr features2 = image2->get_image_features();
if (!features1 || !features2) {
LOG(ERROR) << "Unable to load features from images";
return;
}
ostringstream ss;
ss << "matches_" << image1->get_name() << "_" << image2->get_name();
if (keypointsInliers.size() > 0) {
ss << "_RANSAC";
} else {
ss << "_ALL_MATCHES";
}
ss << ".jpg";
write_matches_image(img_gray1, *features1,
img_gray2, *features2,
matches, keypointsInliers, ss.str());
}