-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_dso_pangolin.cpp
593 lines (495 loc) · 16.4 KB
/
main_dso_pangolin.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
* This file is part of DSO.
*
* Copyright 2016 Technical University of Munich and Intel.
* Developed by Jakob Engel <engelj at in dot tum dot de>,
* for more information see <http://vision.in.tum.de/dso>.
* If you use this code, please cite the respective publications as
* listed on the above website.
*
* DSO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DSO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DSO. If not, see <http://www.gnu.org/licenses/>.
*/
#include <thread>
#include <locale.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "IOWrapper/Output3DWrapper.h"
#include "IOWrapper/ImageDisplay.h"
#include <boost/thread.hpp>
#include "util/settings.h"
#include "util/globalFuncs.h"
#include "util/DatasetReader.h"
#include "util/globalCalib.h"
#include "util/NumType.h"
#include "FullSystem/FullSystem.h"
#include "OptimizationBackend/MatrixAccumulators.h"
#include "FullSystem/PixelSelector2.h"
#include "IOWrapper/Pangolin/PangolinDSOViewer.h"
#include "IOWrapper/OutputWrapper/SampleOutputWrapper.h"
std::string vignette = "";
std::string gammaCalib = "";
std::string source = "";
std::string calib = "";
double rescale = 1;
bool reverse = false;
bool disableROS = false;
int start=0;
int end=100000;
bool prefetch = false;
//控制速度的??
float playbackSpeed=0; // 0 for linearize (play as fast as possible, while sequentializing tracking & mapping). otherwise, factor on timestamps.
bool preload=false;
bool useSampleOutput=false;
int mode=0;
bool firstRosSpin=false;
using namespace dso;
void my_exit_handler(int s)
{
printf("Caught signal %d\n",s);
exit(1);
}
void exitThread()
{
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = my_exit_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
firstRosSpin=true;
while(true) pause();
}
void settingsDefault(int preset)
{
printf("\n=============== PRESET Settings: ===============\n");
if(preset == 0 || preset == 1)
{
printf("DEFAULT settings:\n"
"- %s real-time enforcing\n"
"- 2000 active points\n"
"- 5-7 active frames\n"
"- 1-6 LM iteration each KF\n"
"- original image resolution\n", preset==0 ? "no " : "1x");
playbackSpeed = (preset==0 ? 0 : 1);
preload = preset==1;
setting_desiredImmatureDensity = 1500;
setting_desiredPointDensity = 2000;
setting_minFrames = 5;
setting_maxFrames = 7;
setting_maxOptIterations=6;
setting_minOptIterations=1;
setting_logStuff = false;
}
if(preset == 2 || preset == 3)
{
printf("FAST settings:\n"
"- %s real-time enforcing\n"
"- 800 active points\n"
"- 4-6 active frames\n"
"- 1-4 LM iteration each KF\n"
"- 424 x 320 image resolution\n", preset==0 ? "no " : "5x");
playbackSpeed = (preset==2 ? 0 : 5);
preload = preset==3;
setting_desiredImmatureDensity = 600;
setting_desiredPointDensity = 800;
setting_minFrames = 4;
setting_maxFrames = 6;
setting_maxOptIterations=4;
setting_minOptIterations=1;
benchmarkSetting_width = 424;
benchmarkSetting_height = 320;
setting_logStuff = false;
}
printf("==============================================\n");
}
/******************处理指令传入参数调整系统设置************************/
void parseArgument(char* arg)
{
int option;
float foption;
char buf[1000];
/*******从arg字符串中格式化读入一个整数值,并存入option中********/
if(1==sscanf(arg,"sampleoutput=%d",&option))
{
if(option==1)
{
useSampleOutput = true;
printf("USING SAMPLE OUTPUT WRAPPER!\n");
}
return;
}
if(1==sscanf(arg,"quiet=%d",&option))
{
if(option==1)
{
setting_debugout_runquiet = true;
printf("QUIET MODE, I'll shut up!\n");
}
return;
}
if(1==sscanf(arg,"preset=%d",&option))
{
settingsDefault(option);
return;
}
if(1==sscanf(arg,"rec=%d",&option))
{
if(option==0)
{
disableReconfigure = true;
printf("DISABLE RECONFIGURE!\n");
}
return;
}
/*******设置不启用ROS***********/
if(1==sscanf(arg,"noros=%d",&option))
{
if(option==1)
{
disableROS = true;
disableReconfigure = true;
printf("DISABLE ROS (AND RECONFIGURE)!\n");
}
return;
}
/**************设置不打印日志输出***************/
if(1==sscanf(arg,"nolog=%d",&option))
{
if(option==1)
{
setting_logStuff = false;
printf("DISABLE LOGGING!\n");
}
return;
}
if(1==sscanf(arg,"reverse=%d",&option))
{
if(option==1)
{
reverse = true;
printf("REVERSE!\n");
}
return;
}
/*************设置不开启GUI*************/
if(1==sscanf(arg,"nogui=%d",&option))
{
if(option==1)
{
disableAllDisplay = true;
printf("NO GUI!\n");
}
return;
}
/************设置不开启多线程**********/
if(1==sscanf(arg,"nomt=%d",&option))
{
if(option==1)
{
multiThreading = false;
printf("NO MultiThreading!\n");
}
return;
}
if(1==sscanf(arg,"prefetch=%d",&option))
{
if(option==1)
{
prefetch = true;
printf("PREFETCH!\n");
}
return;
}
if(1==sscanf(arg,"start=%d",&option))
{
start = option;
printf("START AT %d!\n",start);
return;
}
if(1==sscanf(arg,"end=%d",&option))
{
end = option;
printf("END AT %d!\n",start);
return;
}
/****************加载数据集**************/
if(1==sscanf(arg,"files=%s",buf))
{
source = buf; //source中存入了图片文件
printf("loading data from %s!\n", source.c_str());
return;
}
/**************加载相机标定**************/
if(1==sscanf(arg,"calib=%s",buf))
{
calib = buf;
printf("loading calibration from %s!\n", calib.c_str());
return;
}
/**************加载相机边缘去畸变**************/
if(1==sscanf(arg,"vignette=%s",buf))
{
vignette = buf;
printf("loading vignette from %s!\n", vignette.c_str());
return;
}
/**************加载相机gamma校正***********/
if(1==sscanf(arg,"gamma=%s",buf))
{
gammaCalib = buf;
printf("loading gammaCalib from %s!\n", gammaCalib.c_str());
return;
}
if(1==sscanf(arg,"rescale=%f",&foption))
{
rescale = foption;
printf("RESCALE %f!\n", rescale);
return;
}
if(1==sscanf(arg,"speed=%f",&foption))
{
playbackSpeed = foption;
printf("PLAYBACK SPEED %f!\n", playbackSpeed);
return;
}
if(1==sscanf(arg,"save=%d",&option))
{
if(option==1)
{
debugSaveImages = true;
if(42==system("rm -rf images_out")) printf("system call returned 42 - what are the odds?. This is only here to shut up the compiler.\n");
if(42==system("mkdir images_out")) printf("system call returned 42 - what are the odds?. This is only here to shut up the compiler.\n");
if(42==system("rm -rf images_out")) printf("system call returned 42 - what are the odds?. This is only here to shut up the compiler.\n");
if(42==system("mkdir images_out")) printf("system call returned 42 - what are the odds?. This is only here to shut up the compiler.\n");
printf("SAVE IMAGES!\n");
}
return;
}
/***************设置系统运行时是否开启光度标定,引入两个光度参数*************/
if(1==sscanf(arg,"mode=%d",&option))
{
mode = option;
if(option==0)
{
printf("PHOTOMETRIC MODE WITH CALIBRATION!\n");
}
if(option==1)
{
printf("PHOTOMETRIC MODE WITHOUT CALIBRATION!\n");
setting_photometricCalibration = 0;
setting_affineOptModeA = 0; //-1: fix. >=0: optimize (with prior, if > 0).
setting_affineOptModeB = 0; //-1: fix. >=0: optimize (with prior, if > 0).
}
if(option==2)
{
printf("PHOTOMETRIC MODE WITH PERFECT IMAGES!\n");
setting_photometricCalibration = 0;
setting_affineOptModeA = -1; //-1: fix. >=0: optimize (with prior, if > 0).
setting_affineOptModeB = -1; //-1: fix. >=0: optimize (with prior, if > 0).
setting_minGradHistAdd=3;
}
return;
}
printf("could not parse argument \"%s\"!!!!\n", arg);
}
int main( int argc, char** argv )
{
//setlocale(LC_ALL, "");
//解析命令行传入的参数
for(int i=1; i<argc;i++)
parseArgument(argv[i]);
// hook crtl+C.
boost::thread exThread = boost::thread(exitThread);
//标定矫正相机内参矩阵K
ImageFolderReader* reader = new ImageFolderReader(source,calib, gammaCalib, vignette);
reader->setGlobalCalibration();
//图像去Gamma畸变操作
if(setting_photometricCalibration > 0 && reader->getPhotometricGamma() == 0)
{
printf("ERROR: dont't have photometric calibation. Need to use commandline options mode=1 or mode=2 ");
exit(1);
}
//将读入图片的顺序倒置
int lstart=start;
int lend = end;
int linc = 1; //可以调节遍历的图像序列间隔
if(reverse)
{
printf("REVERSE!!!!");
lstart=end-1;
if(lstart >= reader->getNumImages())
lstart = reader->getNumImages()-1;
lend = start;
linc = -1;
}
//创建整个系统 new出来的fullSystem是个指针 同时也实例化了很多关键的类 具体查看FullSystem.cpp中的构造函数
FullSystem* fullSystem = new FullSystem();
fullSystem->setGammaFunction(reader->getPhotometricGamma());
fullSystem->linearizeOperation = (playbackSpeed==0);
//创建GUI
IOWrap::PangolinDSOViewer* viewer = 0;
if(!disableAllDisplay)
{
viewer = new IOWrap::PangolinDSOViewer(wG[0],hG[0], false);
fullSystem->outputWrapper.push_back(viewer);
}
if(useSampleOutput)
fullSystem->outputWrapper.push_back(new IOWrap::SampleOutputWrapper());
// to make MacOS happy: run this in dedicated thread -- and use this one to run the GUI.
std::thread runthread([&]() {
std::vector<int> idsToPlay; //整型向量
std::vector<double> timesToPlayAt; //浮点型向量
/*********遍历图像序列的index和timestmap保存到idsToPlay和timesToPlayAt*******/
/******************确定了每帧图像的时刻以及整个序列的大小********************/
for(int i = lstart; i >= 0 && i< reader->getNumImages() && linc*i < linc*lend;i+=linc)
{
//动态vector容器 size在不断变化
idsToPlay.push_back(i);
if(timesToPlayAt.size() == 0) //?????
{
timesToPlayAt.push_back((double)0);
}
else
{
//获取当前读入图像的时间戳
double tsThis = reader->getTimestamp(idsToPlay[idsToPlay.size()-1]); //idsToPlay。size()-1是当前帧的index
//获取前一个时刻读入图像的时间戳
double tsPrev = reader->getTimestamp(idsToPlay[idsToPlay.size()-2]);
//将当前帧的运行时刻存储到timesToPlayAt
timesToPlayAt.push_back(timesToPlayAt.back() + fabs(tsThis-tsPrev)/playbackSpeed);
}
}
std::vector<ImageAndExposure*> preloadedImages;
if(preload)
{
printf("LOADING ALL IMAGES!\n");
for(int ii=0;ii<(int)idsToPlay.size(); ii++)
{
int i = idsToPlay[ii];
preloadedImages.push_back(reader->getImage(i));
}
}
struct timeval tv_start;
gettimeofday(&tv_start, NULL);
clock_t started = clock();
double sInitializerOffset=0;
/*******************通过idsTplay去遍历读入图像序列********************/
for(int ii=0;ii<(int)idsToPlay.size(); ii++)
{
// if not initialized: reset start time.
if(!fullSystem->initialized)
{
gettimeofday(&tv_start, NULL);
started = clock();
sInitializerOffset = timesToPlayAt[ii];
}
int i = idsToPlay[ii];
//实例化ImageAndExposure对象(指针)
ImageAndExposure* img;
if(preload)
img = preloadedImages[ii];
else
img = reader->getImage(i); //读入图像
bool skipFrame=false;
if(playbackSpeed!=0)
{
struct timeval tv_now; gettimeofday(&tv_now, NULL);
double sSinceStart = sInitializerOffset + ((tv_now.tv_sec-tv_start.tv_sec) + (tv_now.tv_usec-tv_start.tv_usec)/(1000.0f*1000.0f));
if(sSinceStart < timesToPlayAt[ii])
usleep((int)((timesToPlayAt[ii]-sSinceStart)*1000*1000));
else if(sSinceStart > timesToPlayAt[ii]+0.5+0.1*(ii%2))
{
printf("SKIPFRAME %d (play at %f, now it is %f)!\n", ii, timesToPlayAt[ii], sSinceStart);
skipFrame=true;
}
}
/***********the input of Direct Sparse Odemetry System!!!!************/
///没有跳过这一帧,则通过Fullsystem::addActiveFrame为该帧创建FrameHessiang和FrameShell
// - FrameHessian保存影像信息
// - FrameShell保存帧的位姿信息
//这里读入的img是ImageAndExposure类的对象,此时img不是原始图像,而是经过光度标定后的辐射值图像,但像素强度仍为(0,255)
if(!skipFrame) fullSystem->addActiveFrame(img, i);
//用完了就删掉img指针
delete img;
//初始化失败了就重新创建新的fullsystem读入下一帧
if(fullSystem->initFailed || setting_fullResetRequested)
{
if(ii < 250 || setting_fullResetRequested) //ii<250 ???
{
printf("RESETTING!\n");
std::vector<IOWrap::Output3DWrapper*> wraps = fullSystem->outputWrapper;
delete fullSystem;
for(IOWrap::Output3DWrapper* ow : wraps) ow->reset();
fullSystem = new FullSystem();
fullSystem->setGammaFunction(reader->getPhotometricGamma());
fullSystem->linearizeOperation = (playbackSpeed==0);
fullSystem->outputWrapper = wraps;
setting_fullResetRequested=false;
}
}
//跟踪失败了 直接break
if(fullSystem->isLost)
{
printf("LOST!!\n");
break;
}
}
fullSystem->blockUntilMappingIsFinished();
clock_t ended = clock();
struct timeval tv_end;
gettimeofday(&tv_end, NULL);
fullSystem->printResult("result.txt");
int numFramesProcessed = abs(idsToPlay[0]-idsToPlay.back());
double numSecondsProcessed = fabs(reader->getTimestamp(idsToPlay[0])-reader->getTimestamp(idsToPlay.back()));
double MilliSecondsTakenSingle = 1000.0f*(ended-started)/(float)(CLOCKS_PER_SEC);
double MilliSecondsTakenMT = sInitializerOffset + ((tv_end.tv_sec-tv_start.tv_sec)*1000.0f + (tv_end.tv_usec-tv_start.tv_usec)/1000.0f);
printf("\n======================"
"\n%d Frames (%.1f fps)"
"\n%.2fms per frame (single core); "
"\n%.2fms per frame (multi core); "
"\n%.3fx (single core); "
"\n%.3fx (multi core); "
"\n======================\n\n",
numFramesProcessed, numFramesProcessed/numSecondsProcessed,
MilliSecondsTakenSingle/numFramesProcessed,
MilliSecondsTakenMT / (float)numFramesProcessed,
1000 / (MilliSecondsTakenSingle/numSecondsProcessed),
1000 / (MilliSecondsTakenMT / numSecondsProcessed));
//fullSystem->printFrameLifetimes();
if(setting_logStuff)
{
std::ofstream tmlog;
tmlog.open("logs/time.txt", std::ios::trunc | std::ios::out);
tmlog << 1000.0f*(ended-started)/(float)(CLOCKS_PER_SEC*reader->getNumImages()) << " "
<< ((tv_end.tv_sec-tv_start.tv_sec)*1000.0f + (tv_end.tv_usec-tv_start.tv_usec)/1000.0f) / (float)reader->getNumImages() << "\n";
tmlog.flush();
tmlog.close();
}
});
if(viewer != 0)
viewer->run();
runthread.join();
for(IOWrap::Output3DWrapper* ow : fullSystem->outputWrapper)
{
ow->join();
delete ow;
}
printf("DELETE FULLSYSTEM!\n");
delete fullSystem;
printf("DELETE READER!\n");
delete reader;
printf("EXIT NOW!\n");
return 0;
}