-
Notifications
You must be signed in to change notification settings - Fork 17
/
query_processor.cpp
179 lines (134 loc) · 4.92 KB
/
query_processor.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
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
/*
* query_processor.cpp
*
* Created on: Mar 23, 2015
* Author: Fabian Tschopp, Marco Zorzi
*/
#include "query_processor.hpp"
#include "query_loader.hpp"
#include "P3p.hpp"
#include "P4pf.hpp"
#include <flann/flann.h>
#include <omp.h>
#include <vector>
#include "pose_utils.hpp"
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Eigen>
namespace pose_estimation {
SiftMatch::SiftMatch(unsigned int lindex_p, unsigned int mindex_p,
float distance_p) :
lindex(lindex_p), mindex(mindex_p), distance(distance_p) {
}
SiftMatch::SiftMatch() :
lindex(0), mindex(0), distance(0.0) {
}
QueryProcessor::QueryProcessor(parse_bundler &parsebundler) :
parsebundler_(parsebundler) {
}
bool QueryProcessor::BundleAdjust(Query &query,
std::vector<SiftMatch> &fitted_matches,
std::vector<SIFT_keypoint> &sift_keypoints,
std::vector<feature_3D_info> &sifts) {
// TODO Levenberg Marquardt for 1 camera pose
// TODO Write-back directly into query
// TODO This is optional, if there is enough time to do it.
return true;
}
bool QueryProcessor::Project(Query &query,
std::vector<Eigen::Matrix<double, 2, 1>> &fitpoints_2d,
std::vector<Eigen::Matrix<double, 3, 1>> &fitpoints_3d,
std::vector<SiftMatch> &matches,
std::vector<SIFT_keypoint> &sift_keypoints,
std::vector<feature_3D_info> &sifts) {
fitpoints_2d.resize(matches.size());
fitpoints_3d.resize(matches.size());
Eigen::Matrix<double, 3, 4> proj_matrix = query.proj_matrix();
// Reproject in parallel
#pragma omp parallel for
for (unsigned int i = 0; i < matches.size(); ++i) {
SiftMatch match = matches[i];
SIFT_keypoint keypoint = sift_keypoints[match.lindex];
point3D point = sifts[match.mindex].point;
// Load 3D point
Eigen::Matrix<double, 4, 1> point3d;
point3d << point.x, point.y, point.z, 1.0;
// Load 2D point
Eigen::Matrix<double, 3, 1> point2d;
point2d << -(keypoint.x - (double) (query.image_width()) / 2.0), -(keypoint.y
- (double) (query.image_height()) / 2.0), 1.0;
// Projection
Eigen::Matrix<double, 3, 1> point2d_proj = proj_matrix * point3d;
fitpoints_3d[i] = (point3d.block(0, 0, 3, 1));
fitpoints_2d[i] = ((point2d_proj / point2d_proj(2)).block(0, 0, 2, 1));
}
return true;
}
std::vector<SiftMatch> QueryProcessor::TestHypothesis(Query &query,
std::vector<SiftMatch> &matches,
std::vector<SIFT_keypoint> &sift_keypoints,
std::vector<feature_3D_info> &sifts, double match_eps,
double &fit_quality) {
fit_quality = 0;
std::vector<SiftMatch> fits;
std::vector<Eigen::Matrix<double, 2, 1>> fitpoints_2d;
std::vector<Eigen::Matrix<double, 3, 1>> fitpoints_3d;
Project(query, fitpoints_2d, fitpoints_3d, matches, query.sift_keypoints(),
(*sifts_));
for (unsigned int i = 0; i < matches.size(); ++i) {
SiftMatch match = matches[i];
SIFT_keypoint keypoint = sift_keypoints[match.lindex];
// Load 2D point
Eigen::Matrix<double, 2, 1> point2d;
point2d << -(keypoint.x - (double) (query.image_width()) / 2.0), -(keypoint.y
- (double) (query.image_height()) / 2.0);
// Load 2d reproject point
Eigen::Matrix<double, 2, 1> point2d_proj;
point2d_proj = fitpoints_2d[i];
// Reprojection 2 norm error
double match_dist = (point2d - point2d_proj).norm();
if (match_dist < match_eps) {
fits.push_back(match);
}
}
int width = query.image_width();
int height = query.image_height();
Eigen::MatrixXf fit_area(height, width);
Eigen::MatrixXf match_area(height, width);
fit_area.setZero();
match_area.setZero();
int cover_area = std::ceil((float) (query.image_width()) / 20.0);
#pragma omp parallel for
for (unsigned int i = 0; i < fits.size(); ++i) {
SiftMatch &match = fits[i];
SIFT_keypoint &keypoint = sift_keypoints[match.lindex];
int x = rint(keypoint.x);
int y = rint(keypoint.y);
int start_x = std::max(0, std::min(x - cover_area / 2, width - 1));
int start_y = std::max(0, std::min(y - cover_area / 2, height - 1));
int stop_x = std::max(0, std::min(x + cover_area / 2, width - 1));
int stop_y = std::max(0, std::min(y + cover_area / 2, height - 1));
if (start_y < stop_y && start_x < stop_x) {
fit_area.block(start_y, start_x, stop_y - start_y, stop_x - start_x).setOnes();
}
}
#pragma omp parallel for
for (unsigned int i = 0; i < matches.size(); ++i) {
SiftMatch &match = matches[i];
SIFT_keypoint &keypoint = sift_keypoints[match.lindex];
int x = rint(keypoint.x);
int y = rint(keypoint.y);
int start_x = std::max(0, std::min(x - cover_area / 2, width - 1));
int start_y = std::max(0, std::min(y - cover_area / 2, height - 1));
int stop_x = std::max(0, std::min(x + cover_area / 2, width - 1));
int stop_y = std::max(0, std::min(y + cover_area / 2, height - 1));
if (start_y < stop_y && start_x < stop_x) {
match_area.block(start_y, start_x, stop_y - start_y, stop_x - start_x).setOnes();
}
}
float fit_area_val = fit_area.sum();
float match_area_val = match_area.sum();
fit_quality = fit_area_val / match_area_val;
return fits;
}
}