-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkittiReader.cpp
313 lines (277 loc) · 10.4 KB
/
kittiReader.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
#include "kittiReader.h"
#include "constsUtils.h"
#include <sstream>
#include <sys/stat.h>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <Eigen/Dense>
#include <cmath>
#include <gtsam/inference/Symbol.h>
// Kitti directory structure is assumed to be the same as the distribution
// originally, i.e.
// kittiDirectory -
// dataset -
// poses -
// 00.txt
// 01.txt
// etc.
// sequences -
// 00 -
// calib.txt
// image_0 (left gray) -
// 000000.png
// 000001.png
// etc
// image_1 (right gray) -
//
// image_2 (left color) -
//
// image_3 (right color) -
// devkit -
// Path to kitti below should be the path to the directory ABOVE dataset,
// i.e. "kittiDirectory" in the example above
const std::string pathToKitti("ENTER YOUR PATH HERE/");
// ###############################################################
kittiReader::kittiReader(int seqN, bool color){
this->seqN = seqN;
this->color = color;
// Not my smoothest presentation but... this is a check, not important
// Don't enter your path here
if(pathToKitti == "ENTER YOUR PATH HERE/"){
throw std::runtime_error("Fill in the path to the KITTI dataset in kittiReader.cpp!\n");
}
pathToData = pathToKitti + std::string("dataset/");
pathToSeq = pathToData + std::string("sequences/");
pathToPose = pathToData + std::string("poses/");
pathToResults = pathToKitti + std::string("results");
if(!file_exists(pathToResults)){
std::cout<<"Making results directory: "<<pathToResults<<std::endl;
mkdir(pathToResults.c_str(),0777);
}
char seqSpecifier[30];
sprintf(seqSpecifier,"%02i/",seqN);
this->seqSpec = std::string(seqSpecifier);
pathToSeq += seqSpecifier;
pathToResults += seqSpecifier;
if(!file_exists(pathToResults)){
std::cout<<"Making subresults directory for sequence: "<<seqSpecifier<<std::endl;
mkdir(pathToResults.c_str(),0777);
}
sprintf(seqSpecifier,"%02i.txt",seqN);
pathToPose += seqSpecifier;
loadCalib();
f = matplot::figure(true);
ax = f->current_axes();
}
// ###############################################################
void kittiReader::loadCalib(){
if(!file_exists(pathToSeq + std::string("calib.txt"))){
std::string errMsg("Kitti Calibration could not be found on path: ");
errMsg += pathToSeq + std::string("calib.txt\n");
throw std::runtime_error(errMsg);
}
std::ifstream myFile(pathToSeq + std::string("calib.txt"));
std::string line;
if(color){
// first two lines are for gray
std::getline(myFile,line);
std::getline(myFile,line);
}
std::getline(myFile,line);
std::stringstream ss(line);
// each line begins with a label
ss.ignore(4,' '); //ignore 4 chars, or until space
double val;
for(int count = 0; ss>>val ; count++){
switch(count){
case 0: fx = val; break;
case 1: s = val; break;
case 2: u0 = val; break;
case 3: initL = val; break;
case 5: fy = val; break;
case 6: v0 = val; break;
default: continue;
}
}
std::getline(myFile,line);
std::stringstream ss2(line);
// each line begins with a label
ss2.ignore(4,' '); //ignore 4 chars, or until space
for(int count = 0; ss2>>val ; count++){
if(count == 3){
initR = val;
}else if(count == 4){
break;
}
}
initL = initL/fx;
initR = initR/fx;
b = std::abs(initR-initL);
K = Eigen::Matrix<double,3,3>::Identity();
K(0,0) = fx;
K(1,1) = fy;
K(0,1) = s;
K(0,2) = u0;
K(1,2) = v0;
myFile.close();
}
// ###############################################################
gtsam::Pose3 kittiReader::groundTruth(int poseN){
if(!file_exists(pathToPose)){
std::string errMsg("Ground truth could not be found!\n Path to ground truth: \n");
errMsg += pathToPose + std::string("\n");
throw std::runtime_error(errMsg);
}
std::ifstream myFile(pathToPose);
std::string line;
int lineCounter = 0;
while(lineCounter < poseN){
lineCounter++;
if(!std::getline(myFile,line)){
std::string errMsg("Requested GT pose beyond end of file!\n");
errMsg += std::string("File being read: ") + pathToPose + std::string("\n");
errMsg += std::string("Lines read: ") + std::to_string(lineCounter);
errMsg += std::string(" Pose requested: ") + std::to_string(poseN);
errMsg += std::string("\n");
throw std::runtime_error(errMsg);
}
}
std::getline(myFile,line);
std::stringstream ss(line);
double gt[12];
for(int i = 0; i < 12; i++){
ss>>gt[i];
}
gtsam::Pose3 Pgt(gtsam::Rot3(gt[0],gt[1],gt[2],
gt[4],gt[5],gt[6],
gt[8],gt[9],gt[10]),
gtsam::Point3(gt[3],gt[7],gt[11]));
myFile.close();
return Pgt;
}
// ###############################################################
void kittiReader::writePoses(const std::vector<gtsam::Pose3>& x, std::string ID){
FILE *myFile;
if(!file_exists(pathToResults)){
std::string errMsg("Path to results directory could not be found!\nPath to directory: \n");
errMsg += pathToResults;
throw std::runtime_error(errMsg);
}
myFile = fopen((pathToResults + ID + std::string(".txt")).c_str(),"w");
for(size_t p = 0; p < x.size(); p++){
Eigen::Matrix<double,4,4> P = x[p].matrix();
fprintf(myFile,"%f %f %f %f %f %f %f %f %f %f %f %f\n",P(0,0),P(0,1),P(0,2),
P(0,3),P(1,0),P(1,1),P(1,2),P(1,3),P(2,0),P(2,1),P(2,2),P(2,3));
}
fclose(myFile);
}
// ###############################################################
void kittiReader::writeMetrics(const std::vector<double>& time4BBs,const std::vector<double>& time4Opt,
const std::vector<double>& time4ProbWin,const std::vector<double>& slidingFPS,
const std::vector<int>& numMeas,const std::vector<int>& numLand,std::string ID){
FILE *myFile;
if(!file_exists(pathToResults)){
std::string errMsg("Path to results directory could not be found!\nPath to directory: \n");
errMsg += pathToResults;
throw std::runtime_error(errMsg);
}
myFile = fopen((pathToResults + ID + std::string("_metrics.txt")).c_str(),"w");
for(size_t i = 0; i < time4BBs.size(); i++){
fprintf(myFile,"%g %g %g %g %d %d\n",time4BBs[i],time4Opt[i],time4ProbWin[i],slidingFPS[i],numMeas[i],numLand[i]);
}
fclose(myFile);
}
// ###############################################################
void kittiReader::showMetric(const std::vector<double>& metric, const std::string& name){
size_t numIter = metric.size(); //should all be the same size really...
std::vector<int> iterations(numIter);
for(size_t iter = 0; iter < numIter; iter++){
iterations[iter] = iter;
}
matplot::plot(iterations, metric);
matplot::xlabel("Iteration");
matplot::ylabel(name);
matplot::grid(matplot::on);
matplot::show();
}
// ###############################################################
void kittiReader::updateTrajectory(const gtsam::Values& estimate){
gtsam::KeyVector keys = estimate.keys();
for(size_t k = 0; k < keys.size(); k++){
gtsam::Symbol keyS(keys[k]);
if( (keyS.chr() == 'x') && ((keyS.index() % 2) == 0) ) {
//update the pose for this left camera index
int idx = static_cast<int>(keyS.index()/2);
gtsam::Vector3 t = estimate.at(keyS).cast<gtsam::Pose3>().translation();
if(idx+1 > trajx.size()){
//new pose
int buffer = idx-trajx.size(); //this adds zeros until the next element is the right index
for(int buf = 0; buf < buffer; buf++){
trajx.push_back(0);
trajz.push_back(0);
}
trajx.push_back(t(0));
trajz.push_back(t(2));
}else{
//updating previously added pose
trajx[idx] = t(0);
trajz[idx] = t(2);
}
}
}
// ax->plot(gtx, gtz);
// matplot::hold(matplot::on);
ax->plot(trajx, trajz);
// ax->xlim({-10, 100});
// ax->ylim({-10, 100});
// matplot::xlabel("X position [m]");
// matplot::ylabel("Z position [m]");
// matplot::grid(matplot::on);
f->draw();
}
// ###############################################################
std::vector< gtsam::Pose3 > kittiReader::groundTruthSet()
{
if(!file_exists(pathToPose)){
std::string errMsg("Ground truth could not be found!\n Path to ground truth: \n");
errMsg += pathToPose + std::string("\n");
throw std::runtime_error(errMsg);
}
std::vector< gtsam::Pose3 > gts;
// just a guess...
gts.reserve(2000);
gtx.reserve(2000);
gtz.reserve(2000);
std::ifstream myFile(pathToPose);
std::string line;
while(std::getline(myFile,line)){
// this is a requested pose!
std::stringstream ss(line);
double gt[12];
for(int i = 0; i < 12; i++){
ss>>gt[i];
}
gtsam::Pose3 Pgt(gtsam::Rot3(gt[0],gt[1],gt[2],
gt[4],gt[5],gt[6],
gt[8],gt[9],gt[10]),
gtsam::Point3(gt[3],gt[7],gt[11]));
gts.push_back(Pgt);
gtx.push_back(gt[3]);
gtz.push_back(gt[11]);
}
// if(!getAll){
// // if the loop exited, but there is still a pose request, then
// // it must have been beyond the end of the file!
// if(lineCount < posesN[counter]){
// std::string errMsg("Requested GT pose beyond end of file!\n");
// errMsg += std::string("File being read: ") + pathToPose + std::string("\n");
// errMsg += std::string("Lines read: ") + std::to_string(lineCount+1);
// errMsg += std::string(" Pose requested: ") + std::to_string(posesN[counter]);
// errMsg += std::string("\n");
// throw std::runtime_error(errMsg);
// }
// }
myFile.close();
return gts;
}