-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoreader.hpp
412 lines (325 loc) · 9.15 KB
/
photoreader.hpp
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <string>
#include <vector>
#include <opencv2/core/persistence.hpp>
#include "exif.h"
#include "exif.cpp"
using namespace cv;
double exif_read(std::string img_path,bool print=false)
{
double seconds;
double h,m,s,ms;
// Read the JPEG file into a buffer
FILE *fp = fopen(img_path.c_str(), "rb");
if (!fp)
{
printf("Can't open file.\n");
}
fseek(fp, 0, SEEK_END);
unsigned long fsize = ftell(fp);
rewind(fp);
unsigned char *buf = new unsigned char[fsize];
if (fread(buf, 1, fsize, fp) != fsize)
{
printf("Can't read file.\n");
delete[] buf;
}
fclose(fp);
// Parse EXIF
easyexif::EXIFInfo result;
int code = result.parseFrom(buf, fsize);
delete[] buf;
if (code)
{
printf("Error parsing EXIF: code %d\n", code);
return 0.0;
}
h = double(atoi(result.DateTimeOriginal.substr(11,2).c_str()));
m = double(atoi(result.DateTimeOriginal.substr(14,2).c_str()));
s = double(atoi(result.DateTimeOriginal.substr(17,2).c_str()));
ms = double(atoi(result.SubSecTimeOriginal.c_str()));
seconds = h*3600 + m*60 + s + ms/100;
if (print)
{
cout<<result.DateTimeOriginal<<" "<<result.SubSecTimeOriginal<<" "<<seconds<<endl;
}
return seconds;
}
string preffix(unsigned int number,int nZeros)
{
int maxNumb = 10;
string res = "";
for(unsigned int i = 1; i<nZeros; i++)
{
maxNumb *= 10;
}
if (maxNumb < number)
{
return "";
}
int cont = maxNumb;
for(;;)
{
if (cont > number)
{
res+="0";
cont/= 10;
}
else
{
break;
}
}
//cout<<maxNumb;
return res;
}
//void imagelist(string path,vector<String>* output,bool outputxt = false)
//{
//
// vector<String> fn;
// cv::glob(path,fn,false);
//
// ofstream file;
// if(outputxt)
// {
// file.open("list.txt");
// }
//
// for (int i = 0; i < fn.size(); i++)
// {
//
// if(outputxt)
// {
// file << fn.at(i)<<endl;
// }
//
// }
//
// *output = fn;
//}
struct photoReader
{
//path for the original images
string originalPath,calibPath,outPath;
string LfolderName,RfolderName,Ext;
bool saveImgs;
//the calibration data
//camera matrices and distortion arrays
cv::Mat McamL,McamR,VdistL,VdistR;
//vectors to store the names of the original images
vector<String> origNamesL,origNamesR;
//vectors to store the names of the undistorted images
vector<string> outNamesL,outNamesR;
//to store the EXIF timestamps
vector<double> exifTimestampsL,exifTimestampsR,exifTimeDiffs;
photoReader(string origPathName,string ext,string lFname,string rFname,string outFname,string calibDataPath,bool writeImgs=true);
void readingCalibdata(bool print=false);
void imagelist2(bool print = false);
void outputImgNames(bool print=false);
void rectifyImages();
};
void photoReader::readingCalibdata(bool print)
{
FileStorage fs(calibPath, FileStorage::READ);
fs["M1"] >> McamL;
fs["D1"] >> VdistL;
fs["M2"] >> McamR;
fs["D2"] >> VdistR;
//the scaling of the matrices should be removed before exporting the intrinsics
//TO BE REMOVED:
//McamL *=4;McamR*=4;
///debugging
if (print)
{
cout<<endl<<endl<<McamL<<endl<<endl<<VdistL<<endl<<endl<<McamR<<endl<<endl<<VdistR<<endl<<endl<<endl;
}
fs.release();
}
void photoReader::imagelist2(bool print)
{
string lsep1="";
string rsep1="";
string sep2="";
if(originalPath.back() != '/')
{
lsep1+="/";
rsep1+="/";
}
if(LfolderName.back() != '/' || RfolderName.back() != '/' )
{
sep2+="/";
}
if(Ext.find(".")==-1)
{
sep2+=".";
if(Ext.find("*")==-1)
{
sep2+="*";
}
}
//LEFT CAMERA
string toReadL = originalPath+lsep1+LfolderName+sep2+Ext;
//RIGHT CAMERA
string toReadR = originalPath+rsep1+RfolderName+sep2+Ext;
if(toReadL.find("*")==-1 || toReadR.find("*")==-1)
{
cout<<"O programa nao sera capaz de ler as imagens, confira os argumentos passados"<<endl;
}
if (print)
{
cout<<toReadL<<endl<<toReadR<<endl;
}
cv::glob(toReadL,origNamesL,false);
cv::glob(toReadR,origNamesR,false);
//cout<<endl<<origNamesL.size();
}
void photoReader::outputImgNames(bool print)
{
cout<<origNamesL.size()<<" "<<origNamesR.size()<<endl;
if (origNamesL.size() == origNamesR.size())
{
if (print)
{
for (unsigned int i = 0; i<origNamesL.size(); i++)
{
cout<<origNamesL.at(i)<<endl<<origNamesR.at(i)<<endl<<endl;
}
}
}
else
{
cerr<<"há mais imagens de uma câmera que da outra, conferir";
}
}
void photoReader::rectifyImages()
{
//creating the two directories
string left = outPath+"/esquerda";
string right = outPath+"/direita";
string outname;
string cmdL = "mkdir "+left;
string cmdR = "mkdir "+right;
// #linux_specific
system(cmdL.c_str());
system(cmdR.c_str());
cv::Mat orig,mod;
//reading, rectifying, saving and storing the full path
//LEFT
//The "fakes" are a workaround for some photos that aren't taken
for (unsigned int i = 0; i<origNamesL.size(); i++)
{
string fakepart = "";
bool fakefound = false;
//read
if(saveImgs){
orig = cv::imread(origNamesL.at(i),IMREAD_COLOR);}
// cout<<origNamesL.at(i).find("fake")<<endl;
// cout<<origNamesR.at(i)<<endl;
if (origNamesL.at(i).find("fake") < origNamesL.at(i).size() || origNamesL.at(i).find("fake") < 0)
{
// cout<<origNamesL.at(i).find("fake")<<endl;
fakepart = "_fake";
fakefound = true;
}
//compensate for lens distortion
if(saveImgs){
undistort(orig,mod,McamL,VdistL);}
//the outname
outname = left+"/left"+preffix(i+1,5)+to_string(i+1)+fakepart+".jpg";
outNamesL.push_back(outname);
if(saveImgs){
cout<<"undistorting and saving "<<outname<<endl;}
// else
// {cout<<"reading "<<outname<<endl;}
//saving the image
if(saveImgs){
imwrite(outname,mod);
orig.release();}
outname.clear();
mod.release();
//the EXIF infos
if (!fakefound)
{
double tempSeconds = exif_read(origNamesL.at(i));
exifTimestampsL.push_back(tempSeconds);
}
}
cout<<endl;
//RIGHT
for (unsigned int i = 0; i<origNamesR.size(); i++)
{
string fakepart = "";
bool fakefound = false;
//read
if(saveImgs){
orig = cv::imread(origNamesR.at(i),IMREAD_COLOR);}
if (origNamesR.at(i).find("fake") < origNamesR.at(i).size() || origNamesR.at(i).find("fake") < 0)
{
fakepart = "_fake";
fakefound = true;
}
//compensate for lens distortion
if(saveImgs){
undistort(orig,mod,McamR,VdistR);}
// else
// {cout<<"reading "<<outname<<endl;}
//the outname
outname = right+"/right"+preffix(i+1,5)+to_string(i+1)+fakepart+".jpg";
outNamesR.push_back(outname);
if(saveImgs){
cout<<"undistorting and saving "<<outname<<endl;}
//saving the image
if(saveImgs){
imwrite(outname,mod);
orig.release();}
outname.clear();
mod.release();
//the EXIF infos
if (!fakefound)
{
double tempSeconds = exif_read(origNamesR.at(i));
exifTimestampsR.push_back(tempSeconds);
}
}
}
photoReader::photoReader(string origPathName,string ext,string lFname,string rFname,string outFname,string calibDataPath,bool writeImgs)
{
///CONSTRUCTOR
originalPath = origPathName;
calibPath = calibDataPath;
LfolderName = lFname;
RfolderName = rFname;
Ext = ext;
outPath = outFname;
saveImgs = writeImgs;
//to read calibration data
readingCalibdata();
//to create the lists of images
imagelist2();
//a little test
outputImgNames();
//making the directory for the
string command01 = "mkdir "+outPath;
// #linux_specific
system(command01.c_str());
rectifyImages();
/// Timekeeping functions to implement
////////// for (unsigned int i = 0;i<exifTimestampsL.size();i++)
////////// {
////////// int dif1 = fabs(exifTimestampsL.at(i)-exifTimestampsR.at(i));
//////////
////////// if (dif1 > 0.1)
////////// {
////////// cout<<"diferença na timestamp do EXIF"<<endl;
////////// }
//////////
////////// if (i > 0)
////////// {
////////// double dif21 = fabs(exifTimestampsL.at(i) - exifTimestampsL.at(i-1));
////////// exifTimeDiffs.push_back(dif21);
////////// }
////////// }
// cout<<exifTimeDiffs.size()<<" "<<exifTimestampsL.size()<<endl;
}