-
Notifications
You must be signed in to change notification settings - Fork 83
/
SampleOutputWrapper.h
356 lines (268 loc) · 12.4 KB
/
SampleOutputWrapper.h
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
/**
* 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/>.
*/
#pragma once
#include "boost/thread.hpp"
#include "util/MinimalImage.h"
#include "IOWrapper/Output3DWrapper.h"
#include "FullSystem/HessianBlocks.h"
#include "util/FrameShell.h"
// For outputting as .csv file
#include <iostream>
#include <fstream>
// For outputting a POST request
#include "util/HTTPPOSTRequest.h"
namespace dso
{
class FrameHessian;
class CalibHessian;
class FrameShell;
namespace IOWrap
{
class SampleOutputWrapper : public Output3DWrapper
{
public:
// CSV File output
std::ofstream posesCSV;
std::ofstream hessiansCSV;
std::ofstream pointCloudOBJ;
// HTTP POST Request
dso::HTTPPOSTRequest httpPOSTRequest;
inline SampleOutputWrapper()
{
posesCSV.open("/home/akp/cameraPoses.csv");
hessiansCSV.open("/home/akp/cameraHessians.csv");
pointCloudOBJ.open("/home/akp/cameraPointcloud.obj");
pointCloudOBJ << "o DSO_Pointcloud\n";
// POST to Webserver:
httpPOSTRequest.init("master.kalisz.co", "http");
printf("OUT: Created SampleOutputWrapper\n");
}
virtual ~SampleOutputWrapper()
{
posesCSV.close();
hessiansCSV.close();
pointCloudOBJ.close();
printf("OUT: Destroyed SampleOutputWrapper\n");
}
virtual void publishGraph(const std::map<uint64_t,Eigen::Vector2i> &connectivity)
{
/*
printf("OUT: got graph with %d edges\n", (int)connectivity.size());
int maxWrite = 5;
for(const std::pair<uint64_t,Eigen::Vector2i> &p : connectivity)
{
int idHost = p.first>>32;
int idTarget = p.first & ((uint64_t)0xFFFFFFFF);
printf("OUT: Example Edge %d -> %d has %d active and %d marg residuals\n", idHost, idTarget, p.second[0], p.second[1]);
maxWrite--;
if(maxWrite==0) break;
}
*/
}
virtual void publishKeyframes( std::vector<FrameHessian*> &frames, bool final, CalibHessian* HCalib)
{
// Used for acumulating model:
for(FrameHessian* f : frames)
{
/*
printf("OUT: KF %d (%s) (id %d, tme %f): %d active, %d marginalized, %d immature points. CameraToWorld:\n",
f->frameID,
final ? "final" : "non-final",
f->shell->incoming_id,
f->shell->timestamp,
(int)f->pointHessians.size(), (int)f->pointHessiansMarginalized.size(), (int)f->immaturePoints.size());
std::cout << f->shell->camToWorld.matrix3x4() << "\n";
*/
long timestamp = f->shell->id * (1.0 / 25.0) * 1000.0;
hessiansCSV << timestamp << ",";
std::string pointCloudString = "";
bool firstPoint = true;
//int maxWrite = 5;
for(PointHessian* p : f->pointHessians)
{
/*
printf("OUT: Example Point x=%.1f, y=%.1f, idepth=%f, idepth std.dev. %f, %d inlier-residuals\n",
p->u, p->v, p->idepth_scaled, sqrt(1.0f / p->idepth_hessian), p->numGoodResiduals );
*/
//skip for final!=false (according to Output3DWrapper.h)
if(final==false)
{
// Go through all points in the pattern
//for(int pnt=0;pnt<patternNum;pnt++)
//{
//int dx = patternP[pnt][0];
//int dy = patternP[pnt][1];
float fx,fy,cx,cy;
float fxi,fyi,cxi,cyi;
fx = HCalib->fxl();
fy = HCalib->fyl();
cx = HCalib->cxl();
cy = HCalib->cyl();
fxi = 1/fx;
fyi = 1/fy;
cxi = -cx / fx;
cyi = -cy / fy;
// Transform to camera space (use inverse of calibration matrix):
float depth = 1.0f / p->idepth_scaled;
float x = ((p->u)*fxi + cxi) * depth; //((p->u+dx)*fxi + cxi) * depth;
float y = ((p->v)*fyi + cyi) * depth; //((p->v+dy)*fyi + cyi) * depth;
float z = depth*(1 + 2*fxi * (rand()/(float)RAND_MAX-0.5f));
// Create 3D Vector of point in world space
Eigen::Vector4d imagePoint(x,y,z, 1.0);
// Transform to world space:
Eigen::Matrix4d cam2World( f->shell->camToWorld.matrix() );
Eigen::Vector4d worldPoint = cam2World * imagePoint;
pointCloudOBJ << "v " << worldPoint[0] << " " << worldPoint[1] << " " << worldPoint[2] << "\n";
pointCloudOBJ.flush();
if(!firstPoint)
{
pointCloudString += ";";
}
pointCloudString += std::to_string(worldPoint[0]) + ","
+ std::to_string(worldPoint[1]) + ","
+ std::to_string(worldPoint[2]) + ","
+ std::to_string(p->color[0]) + ","
+ std::to_string(p->color[1]) + ","
+ std::to_string(p->color[2]);
//}
}
hessiansCSV << p->idepth_hessian << ", ";
//Flush because Destructor is never called...
hessiansCSV.flush();
//maxWrite--;
//if(maxWrite==0) break;
}
if(pointCloudString != "")
{
//httpPOSTRequest.addPointCloud(pointCloudString);
}
}
}
virtual void publishCamPose(FrameShell* frame, CalibHessian* HCalib)
{
// First show timestamp (based on 25 fps) in milliseconds:
long timestamp = frame->id * (1.0 / 25.0) * 1000.0;
posesCSV << timestamp << ",";
SE3 matrix = frame->camToWorld;
Eigen::Vector3d translation = matrix.translation().transpose().cast<double>();
Eigen::Quaterniond quaternion = matrix.so3().unit_quaternion().cast<double>();
// Translation:
posesCSV << translation[0] << ","
<< translation[1] << ","
<< translation[2] << ",";
// Quaternion:
posesCSV << quaternion.w() << ","
<< quaternion.x() << ","
<< quaternion.y() << ","
<< quaternion.z() << "\n";
posesCSV.flush(); //Flush because Destructor is never called...
//std::cout << "Publishing new camera pose via HTTP" << std::endl;
httpPOSTRequest.addCameraPose(timestamp, matrix.translation(), matrix.unit_quaternion() );
/*
printf("OUT: Current Frame %d (time %f, internal ID %d). CameraToWorld:\n",
frame->incoming_id,
frame->timestamp,
frame->id);
// Matrix3x4 from Sophus library is of type SE3
// which - in turn - internally is a Matrix<Scalar,3,4>
// Conversion to Translation and Quaternion is below.
// First show timestamp (based on 25 fps) in milliseconds:
double timestep_ms = (1.0 / 25.0) * 1000.0;
Eigen::Affine3d dsoPose(frame->camToWorld.matrix3x4());
// Matrix converting from DSO space to Blender space (will be used later for visualization):
// x = -x
// y = z
// z = -y
Eigen::Matrix3d linearD2B(3,3);
linearD2B << -1, 0, 0,
0, 0, 1,
0, -1, 0;
Eigen::Affine3d dso2Blender;
dso2Blender.setIdentity();
dso2Blender.linear() = linearD2B;
// Transform the pose back to Blender:
Eigen::Affine3d blenderCam2World = dso2Blender * dsoPose;
Eigen::Affine3d blenderWorld2Cam = blenderCam2World.inverse(); //Inverse = World to Camera
Eigen::Quaterniond q(blenderWorld2Cam.linear());
Eigen::Vector3d t(blenderWorld2Cam.translation());
posesCSV << frame->id * timestep_ms << ",";
// Translation:
posesCSV << t.x() << ","
<< t.y() << ","
<< t.z() << ",";
// Quaternion:
posesCSV << q.w() << ","
<< q.x() << ","
<< q.y() << ","
<< q.z() << "\n";
//Flush because Destructor is never called...
posesCSV.flush();
*/
}
virtual void pushLiveFrame(FrameHessian* image)
{
/*
// can be used to get the raw image / intensity pyramid.
printf("OUT: pushLiveFrame %d (time %f, internal ID %d). CameraToWorld:\n",
image->shell->incoming_id,
image->shell->timestamp,
image->shell->id);
FrameShell* shell = image->shell;
std::cout << shell->camToWorld.matrix3x4() << "\n";
std::cout << "camToTrackingRef: " << shell->camToTrackingRef.matrix3x4() << "\n";
*/
}
virtual void pushDepthImage(MinimalImageB3* image)
{
// can be used to get the raw image with depth overlay.
}
virtual bool needPushDepthImage()
{
return false;
}
virtual void pushDepthImageFloat(MinimalImageF* image, FrameHessian* KF )
{
/*
printf("OUT: Predicted depth for KF %d (id %d, time %f, internal frame-ID %d). CameraToWorld:\n",
KF->frameID,
KF->shell->incoming_id,
KF->shell->timestamp,
KF->shell->id);
std::cout << KF->shell->camToWorld.matrix3x4() << "\n";
int maxWrite = 5;
for(int y=0;y<image->h;y++)
{
for(int x=0;x<image->w;x++)
{
if(image->at(x,y) <= 0) continue;
printf("OUT: Example Idepth at pixel (%d,%d): %f.\n", x,y,image->at(x,y));
maxWrite--;
if(maxWrite==0) break;
}
if(maxWrite==0) break;
}
*/
}
};
}
}