Repository navigation
Expand file tree
/
Copy pathUAVBundle.cpp
More file actions
431 lines (364 loc) · 16.3 KB
/
Copy pathUAVBundle.cpp
File metadata and controls
431 lines (364 loc) · 16.3 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//
// Created by wuwei on 17-7-31.
//
#include "UAVBundle.h"
using namespace openMVG;
using namespace openMVG::sfm;
using namespace openMVG::cameras;
/***
* 为了保持风格的统一,需要集成至Features_Provider能够方便进行处理和解算,
* 在密集匹配的过程中欧给你应该也需要进行处理才对
*/
struct Features_Provider_Gpu:public Features_Provider
{
/***
* 导入GPU解算得到的特征点
* @param sfm_data
* @param feat_directory
* @return
*/
virtual bool load(
const SfM_Data & sfm_data,
const std::string & feat_directory)
{
C_Progress_display my_progress_bar( sfm_data.GetViews().size(),
std::cout, "\n- Features Loading -\n" );
// Read for each view the corresponding features and store them as PointFeatures
bool bContinue = true;
//导入特征点的过程应该比较快,不需要通过多线程操作了
for (Views::const_iterator iter = sfm_data.GetViews().begin();
iter != sfm_data.GetViews().end() && bContinue; ++iter) {
const std::string sImageName = stlplus::create_filespec(sfm_data.s_root_path, iter->second.get()->s_Img_path);
const std::string basename = stlplus::basename_part(sImageName);
const std::string featFile = stlplus::create_filespec(feat_directory, basename, ".gpufeat");
//导入特征点
//通过读取二进制文件的方式导入
if (!stlplus::file_exists(featFile))
{
bContinue= false;
return bContinue;
}
ifstream ifs(featFile,ios_base::in|ios_base::binary);
int num_feats;
ifs.read((char*)&num_feats,sizeof(int));
float* points = new float[2*num_feats];
ifs.read((char*)points,sizeof(float)*2*num_feats);
features::PointFeatures vec_points;
for (int i = 0; i < num_feats; ++i) {
vec_points.push_back( features::PointFeature(points[2*i+0],points[2*i+1]));
}
feats_per_view[iter->second.get()->id_view] = vec_points;
delete[]points;points=NULL;
++my_progress_bar;
}
return bContinue;
}
};
void UAVBundle::UAVBundleTwoViewExtract(string pathRotMat) {
SfM_Data sfm_data;
if (!Load(sfm_data, _info_._g_SFM_data, ESfM_Data(VIEWS|INTRINSICS))) {
std::cerr << std::endl
<< "The input SfM_Data file \""<< _info_._g_SFM_data << "\" cannot be read." << std::endl;
return ;
}
using namespace openMVG::features;
const std::string sImage_describer = stlplus::create_filespec(_info_._g_match_dir_, "image_describer", "json");
std::unique_ptr<Regions> regions_type = Init_region_type_from_file(sImage_describer);
if (!regions_type)
{
std::cerr << "Invalid: "
<< sImage_describer << " regions type file." << std::endl;
return ;
}
// Features reading
std::shared_ptr<Regions_Provider> feats_provider = std::make_shared<Regions_Provider>();
if (!feats_provider->load(sfm_data, _info_._g_feature_dir_, regions_type)) {
std::cerr << std::endl
<< "Invalid features." << std::endl;
return ;
}
// Matches reading
PairWiseMatches map_PutativesMatches;
if // Try to read the two matches file formats
(
!(!Load(map_PutativesMatches, stlplus::create_filespec(_info_._g_match_dir_, "matches.e.txt")) ||
!Load(map_PutativesMatches,stlplus::create_filespec(_info_._g_match_dir_, "matches.e.bin")))
)
{
std::cerr << std::endl
<< "Invalid matches file." << std::endl;
return ;
}
//K
Mat3 K;
K(0,0)=_info_._g_focal_x;K(0,1)=0;K(0,2)=_info_._g_ppx;
K(1,0)=0;K(1,1)=_info_._g_focal_y;K(1,2)=_info_._g_ppx;
K(2,0)=0;K(2,1)=0;K(2,2)=1;
FILE* file=fopen(pathRotMat.c_str(),"wb+");
//get matches
Pair_Set pairs = getPairs(map_PutativesMatches);
int totalPairs = 0;
fwrite(&totalPairs,sizeof(int),1,file);
for (Pair_Set::iterator iter = pairs.begin();iter!=pairs.end();++iter) {
sfm::RelativePose_Info relativePose_info;
IndexT idx1 = iter->first;
IndexT idx2 = iter->second;
IndMatches matches = map_PutativesMatches.find(std::make_pair(idx1,idx2))->second;
const std::vector<PointFeature>
featsL = feats_provider->get(idx1)->GetRegionsPositions(),
featsR = feats_provider->get(idx2)->GetRegionsPositions();
const View * vl = sfm_data.views.at(idx1).get();
const View * vr = sfm_data.views.at(idx2).get();
std::pair<size_t, size_t> size_imaL(vl->ui_width, vl->ui_height);
std::pair<size_t, size_t> size_imaR(vr->ui_width, vr->ui_height);
int sizepnts1 = featsL.size();
int sizepnts2 = featsR.size();
int matchNum = matches.size();
Mat xL(2, matchNum);
Mat xR(2, matchNum);
for (size_t k = 0; k < matchNum; ++k) {
const PointFeature & imaL = featsL[matches.at(k).i_];
const PointFeature & imaR = featsR[matches.at(k).j_];
xL.col(k) = imaL.coords().cast<double>();
xR.col(k) = imaR.coords().cast<double>();
}
//相对定向,得到R和T相对定向元素
if (!sfm::robustRelativePose(K, K, xL, xR, relativePose_info, size_imaL, size_imaR, 256))
{
std::cerr << " /!\\ Robust relative pose estimation failure."
<< std::endl;
return ;
}
std::cout << "\nFound an Essential matrix:\n"
<< "\tprecision: " << relativePose_info.found_residual_precision << " pixels\n"
<< "\t#inliers: " << relativePose_info.vec_inliers.size() << "\n"
<< "\t#matches: " << matchNum
<< std::endl;
//DLT计算三维点的坐标
Pinhole_Intrinsic intrinsic0(vl->ui_width, vl->ui_height, K(0, 0), K(0, 2), K(1, 2));
Pinhole_Intrinsic intrinsic1(vr->ui_width, vr->ui_height, K(0, 0), K(0, 2), K(1, 2));
const Pose3 pose0 = Pose3(Mat3::Identity(), Vec3::Zero());
const Pose3 pose1 = relativePose_info.relativePose;
// Init structure by inlier triangulation
const Mat34 P1 = intrinsic0.get_projective_equivalent(pose0);
const Mat34 P2 = intrinsic1.get_projective_equivalent(pose1);
fwrite(&P1,sizeof(double),12,file);
fwrite(&P2,sizeof(double),12,file);
std::vector<Vec3> vec_3DPoints;
for (size_t i = 0; i < relativePose_info.vec_inliers.size(); ++i) {
const PointFeature & LL = featsL[matches[relativePose_info.vec_inliers[i]].i_];
const PointFeature & RR = featsR[matches[relativePose_info.vec_inliers[i]].j_];
// Point triangulation
Vec3 X;
TriangulateDLT(P1, LL.coords().cast<double>(), P2, RR.coords().cast<double>(), &X);
// Reject point that is behind the camera
if (pose0.depth(X) < 0 && pose1.depth(X) < 0)
continue;
vec_3DPoints.emplace_back(X); //DLT 3D points
}
int size = vec_3DPoints.size();
fwrite(&size,sizeof(unsigned long),1,file);
for(size_t i = 0; i < vec_3DPoints.size(); ++i)
{
fwrite(&vec_3DPoints[i],sizeof(Vec3),1,file);
}
totalPairs++;
}
fseek(file,0,SEEK_SET);
fwrite(&totalPairs,sizeof(int),1,file);
fclose(file);
}
void UAVBundle::UAVBundleGlobal() {
//method
int iRotationAveragingMethod = int (ROTATION_AVERAGING_L2);
int iTranslationAveragingMethod = int (TRANSLATION_AVERAGING_SOFTL1);
//判断方法是否正确
if (iRotationAveragingMethod < ROTATION_AVERAGING_L1 ||
iRotationAveragingMethod > ROTATION_AVERAGING_L2 ) {
std::cerr << "\n Rotation averaging method is invalid" << std::endl;
return;
}
//cameras::Intrinsic_Parameter_Type;
//调整所有的参数
const cameras::Intrinsic_Parameter_Type intrinsic_refinement_options = Intrinsic_Parameter_Type::ADJUST_ALL;
if (iTranslationAveragingMethod < TRANSLATION_AVERAGING_L1 ||
iTranslationAveragingMethod > TRANSLATION_AVERAGING_SOFTL1 ) {
std::cerr << "\n Translation averaging method is invalid" << std::endl;
return;
}
SfM_Data sfm_data;
if (!Load(sfm_data,_info_._g_SFM_data, ESfM_Data(VIEWS|INTRINSICS))) {
std::cerr << std::endl
<< "The input SfM_Data file \""<< _info_._g_SFM_data << "\" cannot be read." << std::endl;
return ;
}
//features and matches
using namespace openMVG::features;
const std::string sImage_describer = stlplus::create_filespec(_info_._g_match_dir_, "image_describer", "json");
std::unique_ptr<Regions> regions_type = Init_region_type_from_file(sImage_describer);
if (!regions_type)
{
std::cerr << "Invalid: "
<< sImage_describer << " regions type file." << std::endl;
return ;
}
// Features reading
std::shared_ptr<Features_Provider> feats_provider = std::make_shared<Features_Provider>();
if (!feats_provider->load(sfm_data, _info_._g_feature_dir_, regions_type)) {
std::cerr << std::endl
<< "Invalid features." << std::endl;
return ;
}
// Matches reading
std::shared_ptr<Matches_Provider> matches_provider = std::make_shared<Matches_Provider>();
if // Try to read the two matches file formats
(
!(matches_provider->load(sfm_data, stlplus::create_filespec(_info_._g_match_dir_, "matches.e.txt")) ||
matches_provider->load(sfm_data, stlplus::create_filespec(_info_._g_match_dir_, "matches.e.bin")))
)
{
std::cerr << std::endl
<< "Invalid matches file." << std::endl;
return ;
}
if (_info_._g_point_cloud_dir.empty()) {
std::cerr << "\nIt is an invalid output directory" << std::endl;
return ;
}
if (!stlplus::folder_exists(_info_._g_point_cloud_dir))
{
if (!stlplus::folder_create(_info_._g_point_cloud_dir))
{
std::cerr << "\nCannot create the output directory" << std::endl;
}
}
//---------------------------------------
// Global SfM reconstruction process
//---------------------------------------
openMVG::system::Timer timer;
GlobalSfMReconstructionEngine_RelativeMotions sfmEngine(
sfm_data,
_info_._g_point_cloud_dir,
stlplus::create_filespec(_info_._g_point_cloud_dir, "Reconstruction_Report.html"));
// Configure the features_provider & the matches_provider
sfmEngine.SetFeaturesProvider(feats_provider.get());
sfmEngine.SetMatchesProvider(matches_provider.get());
// Configure reconstruction parameters
sfmEngine.Set_Intrinsics_Refinement_Type(intrinsic_refinement_options);
bool b_use_motion_priors = _info_._g_Has_Pos;
sfmEngine.Set_Use_Motion_Prior(b_use_motion_priors);
// Configure motion averaging method
sfmEngine.SetRotationAveragingMethod(
ERotationAveragingMethod(iRotationAveragingMethod));
sfmEngine.SetTranslationAveragingMethod(
ETranslationAveragingMethod(iTranslationAveragingMethod));
if (sfmEngine.Process())
{
std::cout << std::endl << " Total Ac-Global-Sfm took (s): " << timer.elapsed() << std::endl;
std::cout << "...Generating SfM_Report.html" << std::endl;
Generate_SfM_Report(sfmEngine.Get_SfM_Data(),
stlplus::create_filespec(_info_._g_point_cloud_dir, "SfMReconstruction_Report.html"));
//-- Export to disk computed scene (data & visualizable results)
std::cout << "...Export SfM_Data to disk." << std::endl;
Save(sfmEngine.Get_SfM_Data(),
stlplus::create_filespec(_info_._g_point_cloud_dir, "sfm_data", ".bin"),
ESfM_Data(ALL));
Save(sfmEngine.Get_SfM_Data(),
stlplus::create_filespec(_info_._g_point_cloud_dir, "cloud_and_poses", ".ply"),
ESfM_Data(ALL));
return ;
}
return ;
}
void UAVBundle::UAVBundleGlobalGpu() {
//method
int iRotationAveragingMethod = int (ROTATION_AVERAGING_L2);
int iTranslationAveragingMethod = int (TRANSLATION_AVERAGING_SOFTL1);
//判断方法是否正确
if (iRotationAveragingMethod < ROTATION_AVERAGING_L1 ||
iRotationAveragingMethod > ROTATION_AVERAGING_L2 ) {
std::cerr << "\n Rotation averaging method is invalid" << std::endl;
return;
}
//cameras::Intrinsic_Parameter_Type;
//调整所有的参数
const cameras::Intrinsic_Parameter_Type intrinsic_refinement_options = Intrinsic_Parameter_Type::ADJUST_ALL;
if (iTranslationAveragingMethod < TRANSLATION_AVERAGING_L1 ||
iTranslationAveragingMethod > TRANSLATION_AVERAGING_SOFTL1 ) {
std::cerr << "\n Translation averaging method is invalid" << std::endl;
return;
}
SfM_Data sfm_data;
if (!Load(sfm_data,_info_._g_SFM_data, ESfM_Data(VIEWS|INTRINSICS))) {
std::cerr << std::endl
<< "The input SfM_Data file \""<< _info_._g_SFM_data << "\" cannot be read." << std::endl;
return ;
}
// Features reading
std::shared_ptr<Features_Provider_Gpu> feats_provider = std::make_shared<Features_Provider_Gpu>();
if (!feats_provider->load(sfm_data, _info_._g_feature_dir_)) {
std::cerr << std::endl
<< "Invalid features." << std::endl;
return ;
}
// Matches reading
std::shared_ptr<Matches_Provider> matches_provider = std::make_shared<Matches_Provider>();
if // Try to read the two matches file formats
(
!(matches_provider->load(sfm_data, stlplus::create_filespec(_info_._g_match_dir_, "matches.e.txt")) ||
matches_provider->load(sfm_data, stlplus::create_filespec(_info_._g_match_dir_, "matches.e.bin")))
)
{
std::cerr << std::endl
<< "Invalid matches file." << std::endl;
return ;
}
if (_info_._g_point_cloud_dir.empty()) {
std::cerr << "\nIt is an invalid output directory" << std::endl;
return ;
}
if (!stlplus::folder_exists(_info_._g_point_cloud_dir))
{
if (!stlplus::folder_create(_info_._g_point_cloud_dir))
{
std::cerr << "\nCannot create the output directory" << std::endl;
}
}
//---------------------------------------
// Global SfM reconstruction process
//---------------------------------------
openMVG::system::Timer timer;
GlobalSfMReconstructionEngine_RelativeMotions sfmEngine(
sfm_data,
_info_._g_point_cloud_dir,
stlplus::create_filespec(_info_._g_point_cloud_dir, "Reconstruction_Report.html"));
// Configure the features_provider & the matches_provider
sfmEngine.SetFeaturesProvider(feats_provider.get());
sfmEngine.SetMatchesProvider(matches_provider.get());
// Configure reconstruction parameters
sfmEngine.Set_Intrinsics_Refinement_Type(intrinsic_refinement_options);
bool b_use_motion_priors = _info_._g_Has_Pos;
sfmEngine.Set_Use_Motion_Prior(b_use_motion_priors);
// Configure motion averaging method
sfmEngine.SetRotationAveragingMethod(
ERotationAveragingMethod(iRotationAveragingMethod));
sfmEngine.SetTranslationAveragingMethod(
ETranslationAveragingMethod(iTranslationAveragingMethod));
if (sfmEngine.Process())
{
std::cout << std::endl << " Total Ac-Global-Sfm took (s): " << timer.elapsed() << std::endl;
std::cout << "...Generating SfM_Report.html" << std::endl;
Generate_SfM_Report(sfmEngine.Get_SfM_Data(),
stlplus::create_filespec(_info_._g_point_cloud_dir, "SfMReconstruction_Report.html"));
//-- Export to disk computed scene (data & visualizable results)
std::cout << "...Export SfM_Data to disk." << std::endl;
Save(sfmEngine.Get_SfM_Data(),
stlplus::create_filespec(_info_._g_point_cloud_dir, "sfm_data", ".bin"),
ESfM_Data(ALL));
Save(sfmEngine.Get_SfM_Data(),
stlplus::create_filespec(_info_._g_point_cloud_dir, "cloud_and_poses", ".ply"),
ESfM_Data(ALL));
return ;
}
return ;
}