-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualSensor.h
276 lines (221 loc) · 6.41 KB
/
VirtualSensor.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
#pragma once
#include <vector>
#include <iostream>
#include <cstring>
#include <fstream>
#include <filesystem>
#include "Eigen.h"
#include "FreeImageHelper.h"
typedef unsigned char BYTE;
// reads sensor files according to https://vision.in.tum.de/data/datasets/rgbd-dataset/file_formats
class VirtualSensor
{
public:
VirtualSensor() : m_currentIdx(-1), m_increment(10)
{
}
~VirtualSensor()
{
SAFE_DELETE_ARRAY(m_depthFrame);
SAFE_DELETE_ARRAY(m_colorFrame);
}
bool Init(const std::string& datasetDir)
{
m_baseDir = datasetDir;
// read filename lists
if (!ReadFileList(datasetDir + "depth.txt", m_filenameDepthImages, m_depthImagesTimeStamps)) return false;
if (!ReadFileList(datasetDir + "rgb.txt", m_filenameColorImages, m_colorImagesTimeStamps)) return false;
// read trackings
if (!ReadTrajectoryFile(datasetDir + "groundtruth.txt", m_trajectory, m_trajectoryTimeStamps)) return false;
// Commented the following line for the freiburg2 dataset..
// if (m_filenameDepthImages.size() != m_filenameColorImages.size()) return false;
// image resolutions
m_colorImageWidth = 640;
m_colorImageHeight = 480;
m_depthImageWidth = 640;
m_depthImageHeight = 480;
// intrinsics
m_colorIntrinsics << 525.0f, 0.0f, 319.5f,
0.0f, 525.0f, 239.5f,
0.0f, 0.0f, 1.0f;
m_depthIntrinsics = m_colorIntrinsics;
m_colorExtrinsics.setIdentity();
m_depthExtrinsics.setIdentity();
m_depthFrame = new float[m_depthImageWidth*m_depthImageHeight];
for (unsigned int i = 0; i < m_depthImageWidth*m_depthImageHeight; ++i) m_depthFrame[i] = 0.5f;
m_colorFrame = new BYTE[4* m_colorImageWidth*m_colorImageHeight];
for (unsigned int i = 0; i < 4*m_colorImageWidth*m_colorImageHeight; ++i) m_colorFrame[i] = 255;
m_currentIdx = -1;
return true;
}
bool ProcessNextFrame()
{
if (m_currentIdx == -1) m_currentIdx = 0;
else m_currentIdx += m_increment;
if ((unsigned int)m_currentIdx >= (unsigned int)m_filenameColorImages.size()) return false;
std::cout << "ProcessNextFrame [" << m_currentIdx << " | " << m_filenameColorImages.size() << "]" << std::endl;
FreeImageB rgbImage;
rgbImage.LoadImageFromFile(m_baseDir + m_filenameColorImages[m_currentIdx]);
memcpy(m_colorFrame, rgbImage.data, 4 * 640 * 480);
// depth images are scaled by 5000 (see https://vision.in.tum.de/data/datasets/rgbd-dataset/file_formats)
FreeImageU16F dImage;
dImage.LoadImageFromFile(m_baseDir + m_filenameDepthImages[m_currentIdx]);
for (unsigned int i = 0; i < m_depthImageWidth*m_depthImageHeight; ++i)
{
if (dImage.data[i] == 0)
m_depthFrame[i] = MINF;
else
m_depthFrame[i] = dImage.data[i] * 1.0f / 5000.0f;
}
// find transformation (simple nearest neighbor, linear search)
double timestamp = m_depthImagesTimeStamps[m_currentIdx];
double min = std::numeric_limits<double>::max();
int idx = 0;
for (unsigned int i = 0; i < m_trajectory.size(); ++i)
{
double d = fabs(m_trajectoryTimeStamps[i] - timestamp);
if (min > d)
{
min = d;
idx = i;
}
}
m_currentTrajectory = m_trajectory[idx];
return true;
}
unsigned int GetCurrentFrameCnt()
{
return (unsigned int)m_currentIdx;
}
// get current color data
BYTE* GetColorRGBX()
{
return m_colorFrame;
}
// get current depth data
float* GetDepth()
{
return m_depthFrame;
}
// color camera info
Eigen::Matrix3f GetColorIntrinsics()
{
return m_colorIntrinsics;
}
Eigen::Matrix4f GetColorExtrinsics()
{
return m_colorExtrinsics;
}
unsigned int GetColorImageWidth()
{
return m_colorImageWidth;
}
unsigned int GetColorImageHeight()
{
return m_colorImageHeight;
}
// depth (ir) camera info
Eigen::Matrix3f GetDepthIntrinsics()
{
return m_depthIntrinsics;
}
Eigen::Matrix4f GetDepthExtrinsics()
{
return m_depthExtrinsics;
}
unsigned int GetDepthImageWidth()
{
return m_colorImageWidth;
}
unsigned int GetDepthImageHeight()
{
return m_colorImageHeight;
}
// get current trajectory transformation
Eigen::Matrix4f GetTrajectory()
{
return m_currentTrajectory;
}
private:
bool ReadFileList(const std::string& filename, std::vector<std::string>& result, std::vector<double>& timestamps)
{
std::ifstream fileDepthList(filename, std::ios::in);
if (!fileDepthList.is_open()) return false;
result.clear();
timestamps.clear();
std::string dump;
std::getline(fileDepthList, dump);
std::getline(fileDepthList, dump);
std::getline(fileDepthList, dump);
while (fileDepthList.good())
{
double timestamp;
fileDepthList >> timestamp;
std::string filename;
fileDepthList >> filename;
if (filename == "") break;
timestamps.push_back(timestamp);
result.push_back(filename);
}
fileDepthList.close();
return true;
}
bool ReadTrajectoryFile(const std::string& filename, std::vector<Eigen::Matrix4f>& result, std::vector<double>& timestamps)
{
std::ifstream file(filename, std::ios::in);
if (!file.is_open()) return false;
result.clear();
std::string dump;
std::getline(file, dump);
std::getline(file, dump);
std::getline(file, dump);
while (file.good())
{
double timestamp;
file >> timestamp;
Eigen::Vector3f translation;
file >> translation.x() >> translation.y() >> translation.z();
Eigen::Quaternionf rot;
file >> rot;
Eigen::Matrix4f transf;
transf.setIdentity();
transf.block<3, 3>(0, 0) = rot.toRotationMatrix();
transf.block<3, 1>(0, 3) = translation;
if (rot.norm() == 0) break;
transf = transf.inverse().eval();
timestamps.push_back(timestamp);
result.push_back(transf);
}
file.close();
return true;
}
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
// current frame index
int m_currentIdx;
int m_increment;
// frame data
float* m_depthFrame;
BYTE* m_colorFrame;
Eigen::Matrix4f m_currentTrajectory;
// color camera info
Eigen::Matrix3f m_colorIntrinsics;
Eigen::Matrix4f m_colorExtrinsics;
unsigned int m_colorImageWidth;
unsigned int m_colorImageHeight;
// depth (ir) camera info
Eigen::Matrix3f m_depthIntrinsics;
Eigen::Matrix4f m_depthExtrinsics;
unsigned int m_depthImageWidth;
unsigned int m_depthImageHeight;
// base dir
std::string m_baseDir;
// filenamelist depth
std::vector<std::string> m_filenameDepthImages;
std::vector<double> m_depthImagesTimeStamps;
// filenamelist color
std::vector<std::string> m_filenameColorImages;
std::vector<double> m_colorImagesTimeStamps;
// trajectory
std::vector<Eigen::Matrix4f> m_trajectory;
std::vector<double> m_trajectoryTimeStamps;
};