-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathquery_loader.cpp
More file actions
292 lines (224 loc) · 6.61 KB
/
query_loader.cpp
File metadata and controls
292 lines (224 loc) · 6.61 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* query_loader.cpp
*
* Created on: Mar 21, 2015
* Author: Fabian Tschopp
*/
#include <iostream>
#include <fstream>
#include <string>
#include "parse_bundler.hpp"
#include "bundler_camera.hpp"
#include "query_loader.hpp"
#include "SIFT_keypoint.hpp"
#include "pose_utils.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
namespace pose_estimation {
QueryLoader::~QueryLoader() {
}
QueryLoader::QueryLoader(std::string database_path, std::string query_path) :
database_path_(database_path), query_path_(query_path) {
std::ifstream queryfile;
queryfile.open(database_path + "list.query.fused.txt");
std::string delimiter = " ";
std::string line;
while (getline(queryfile, line)) {
double focal_length = 0.0;
std::vector<std::string> splitline = SplitString(line, " ");
int width = std::stoi(splitline[1]);
int height = std::stoi(splitline[2]);
if (splitline.size() == 4) {
focal_length = std::stod(splitline[3]);
}
std::string name = CutString(splitline[0], "/", ".");
if (name.length() > 0) {
Query query(this, name, focal_length);
query.set_image_width(width);
query.set_image_height(height);
queries_.push_back(query);
}
}
queryfile.close();
}
std::string QueryLoader::database_path() {
return database_path_;
}
std::string QueryLoader::query_path() {
return query_path_;
}
int QueryLoader::GetQueryCount() {
return queries_.size();
}
Query& QueryLoader::GetQuery(int index, bool load_image) {
// Lazy load query image and SIFT when needed
if(load_image) {
queries_[index].LoadImage();
}
queries_[index].LoadSIFT();
return queries_[index];
}
Query::~Query() {
}
Query::Query() {
}
Query::Query(QueryLoader *query_loader, std::string query_image_name,
double focal_length) :
query_loader_(query_loader), query_image_name_(query_image_name), focal_length_(
focal_length), image_width_(0), image_height_(0) {
}
bool Query::LoadImage() {
std::string queryimagepath = query_loader_->query_path() + query_image_name_
+ ".jpg";
std::ifstream queryimage(queryimagepath);
if (queryimage.fail()) {
std::string img_id = query_image_name_;
unsigned int img_id_length = img_id.size() + 1;
while (img_id_length > img_id.size()) {
img_id_length = img_id.size();
img_id = CutStringAfter(img_id, "_");
}
// Get the webpage containing the flickr image
std::string wgetcommand =
"wget -O tmp/tmp.tmp \"http://www.flickr.com/photo_zoom.gne?id="
+ img_id + "&size=o\"";
const char* wgetsystemc = wgetcommand.c_str();
system(wgetsystemc);
std::ifstream tmpfile;
tmpfile.open("tmp/tmp.tmp");
std::string line;
bool found_image = false;
while (getline(tmpfile, line)) {
if (line.find("_o.jpg") != std::string::npos) {
found_image = true;
break;
}
}
if (found_image) {
// Extract the static image path from flickr webpage
std::string static_image_url = CutString(line, "src=\"", "\">");
// Get the image itself
wgetcommand = "wget -O " + queryimagepath + " " + static_image_url;
wgetsystemc = wgetcommand.c_str();
system(wgetsystemc);
} else {
return false;
}
}
// Actually load the image
image_ = cv::imread(queryimagepath, CV_LOAD_IMAGE_COLOR);
//image_width_ = image_.cols;
//image_height_ = image_.rows;
return true;
}
bool Query::LoadSIFT() {
SIFT_loader siftl;
std::string querypath = query_loader_->query_path() + query_image_name_
+ ".key";
std::ifstream querykey(querypath);
if (querykey.fail()) {
// Unzip the descriptor if not existing already
std::string gzipcommand = "gzip -k -d -c "
+ query_loader_->database_path() + "query/" + query_image_name_
+ ".key.gz > " + query_loader_->query_path() + query_image_name_
+ ".key";
const char* gzipsystemc = gzipcommand.c_str();
system(gzipsystemc);
}
// Load the query descriptors
const char* querycharpath = querypath.c_str();
siftl.load_features(querycharpath, LOWE);
sift_descriptors_ = siftl.get_descriptors();
sift_keypoints_ = siftl.get_keypoints();
return true;
}
std::vector<SIFT_keypoint>& Query::sift_keypoints() {
return sift_keypoints_;
}
std::vector<unsigned char*>& Query::sift_descriptors() {
return sift_descriptors_;
}
double Query::focal_length() {
return focal_length_;
}
Eigen::Matrix<double, 3, 1> Query::camera_position() {
return camera_position_;
}
Eigen::Matrix<double, 3, 3> Query::camera_rotation() {
return camera_rotation_;
}
void Query::set_fitpoints_2d(
std::vector<Eigen::Matrix<double, 2, 1>> fitpoints_2d) {
fitpoints_2d_ = fitpoints_2d;
}
void Query::set_fitpoints_3d(
std::vector<Eigen::Matrix<double, 3, 1>> fitpoints_3d) {
fitpoints_3d_ = fitpoints_3d;
}
std::vector<Eigen::Matrix<double, 2, 1>>& Query::fitpoints_2d() {
return fitpoints_2d_;
}
std::vector<Eigen::Matrix<double, 3, 1>>& Query::fitpoints_3d() {
return fitpoints_3d_;
}
void Query::set_image_width(int width) {
image_width_ = width;
}
void Query::set_image_height(int height) {
image_height_ = height;
}
bool Query::ComputeMatrices() {
// Camera intrinsic matrix
camera_matrix_ = Eigen::Matrix<double, 3, 3>::Zero(3, 3);
camera_matrix_(0, 0) = focal_length_;
camera_matrix_(1, 1) = focal_length_;
camera_matrix_(2, 2) = 1.0;
camera_matrix_(0, 2) = 0.0;//image_width_ / 2;
camera_matrix_(1, 2) = 0.0;//image_height_ / 2;
// Inverse matrix for feature preparation
camera_matrix_inv_ = camera_matrix_.inverse();
// 4 to 3 conversion identity matrix
Eigen::Matrix<double, 3, 4> id_mat = Eigen::Matrix<double, 3, 4>::Zero(3,
4);
id_mat(0, 0) = 1.0;
id_mat(1, 1) = 1.0;
id_mat(2, 2) = 1.0;
// Camera extrinsic
Eigen::Matrix<double, 4, 4> rt_mat = Eigen::Matrix<double, 4, 4>::Zero(4,
4);
rt_mat.block(0, 0, 3, 3) = camera_rotation_;
rt_mat.block(0, 3, 3, 1) = camera_position_;
rt_mat(3, 3) = 1;
// We have to take the inverse of the RT-matrix because rotation and translation are world->camera.
proj_matrix_ = camera_matrix_ * id_mat * rt_mat.inverse();
return true;
}
void Query::set_camera_position(Eigen::Matrix<double, 3, 1> position) {
camera_position_ = position;
}
void Query::set_camera_rotation(Eigen::Matrix<double, 3, 3> rotation) {
camera_rotation_ = rotation;
}
void Query::set_focal_length(double focal_length) {
focal_length_ = focal_length;
}
Eigen::Matrix<double, 3, 3> Query::camera_matrix() {
return camera_matrix_;
}
Eigen::Matrix<double, 3, 3> Query::camera_matrix_inv() {
return camera_matrix_inv_;
}
Eigen::Matrix<double, 3, 4> Query::proj_matrix() {
return proj_matrix_;
}
cv::Mat& Query::image() {
return image_;
}
int Query::image_width() {
return image_width_;
}
int Query::image_height() {
return image_height_;
}
}