-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLibViso.cpp
174 lines (140 loc) · 4.68 KB
/
LibViso.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
#include "LibViso.h"
LibViso::LibViso(const QString& imgDir)
:imgDir(imgDir)
{
moveToThread(this);
result = true;
std::string logPath = "log/LibViso.log";
std::string logDir = stlplus::folder_part(logPath);
if (!stlplus::folder_exists(logDir))
{
stlplus::folder_create(logDir);
}
logFile.open(logPath.c_str());
stdFile = std::cout.rdbuf(logFile.rdbuf());
std::cerr.rdbuf(logFile.rdbuf());
std::cout << " currentThreadId : " << this->currentThreadId << std::endl;
}
LibViso::~LibViso()
{
std::cout.rdbuf(stdFile);
//delete stdFile;
if (logFile.is_open()) logFile.close();
}
void LibViso::run()
{
if (!stlplus::folder_exists(imgDir.toStdString()))
{
std::cerr << "\nImgDir : " << imgDir.toStdString() << " directory doesn't exist" << std::endl;
result = false;
return;
}
emit sendStatues(QStringLiteral("Initial Option ....."));
// set most important visual odometry parameters
// for a full parameter list, look at: viso_stereo.h
libviso::VisualOdometryMono::parameters param;
// calibration parameters for sequence 2010_03_09_drive_0019
param.calib.f = 645.24; // focal length in pixels
param.calib.cu = 635.96; // principal point (u-coordinate) in pixels
param.calib.cv = 194.13; // principal point (v-coordinate) in pixels
param.height = 1.6;
param.pitch = -0.08;
param.bucket.max_features = 1000; //disable bucketing
// init visual odometry
libviso::VisualOdometryMono viso(param);
libviso::Reconstruction R; R.setCalibration(param.calib.f, param.calib.cu, param.calib.cv);
emit sendStatues(QStringLiteral("libviso running ....."));
libviso::Matrix poseG = libviso::Matrix::eye(4);
std::vector<libviso::Reconstruction::point3d> pointCloud;
bool replace = false;
// loop through all frames
for (int32_t i = 0; i<373; i++)
{
// input file names
char base_name[256]; sprintf(base_name, "%06d.png", i);
std::string limgName = imgDir.toStdString() + "/I1_" + base_name;
std::vector<libviso::Matrix> points;
// catch image read/write errors here
try {
// load left and right input image
Image<unsigned char> limg;
if (!openMVG::ReadImage(limgName.c_str(), &limg))
continue;
// image dimensions
int32_t width = limg.Width();
int32_t height = limg.Height();
// status
std::cout << "Processing: Frame: " << "->width : " << width << " , height" << height << i << std::endl;;
// compute visual odometry
int32_t dims[] = { width, height, width };
if (viso.process(limg.data(), dims, replace))
{
if (i < 2) continue;
// on success, update current pose
libviso::Matrix pose = viso.getMotion();
if (pose.val != nullptr)
{
//pose = libviso::Matrix::inv(pose);
poseG = poseG * pose;
std::vector<libviso::Matcher::p_match> p_matched = viso.getMatches();
// output some statistics
double num_matches = viso.getNumberOfMatches();
double num_inliers = viso.getNumberOfInliers();
std::cout << ", Matches: " << num_matches;
std::cout << ", Inliers: " << 100.0*num_inliers / num_matches << " %" << ", Current pose: " << std::endl;
std::cout << pose << std::endl << std::endl;
R.update(p_matched, pose, 2, 2, 30, 3);
}
else
{
replace = true;
}
std::vector<libviso::Reconstruction::point3d> points = R.getPoints();
for (const auto& p : points)
{
libviso::Matrix temp(1, 4);
temp.val[0][0] = p.x;
temp.val[0][1] = p.y;
temp.val[0][2] = p.z;
temp.val[0][3] = 1;
temp = temp * pose;
pointCloud.push_back(libviso::Reconstruction::point3d((float)temp.val[0][0], (float)temp.val[0][1], (float)temp.val[0][2]));
}
}
// catch image read errors here
}
catch (...)
{
std::cerr << "ERROR: Couldn't read input files!" << std::endl;
result = false;
return ;
}
}
exportPLY(pointCloud, imgDir.toStdString() + "/pointClouds.ply");
// output
std::cout << "Demo complete! Exiting ..." << std::endl;
emit sendStatues(QStringLiteral("LibViso is ok !"));
}
/// Export 3D point vector and camera position to PLY format
void LibViso::exportPLY(const std::vector<libviso::Reconstruction::point3d>& pointCloud, const std::string& fileName)
{
std::ofstream outfile;
outfile.open(fileName.c_str(), std::ios_base::out);
outfile << "ply"
<< '\n' << "format ascii 1.0"
<< '\n' << "element vertex " << pointCloud.size()
<< '\n' << "property float x"
<< '\n' << "property float y"
<< '\n' << "property float z"
<< '\n' << "property uchar red"
<< '\n' << "property uchar green"
<< '\n' << "property uchar blue"
<< '\n' << "end_header" << std::endl;
for (const auto& p : pointCloud)
{
outfile << p.x << " " << p.y << " " << p.z << "\n";
outfile << "0 0 255 " << "\n";
}
outfile.flush();
outfile.close();
}