/
FileCamera.cpp
319 lines (255 loc) · 7.28 KB
/
FileCamera.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
/*
* FileCamera.cpp
*
* Created on: 18/09/2015
* Author: andrew
*/
#include <stddef.h> /* size_t */
#include <ctype.h>
#include "FileCamera.h"
///////////////////////////////////////////////////////////////////////////////////////////
/*
* nat_sort comparison adapted to C++ from http://sourcefrog.net/projects/
*/
/* These are defined as macros to make it easier to adapt this code to
* different characters types or comparison functions. */
static inline int
nat_isdigit(char a)
{
return isdigit((unsigned char) a);
}
static inline int
nat_isspace(char a)
{
return isspace((unsigned char) a);
}
static inline char
nat_toupper(char a)
{
return toupper((unsigned char) a);
}
static int
compare_right(char const *a, char const *b)
{
int bias = 0;
/* The longest run of digits wins. That aside, the greatest
value wins, but we can't know that it will until we've scanned
both numbers to know that they have the same magnitude, so we
remember it in BIAS. */
for (;; a++, b++) {
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return bias;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b) {
if (!bias)
bias = -1;
} else if (*a > *b) {
if (!bias)
bias = +1;
} else if (!*a && !*b)
return bias;
}
return 0;
}
static int
compare_left(char const *a, char const *b)
{
/* Compare two left-aligned numbers: the first to have a
different value wins. */
for (;; a++, b++) {
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return 0;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b)
return -1;
if (*a > *b)
return +1;
}
return 0;
}
static int
strnatcmp0(char const *a, char const *b, int fold_case)
{
int ai, bi;
char ca, cb;
int fractional, result;
ai = bi = 0;
while (1) {
ca = a[ai]; cb = b[bi];
/* skip over leading spaces or zeros */
while (nat_isspace(ca))
ca = a[++ai];
while (nat_isspace(cb))
cb = b[++bi];
/* process run of digits */
if (nat_isdigit(ca) && nat_isdigit(cb)) {
fractional = (ca == '0' || cb == '0');
if (fractional) {
if ((result = compare_left(a+ai, b+bi)) != 0)
return result;
} else {
if ((result = compare_right(a+ai, b+bi)) != 0)
return result;
}
}
if (!ca && !cb) {
/* The strings compare the same. Perhaps the caller
will want to call strcmp to break the tie. */
return 0;
}
if (fold_case) {
ca = nat_toupper(ca);
cb = nat_toupper(cb);
}
if (ca < cb)
return -1;
if (ca > cb)
return +1;
++ai; ++bi;
}
}
inline int
strnatcmp(char const *a, char const *b) {
return strnatcmp0(a, b, 0);
}
/* Compare, recognizing numeric string and ignoring case. */
inline int
strnatcasecmp(char const *a, char const *b) {
return strnatcmp0(a, b, 1);
}
inline bool strnatcmp_s(const string &a, const string &b) {
return strnatcmp0(a.c_str(), b.c_str(), 0)<0;
}
/* Compare, recognizing numeric string and ignoring case. */
inline bool strnatcasecmp_s(const string &a, const string &b) {
return strnatcmp0(a.c_str(), b.c_str(), 1)<0;
}
///////////////////////////////////////////////////////////////////////////////////////////
bool FileCamera::SetReadDirectory(string dir)
{
bool color_found = true;
bool depth_found = true;
rgb_frames.clear();
depth_frames.clear();
this->dir = dir;
this->RgbDIR = dir + "/rgb";
this->colorDir = opendir (RgbDIR.c_str());
if (colorDir == NULL) {
cout << "Cannot open directory: " << dir << endl;
color_found = false;
}
else {
struct dirent *colorDirent;
while((colorDirent = readdir(colorDir))!=NULL) {
if(strcmp(colorDirent->d_name, ".") && strcmp(colorDirent->d_name, "..")) {
rgb_frames.push_back(string(colorDirent->d_name));
cout << "COLOR FILE: " << colorDirent->d_name << endl;
}
}
sort(rgb_frames.begin(), rgb_frames.end(), strnatcmp_s);
}
closedir(this->colorDir);
this->DepthDIR = dir + "/depth";
this->depthDir = opendir (DepthDIR.c_str());
if (depthDir == NULL) {
cout << "Cannot open directory: " << dir << endl;
depth_found = false;
}
else {
struct dirent *depthDirent;
while((depthDirent = readdir(depthDir))!=NULL) {
if(strcmp(depthDirent->d_name, ".") && strcmp(depthDirent->d_name, "..")) {
depth_frames.push_back(string(depthDirent->d_name));
cout << "DEPTH FILE: " << depthDirent->d_name << endl;
}
}
sort(depth_frames.begin(), depth_frames.end(), strnatcmp_s);
}
closedir(this->depthDir);
frame_idx = 0;
cout << "FILE CAMERA" << endl;
cout << "RGB IMAGES: " << rgb_frames.size() << endl;
cout << "DEPTH IMAGES: " << depth_frames.size() << endl;
return depth_found;
}
bool FileCamera::get_framedata(Rgb<byte>* &color_data, unsigned short* &depth_data)
{
try {
if(frame_idx < rgb_frames.size()){
cout << "Loading DATA" << endl;
string rgb_filename = this->RgbDIR + "/" + rgb_frames[frame_idx];
Image<Rgb<byte> > color = img_load(rgb_filename);
memcpy(color_data, color.data(), color.size().x*color.size().y*sizeof(Rgb<byte>));
cout << "Loaded Color Data" << endl;
}
}
catch(CVD::Exceptions::Image_IO::OpenError err) {
cerr << "Error in loaded color: " << err.what << endl;
}
catch(CVD::Exceptions::Image_IO::EofBeforeImage err) {
cerr << "Error in loading depth: " << err.what << endl;
}
try {
if(frame_idx < depth_frames.size()) {
string depth_filename = this->DepthDIR + "/" + depth_frames[frame_idx];
Image<unsigned short> depth = img_load(depth_filename);
memcpy(depth_data, depth.data(), depth.size().x*depth.size().y*sizeof(unsigned short));
cout << "Loaded Depth Data" << endl;
}
else return false;
}
catch(CVD::Exceptions::Image_IO::OpenError err) {
cerr << "Error in loading depth: " << err.what << endl;
}
catch(CVD::Exceptions::Image_IO::EofBeforeImage err) {
cerr << "Error in loading depth: " << err.what << endl;
}
// frame_idx++;
return true;
}
bool FileCamera::get_frame(Image<Rgb<byte> >& color_image, Image<unsigned short>& depth_image)
{
try {
if(frame_idx < rgb_frames.size()){
string rgb_filename = this->RgbDIR + "/" + rgb_frames[frame_idx];
img_load(color_image, rgb_filename);
}
}
catch(CVD::Exceptions::Image_IO::OpenError err) {
cerr << "Error in loaded color: " << err.what << endl;
}
catch(CVD::Exceptions::Image_IO::EofBeforeImage err) {
cerr << "Error in loading depth: " << err.what << endl;
}
if(frame_idx < depth_frames.size()) {
string depth_filename = this->DepthDIR + "/" + depth_frames[frame_idx];
try {
img_load(depth_image, depth_filename);
}
catch(CVD::Exceptions::Image_IO::OpenError err) {
cerr << "Error in loading depth: " << err.what << " " << depth_filename << endl;
}
catch(CVD::Exceptions::Image_IO::EofBeforeImage err) {
cerr << "Error in loading depth: " << err.what << " " << depth_filename << endl;
}
}
else return false;
// frame_idx++;
return true;
}
FileCamera::FileCamera(string dir) {
// TODO Auto-generated constructor stub
this->frame_idx = 0;
this->SetReadDirectory(dir);
}
FileCamera::~FileCamera() {
// TODO Auto-generated destructor stub
rgb_frames.~vector();
depth_frames.~vector();
}