-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhalo.cpp
319 lines (266 loc) · 6.4 KB
/
halo.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
/*!
* \file orf.cpp
* \author Gabriel Urbain <[email protected]> - Visiting student at MIT SSL
* \date July 2014
* \version 0.1
* \brief Sources for Halo class
*
* License: The MIT License (MIT)
* Copyright (c) 2014, Massachussets Institute of Technology
*/
#include "halo.h"
using namespace std;
Halo::Halo() :
isCamOpen(false), isOrfOpen(false),
imgWidth(640), imgHeight(480), imgNum(0),
fusion_first(true)
{}
Halo::~Halo()
{}
int Halo::init()
{
// We are capturing images
this->load_image = false;
int retVal;
retVal = orf.init();
if (retVal!=0) {
orf.close();
return -1;
}
isOrfOpen = true;
retVal = stereo.init();
if (retVal!=0) {
orf.close();
stereo.close();
return -1;
}
retVal = stereo.start();
if (retVal!=0) {
orf.close();
stereo.close();;
return -1;
}
isCamOpen = true;
return 0;
}
int Halo::init(string directory)
{
// We are using saved data
this->load_image = true;
// Verify the directory and set the name
struct stat st;;
if(stat(directory.c_str(),&st) != 0) {
ERROR<<"The directory to load images does not exist!"<<endl;
return -1;
}
this->load_directory = directory;
orf.init(directory);
stereo.init(directory);
// Load the timestamp
string tsOM = directory + "/timestampOpticsMount.txt";
string tsORF = directory + "/timestampORF.txt";
if(stat(tsOM.c_str(),&st) != 0) {
ERROR<<"The OpticsMount timestamp file does not exist !"<<endl;
return -1;
}
if(stat(tsORF.c_str(),&st) != 0) {
ERROR<<"The ORF timestamp file does not exist !"<<endl;
return -1;
}
string line;
this->omFile.open(tsOM.c_str());
this->orfFile.open(tsORF.c_str());
if (this->omFile.is_open()) {
for(int i=0; i<4; i++) {
getline(this->omFile,line);
}
} else {
ERROR<<"Unable to open OpticsMount timstamp file"<<endl;
return -1;
}
if (orfFile.is_open()) {
for(int i=0; i<4; i++) {
getline(this->orfFile,line);
}
} else {
ERROR<<"Unable to open timestamp file"<<endl;
return -1;
}
isCamOpen = true;
isOrfOpen = true;
this->load_num = 0;
return 0;
}
int Halo::close()
{
int retVal;
if (!load_image) {
if (isOrfOpen) {
retVal = orf.close();
}
if (isCamOpen){
retVal = stereo.close();
}
}
return 0;
}
int Halo::captureAllImages(Mat& iL, Mat& iR, Mat& dT, Mat& vT, Mat& cT, int flag)
{
int retVal;
int flags;
// Create TimeStamp
TimeStamp tsOM, tsORF, dummy;
// If needed, load timestamps
if (load_image) {
string line;
int retVal;
if (this->orfFile.is_open()) {
getline(this->orfFile,line);
if (line=="")
return -1;
tsORF.load(line);
}
if (this->omFile.is_open()) {
getline(this->omFile,line);
if (line=="")
return -1;
tsOM.load(line);
}
}
// If we load from file
if (load_image) {
// Load ORF images
retVal = orf.captureOrf(dT, vT, cT, dummy, this->load_num);
if (retVal!=0)
return -1;
// Load OpticsMount images
retVal = stereo.captureTwoImages(iL, iR, &stereo.rightImgNum, &stereo.leftImgNum, dummy, flags, this->load_num);
if (retVal!=0)
return -1;
this->load_num++;
// Else, if we capture images
} else {
// Capture non rectified ToF images
if (isOrfOpen) {
retVal = orf.captureOrf(dT, vT, cT, tsORF);
if (retVal!=0)
return -1;
}
// Capture frames from stereo cameras
if (isCamOpen){
retVal = stereo.captureTwoImages(iL, iR, &stereo.rightImgNum, &stereo.leftImgNum, tsOM, flags);
if (retVal!=0)
return -1;
}
imgNum++;
}
// Check synchronicity
if (flag==SYNCHRONOUS) {
int del = abs(tsORF.getMeanTime() - tsOM.getMeanTime());
if (!tsOM.isSynchro(tsORF)) {
DEBUG<<"Images "<<this->load_num<<" are not synchronous! Delay between stereo cams and orf cam is : "<<del<<" ms"<<endl;
this->load_num++;
return abs(del);
}
DEBUG<<"Images "<<this->load_num<<" are synchronous! Delay between stereo cams and orf cam is : "<<del<<" ms"<<endl;
}
return 0;
}
int Halo::captureAllRectifiedImages(Mat& iL, Mat& iR, Mat& dT, Mat& vT, Mat& cT, int flag)
{
int retVal;
int flags;
// Create TimeStamp
TimeStamp tsOM, tsORF, dummy;
// If needed, load timestamps
if (load_image) {
string line;
int retVal;
if (this->orfFile.is_open()) {
getline(this->orfFile,line);
if (line=="")
return -1;
tsORF.load(line);
}
if (this->omFile.is_open()) {
getline(this->omFile,line);
if (line=="")
return -1;
tsOM.load(line);
}
}
// If we load from file
if (load_image) {
// Load ORF images
retVal = orf.captureRectifiedOrf(dT, vT, cT, dummy, this->load_num);
if (retVal!=0)
return -1;
// Load OpticsMount images
retVal = stereo.captureTwoRectifiedImages(iL, iR, dummy, this->load_num);
if (retVal!=0)
return -1;
this->load_num++;
// Else, if we capture images
} else {
// Capture non rectified ToF images
if (isOrfOpen) {
retVal = orf.captureRectifiedOrf(dT, vT, cT, tsORF);
if (retVal!=0)
return -1;
}
// Capture frames from stereo cameras
if (isCamOpen){
retVal = stereo.captureTwoRectifiedImages(iL, iR, tsOM);
if (retVal!=0)
return -1;
}
imgNum++;
}
// Check synchronicity
if (flag==SYNCHRONOUS) {
int del = abs(tsORF.getMeanTime() - tsOM.getMeanTime());
if (!tsOM.isSynchro(tsORF)) {
DEBUG<<"Images "<<this->load_num<<" are not synchronous! Delay between stereo cams and orf cam is : "<<del<<" ms"<<endl;
this->load_num++;
return abs(del);
}
DEBUG<<"Images "<<this->load_num<<" are synchronous! Delay between stereo cams and orf cam is : "<<del<<" ms"<<endl;
}
return 0;
}
int Halo::saveAllImages()
{
if (load_image) {
ERROR<<"You cannot save images loaded from hard drive!"<<endl;
return -1;
}
this->stereo.saveTwoImages();
this->orf.saveOrf();
return 0;
}
int Halo::capture3Dcloud(vector<Point3d>& pointcloud, vector<Vec3b>& rgbcloud, string filename)
{
// Variables declaration
Mat dT, iT, cT, iR, iL;
TimeStamp ts;
// Init TimeStamp
ts.start();
// Capture images
captureAllRectifiedImages(iL, iR, dT, iT, cT);
// Perform fusion
if (fusion_first == true) {
t = new HaloTriangulator(stereo.projMatrixR, orf.intrinsicMatrix, stereo.intrinsicMatrixL, stereo.intrinsicMatrixR, filename);
pointcloud = t->fusion(dT, cT, iL, iR, rgbcloud, 0);
fusion_first = false;
} else {
pointcloud = t->fusion(dT, cT, iL, iR, rgbcloud, 0);
}
// Stop TimeStamp
ts.stop();
INFO<<"Image capture and processing time: "<<ts.getProcTime()<<"ms"<<endl;
return 0;
}
int Halo::calib(string filename)
{
haloCalib(filename);
}