forked from HTLife/png_to_klg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
344 lines (277 loc) · 9.08 KB
/
main.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
/*
* main.cpp
*
* Created on: 3 Nov 2016
* Author: Jacky Liu
*
*
* Convert .png sequence in TUM compatible dataset to .klg format
* (.klg is the log file format for Kintinuous and ElasticFusion)
*/
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <sstream>
#define NONE "\033[m"
#define RED "\033[0;32;31m"
extern char *optarg;
extern int optind;
/// PATH_PAIR = pair<depth image path, rgb image path>
typedef std::pair<std::string,std::string> PATH_PAIR;
/// SEQ = pair<timestamp, PATH_PAIR>
typedef std::pair<int64_t,PATH_PAIR> SEQ;
typedef std::vector<SEQ> VEC_INFO;
/// Working directory path
std::string strWorkingDir;
bool g_bFlag_reverse = false;
bool g_bFlag_TUM = false;
double g_dScale = 5000;//depth scale
/** @brief Convert png image files to .klg format
*
* @param vec_info vector of path <timestamp, <depth path, rgb path>>
* @param strKlgFileName output file name
* @return void
*/
void convertToKlg(
VEC_INFO &vec_info,
std::string &strKlgFileName)
{
std::cout << "klg_name:\n\t" << strKlgFileName << std::endl;
std::string filename = strKlgFileName;//"test2.klg";
FILE * logFile = fopen(filename.c_str(), "wb+");
int32_t numFrames = (int32_t)vec_info.size();
fwrite(&numFrames, sizeof(int32_t), 1, logFile);
//CvMat *encodedImage = 0;
VEC_INFO::iterator it = vec_info.begin();
int count = 1;
std::cout << "Progress:\n";
for(it; it != vec_info.end(); it++)
{
std::string strAbsPathDepth =
std::string(
getcwd(NULL, 0)) + "/" +
it->second.first;
cv::Mat depth = imread(strAbsPathDepth.c_str(), cv::IMREAD_UNCHANGED);
double depthScale = g_dScale;
depth.convertTo(depth, CV_16UC1, 1000 * 1.0 / depthScale);
int32_t depthSize = depth.total() * depth.elemSize();
std::string strAbsPath = std::string(
getcwd(NULL, 0)) + "/" +
it->second.second;
#if (CV_VERSION_MAJOR >= 4)
cv::Mat img = cv::imread(strAbsPath.c_str(), cv::IMREAD_UNCHANGED);
#else
IplImage *img =
cvLoadImage(strAbsPath.c_str(),
CV_LOAD_IMAGE_UNCHANGED);
#endif
#if (CV_VERSION_MAJOR >= 4)
if(img.empty())
#else
if(img == NULL)
#endif
{
fclose(logFile);
return;
}
#if (CV_VERSION_MAJOR >= 4)
int32_t imageSize = img.rows * img.cols * sizeof(unsigned char) * 3;
#else
int32_t imageSize = img->height * img->width * sizeof(unsigned char) * 3;
#endif
unsigned char * rgbData = 0;
#if (CV_VERSION_MAJOR >= 4)
rgbData = img.data; // Mat => unsigned char*
#else
rgbData = (unsigned char *)img->imageData;
#endif
std::cout << '\r'
<< std::setw(4) << std::setfill('0') << count << " / "
<< std::setw(4) << std::setfill('0') << vec_info.size()
<< std::flush;
count++;
/// Timestamp
fwrite(&it->first, sizeof(int64_t), 1, logFile);
/// DepthSize
fwrite(&depthSize, sizeof(int32_t), 1, logFile);
/// imageSize
fwrite(&imageSize, sizeof(int32_t), 1, logFile);
/// Depth buffer
fwrite((char*)depth.data, depthSize, 1, logFile);
/// RGB buffer
fwrite(rgbData, imageSize, 1, logFile);
#if (CV_VERSION_MAJOR >= 4)
#else
cvReleaseImage(&img);
#endif
depth.release();
}
std::cout << std::endl;
fclose(logFile);
}
/** @brief Parse associations.txt files to vector
*
* @param strAssociation_Path path of associations.txt
* @param vec_info vector of path <timestamp, <depth path, rgb path>>
* @return void
*/
int parseInfoFile(
std::string &strAssociation_Path,
VEC_INFO &vec_info)
{
char * line = NULL;
size_t len = 0;
ssize_t read;
FILE *pFile = fopen(strAssociation_Path.c_str(), "r");
if(!pFile) {
return -1;
}
int iFrameCnt = 0;
while ((read = getline(&line, &len, pFile)) != -1) {
std::istringstream is(line);
std::string part;
int iIdxToken = 0;
while (getline(is, part))
{
if('#' == part[0])/// Skip file comment '#"
{
continue;
}
int64_t timeSeq = 0;
std::string strDepthPath;
std::string strRgbPath;
std::istringstream iss(part);
std::string token;
while (getline(iss, token, ' '))
{
if(2 == iIdxToken) //Time rgb
{//first token which is time
}
else if(3 == iIdxToken)//rgb path
{
strRgbPath = token;
}
else if(0 == iIdxToken)//Time depth
{
/// Do nothing
//std::cout << token << std::endl;
token.erase(
std::remove(token.begin(),
token.end(), '.'), token.end());
//std::cout << token << std::endl;
long long unsigned int numb;
std::istringstream ( token ) >> numb;
if(true == g_bFlag_TUM)
{
timeSeq = (int64_t)numb;
//timeSeq = iFrameCnt;
iFrameCnt++;
}
else
{
timeSeq = numb * 1000000;
}
}
else if(1 == iIdxToken)//depth path
{
strDepthPath = token;
}
iIdxToken++;
}
PATH_PAIR path_pair;
if(g_bFlag_reverse)
path_pair = PATH_PAIR(strRgbPath, strDepthPath);
else
path_pair = PATH_PAIR(strDepthPath, strRgbPath);
SEQ seq(timeSeq, path_pair);
vec_info.push_back(seq);
}
}
fclose(pFile);
return 0;
}
int main(int argc, char* argv[])
{
int option_count = 0;
std::string strAssociation_Path;
std::string strKlgFileName;
int c = 0;
while((c = getopt(argc, argv, "worts")) != -1)
{
switch(c)
{
case 'w'://Dataset directory root path
option_count++;
strWorkingDir = std::string(argv[optind]);
break;
case 'o'://klg filename
option_count++;
strKlgFileName = std::string(argv[optind]);
break;
case 'r'://associations.txt is in reverse order (rgb)(depth)
option_count++;
g_bFlag_reverse = true;
break;
case 't'://TUM format
option_count++;
g_bFlag_TUM = true;
break;
case 's'://TUM format
option_count++;
sscanf(argv[optind], "%lf", &g_dScale);
break;
default:
break;
}
}
if(option_count < 2)
{
fprintf(stderr,
"Usage: ./pngtoklg -w (working directory) -o (klg file name)\n"
"\n"
"-w working directory\n"
"-o output klg filename\n"
"-t TUM format\n"
"-s Scale factor in floating point ex. '5000'\n"
"For more scale factor detail, please reference: \nhttp://vision.in.tum.de/data/datasets/rgbd-dataset/file_formats#intrinsic_camera_calibration_of_the_kinect\n"
"Example: ./pngtoklg -w ../livingroom_kt0_rs -o liv.klg\n"
"associations.txt should be in (depth_time)(depth_file)(rgb_time)(rgb_file) order\n");
return -1;
}
/// Change working directory
int ret = chdir(strWorkingDir.c_str());
if(ret != 0)
{
fprintf(stderr, RED "dataset path not exist" NONE);
}
printf("\nCurrent working directory:\n\t%s\n", getcwd(NULL, 0));
strAssociation_Path = strWorkingDir;
if(strWorkingDir[strWorkingDir.length() - 1] != '/')
{
strAssociation_Path += '/';
}
strAssociation_Path += "associations.txt";
/// Parse files
// (timestamp, (depth path, rgb path) )
VEC_INFO vec_info;
int err = parseInfoFile(
strAssociation_Path,
vec_info);
if(err != 0)
{
fprintf(stderr,
RED "Fail to find associations.txt under working directory!\n" NONE);
return -1;
}
std::cout << "Depth: " << vec_info.back().second.first << std::endl;
std::cout << "RGB: " << vec_info.back().second.second << std::endl;
std::cout << "scale: " << g_dScale << std::endl;
convertToKlg(vec_info, strKlgFileName);
std::cout << "Conversion complete!\n";
return 0;
}