-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhaloTriangulator.cpp
674 lines (573 loc) · 23.9 KB
/
haloTriangulator.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*!
* \file haloTriangulator.cpp
* \author Gabriel Urbain <[email protected]> - Visiting student at MIT SSL
* \date S 2014
* \version 0.1
* \brief Triangulation and fusion from stereo and orf images
*
* License: The MIT License (MIT)
* Copyright (c) 2014, Massachussets Institute of Technology
*/
#include "haloTriangulator.h"
using namespace cv;
using namespace std;
HaloTriangulator::HaloTriangulator(string filenameORF, string filenameOM, string filenameHALO)
{
if (MLT.empty()) {
FileStorage storage;
int retVal = storage.open(filenameHALO, FileStorage::READ);
if (retVal == 1) {
INFO<<"HALO Calibration file found! No need to perform calibration!"<<endl;
} else {
INFO<<"HALO Calibration file not found! Calibration needed!"<<endl;
exit(1);
}
storage["MLT"]>>MLT;
storage["MTL"]>>MTL;
storage["MRT"]>>MRT;
storage.release();
}
if (intrinsicL.empty()) {
FileStorage storage;
int retVal = storage.open(filenameOM, FileStorage::READ);
if (retVal != 1) {
INFO<<"STEREO Calibration file not found! Calibration needed!"<<endl;
exit(1);
}
storage["intrinsicL"]>>intrinsicL;
storage["intrinsicR"]>>intrinsicR;
storage["projMatrixR"]>>projMatrixStereo;
storage.release();
}
if (intrinsicT.empty()) {
FileStorage storage;
int retVal = storage.open(filenameORF, FileStorage::READ);
if (retVal != 1) {
INFO<<"HALO Calibration file not found! Calibration needed!"<<endl;
exit(1);
}
storage["intrinsicT"]>>intrinsicT;
storage.release();
}
this->computeInterParams();
//stereo = new StereoTriangulator(intrinsicL, intrinsicR);
orf = new OrfTriangulator(intrinsicT);
}
HaloTriangulator::HaloTriangulator(Mat& projMatrixR_, Mat& intrinsicT_, Mat& intrinsicL_, Mat& intrinsicR_, string filenameHALO)
{
if (MLT.empty()) {
FileStorage storage;
int retVal = storage.open(filenameHALO, FileStorage::READ);
if (retVal == 1) {
INFO<<"HALO Calibration file found! No need to perform calibration!"<<endl;
} else {
INFO<<"HALO Calibration file not found! Calibration needed!"<<endl;
exit(1);
}
storage["MLT"]>>MLT;
storage["MRT"]>>MRT;
storage["MTL"]>>MTL;
storage.release();
}
Mat R, T;
FileStorage storage;
int retVal = storage.open("OM_calib.xml", FileStorage::READ);
if (retVal != 1) {
INFO<<"STEREO Calibration file not found! Calibration needed!"<<endl;
exit(1);
}
storage["R"]>>R;
storage["T"]>>T;
storage.release();
Mat rotMatrixL = Mat::zeros(3,4, CV_64F);
rotMatrixL.at<double>(0,0) = 1;
rotMatrixL.at<double>(1,1) = 1;
rotMatrixL.at<double>(2,2) = 1;
Mat rotMatrixR = Mat::zeros(3,4, CV_64F);
rotMatrixR.at<double>(0,0) = R.at<double>(0,0);
rotMatrixR.at<double>(0,1) = R.at<double>(0,1);
rotMatrixR.at<double>(0,2) = R.at<double>(0,2);
rotMatrixR.at<double>(0,3) = T.at<double>(0);
rotMatrixR.at<double>(1,0) = R.at<double>(1,0);
rotMatrixR.at<double>(1,1) = R.at<double>(1,1);
rotMatrixR.at<double>(1,2) = R.at<double>(1,2);
rotMatrixR.at<double>(1,3) = T.at<double>(1);
rotMatrixR.at<double>(2,0) = R.at<double>(2,0);
rotMatrixR.at<double>(2,1) = R.at<double>(2,1);
rotMatrixR.at<double>(2,2) = R.at<double>(2,2);
rotMatrixR.at<double>(2,3) = T.at<double>(2);
intrinsicT = intrinsicT_;
intrinsicL = intrinsicL_;
intrinsicR = intrinsicR_;
projMatrixStereo = projMatrixR_;
this->computeInterParams();
stereo = new StereoTriangulator(projMatrixStereo);
stereo2 = new StereoTriangulator2(rotMatrixL, rotMatrixR, intrinsicL, intrinsicR);
orf = new OrfTriangulator(intrinsicT);
}
HaloTriangulator::HaloTriangulator(Mat& intrinsicT_, Mat& intrinsicL_, Mat& intrinsicR_, Mat& MLT_, Mat& MTL_, Mat& MRT_)
{
intrinsicT = intrinsicT_;
intrinsicL = intrinsicL_;
intrinsicR = intrinsicR_;
MLT = MLT_;
MTL = MTL_;
MRT = MRT_;
this->computeInterParams();
//stereo = new StereoTriangulator(intrinsicL, intrinsicR);
orf = new OrfTriangulator(intrinsicT);
}
vector<Point3d> HaloTriangulator::fusionDebug(Mat& dT, Mat& cT, Mat& iL, Mat& iR, vector<Vec3b>& rgbcloud, int flag)
{
// TODO Correct downsampling
resize(dT, dT, Size((int)dT.cols/ORF_CLOUD_DOWNSAMPLING, (int)dT.rows/ORF_CLOUD_DOWNSAMPLING));
resize(cT, cT, Size((int)cT.cols/ORF_CLOUD_DOWNSAMPLING, (int)cT.rows/ORF_CLOUD_DOWNSAMPLING));
#ifdef FUSION_DEBUG
// DEBUG parameters
Mat dF = Mat::zeros(dT.size(), dT.type());
Mat meanILm = Mat::zeros(dT.size(), CV_8U);
Mat meanIRm = Mat::zeros(dT.size(), CV_8U);
Mat sigma_w_m = Mat::zeros(cT.size(), cT.type());
Mat sigma_t_m = Mat::zeros(cT.size(), cT.type());
Mat sigma_s_m = Mat::zeros(cT.size(), cT.type());
Mat cim = Mat::zeros(dT.size(), CV_8U);
int num = 0;
Mat iR2 = iR.clone();
Mat iL2 = iL.clone();
double sigma_s_max = 0;
double sigma_t_max = 0;
double sigma_w_max = 0;
vector<KeyPoint> keyL, keyR;
vector<double> dist;
Mat newPointStereoL, newPointStereoT;
Point3d newPointStereoL2;
#endif
// Probability parameters
double sigma_i= estimateNoiseVariance(iL, iR);
double meanD, sigma_t, sigma_s, sigma_w;
int meanIL, meanIR;
vector<double> ci;
double prob_stereo_sum;
vector<double> prob_orf, prob_stereo_temp, prob_stereo, prob_joint;
vector<double> depth_saved;
// Algorithm parameters
vector<Point3d> pointcloud, pointcloudf;
vector<Vec3b> rgbcloudf;
double d, di, step, m;
int sum, sum1, sum2;
Point3d newPointT, newPointFus;
Mat newPointLi, newPointTi, newPointRi;
Point2d camCoordL, camCoordR;
// For each points of the image
for (int i=0; i<dT.rows; i++) {
for (int j=0; j<dT.cols; j++) {
// -- STEP 0 : Compute point coordinates --
d = ((dT.at<unsigned short>(i, j)>>2) & 0x3FFF)*0.00061;
newPointT = orf->triangulateOrf(j*ORF_CLOUD_DOWNSAMPLING, i*ORF_CLOUD_DOWNSAMPLING, d);
// -- STEP 1: if the point is framed by the three cameras --
if ( newPointT.z > (b *a1)/(a1 + a2) && (newPointT.z > 0.4 && newPointT.z < 3)) {
if ( -(newPointT.z - b)/a2 < newPointT.x){
if ( abs(newPointT.y) < newPointT.z/max(a3, a4)) {
//if (newPointT.x>0.05 && newPointT.x<0.45 && newPointT.y>0.05 && newPointT.y<0.20) {
// -- STEP 2: Calculate variances --
// 2.1 Compute sigma_t as a function of confidency
sigma_t = (((65535 - cT.at<unsigned short>(i,j))>>2) & 0x3FFF)*0.00061/10;
// 2.2 Compute sigma_s as a function of neighborhood
if ( i > VAR_KER && j > VAR_KER && i < (dT.rows - VAR_KER) && j < (dT.cols - VAR_KER) ) {
// 2.2.1 Compute map of mean values with kernel
sum = 0.0;
for(int k=-VAR_KER; k<VAR_KER+1; k++) {
for(int l=-VAR_KER; l<VAR_KER+1; l++) {
sum = sum + dT.at<unsigned short>(i+k,j+l);
}
}
meanD = sum/((2*VAR_KER+1)*(2*VAR_KER+1));
// 2.2.2 Compute map of variance values with kernel
sum = 0.0;
for(int k=-VAR_KER; k<VAR_KER+1; k++) {
for(int l=-VAR_KER; l<VAR_KER+1; l++) {
sum = sum + pow((dT.at<unsigned short>(i+k,j+l) - meanD), 2);
//cout<<j<<". Sum: "<<sum<<" and dist: "<<(dT.at<unsigned short>(i+k,j+l) - meanD)<<endl;
}
}
sigma_s = (((unsigned short)sqrt(sum/((2*VAR_KER+1)*(2*VAR_KER+1)))>>2) & 0x3FFF)*0.00061/4;
}
// 2.3 Compute sigma_w as the max of sigma_s and sigma_t
sigma_w = max(sigma_t, sigma_s) / 4;
#ifdef FUSION_DEBUG
//DEBUG<<"Point ("<<i<<","<<j<<"): \tSigma_t: "<<sigma_t<<" \tSigma_s: "<<sigma_s<<"\t\tSigma_w: "<<sigma_w<<endl;
sigma_s_max = max(sigma_s_max, sigma_s);
sigma_t_max = max(sigma_t_max, sigma_t);
sigma_w_max = max(sigma_w_max, sigma_w);
sigma_t_m.at<unsigned short>(i, j) = ((unsigned short)(sigma_t/0.00061*2))<<2;
sigma_s_m.at<unsigned short>(i, j) = ((unsigned short)(sigma_s/0.00061*2))<<2;
sigma_w_m.at<unsigned short>(i, j) = max(sigma_t_m.at<unsigned short>(i, j), sigma_s_m.at<unsigned short>(i, j));
#endif
// -- STEP 3: Create a discrete interval --
// 3.1 Initialize
prob_orf.clear();
prob_stereo_temp.clear();
prob_stereo.clear();
prob_joint.clear();
ci.clear();
keyL.clear();
keyR.clear();
dist.clear();
depth_saved.clear();
m = 0;
// 3.2 Create the interval and begin a loop
step= pow(d, 2) / (baseline * OM_FOCUS) * OM_PREC / K_INT;
di = d;
bool di_inc = true;
do {
// -- STEP 4: Compute ORF probability --
prob_orf.push_back(exp(- pow((di - d), 2)/(2*pow(sigma_w, 2))) / sqrt(2 * PI * pow(sigma_w, 2)));
// -- STEP 5: Recover Stereo coordinates --
// 5.1 Get values in T coordinate system
newPointTi = orf->triangulateOrfMat(j*ORF_CLOUD_DOWNSAMPLING, i*ORF_CLOUD_DOWNSAMPLING, di);
// 5.2 Move from T to L
newPointLi = MLT * newPointTi;
// 5.3 Project into uv coordinates
stereo->reprojectStereo(camCoordL, camCoordR, newPointLi.at<double>(0)*1000, newPointLi.at<double>(1)*1000, newPointLi.at<double>(2)*1000);
// newPointLi.at<double>(0,0) = newPointLi.at<double>(0,0) * 1000;
// newPointLi.at<double>(0,1) = newPointLi.at<double>(0,1) * 1000;
// newPointLi.at<double>(0,2) = newPointLi.at<double>(0,2) * 1000;
// stereo2->reprojectStereoMat(camCoordL, camCoordR, newPointLi);
#ifdef FUSION_DEBUG
// 5.4 Reproject all stereo points in L coordinates
newPointStereoL = stereo2->triangulateStereoLinear(camCoordL, camCoordR);
// 5.5 Move from L to T
newPointStereoL.at<double>(0,0) = newPointStereoL.at<double>(0,0) / 1000;
newPointStereoL.at<double>(0,1) = newPointStereoL.at<double>(0,1) / 1000;
newPointStereoL.at<double>(0,2) = newPointStereoL.at<double>(0,2) / 1000;
newPointStereoT = MTL * newPointStereoL;
// circle(iL2, Point(camCoordL.x, camCoordL.y), 2, (255,255,255));
// circle(iR2, Point(camCoordR.x, camCoordR.y), 2, (255,255,255));
// imshow("aa", iL2);
// imshow("bb", iR2);
// cvWaitKey(0);
// 5.6 Add in point cloud for comparaison
// pointcloud.push_back(Point3d(newPointStereoT.at<double>(0,0), newPointStereoT.at<double>(0,1), newPointStereoT.at<double>(0,2)));
// rgbcloud.push_back(Vec3b(0, 255, 0));
// cout<<"(u,v) in L: ("<<camCoordL.x<<", "<<camCoordL.y<<")\t(u,v) in R: ("<<camCoordR.x<<", "<<camCoordR.y<<")"<<endl;
// cout<<newPointStereoT<<" and "<<newPointTi<<" and "<<norm(newPointStereoT-newPointTi)<<endl;
#endif
// -- STEP 6: Compute stereo probability --
//if ( camCoordL.x > TAD_KER && camCoordL.y > TAD_KER && camCoordL.x < (iL.rows - TAD_KER) && camCoordL.y < (iL.cols - TAD_KER) ) {
// 6.1 Compute sum of absolute differences
double sum3 = 0.0;
for(int k=-TAD_KER; k<TAD_KER+1; k++) {
for(int l=-TAD_KER; l<TAD_KER+1; l++) {
sum3 = sum3 + abs((double)iL.at<uchar>(camCoordL.y+k,camCoordL.x+l)/255.0 - (double)iR.at<uchar>(camCoordL.y+k,camCoordL.x+l)/255.0);
}
}
// 6.2 Threshold comparaison
ci.push_back(min((int)sum3, 25));///(2*TAD_KER), 0.5));
//}
// 6.3 Compute stereo probability
prob_stereo.push_back(exp(- ci[m] / sigma_i));;
// -- STEP 7: Compute joint probability --
prob_joint.push_back(prob_stereo[m]*prob_orf[m]);
#ifdef FUSION_DEBUG
// cout<<"Prob ORF: "<<prob_orf[m]<<" \tAnd Prob Stereo: "<<prob_stereo[m]<<"\tAnd Prob Joint: "<<prob_joint[m]<<endl;
#endif
m++;
depth_saved.push_back(di);
if (di_inc == true) {
di = di + step;
if (di > d + 3 * sigma_w) {
di = d;
di_inc = false;
}
} else {
di = di - step;
}
} while (di_inc == true || di > d - 3 * sigma_w);
// -- STEP 8: Select depth which maximize probability --
vector<double>::iterator prob_max = max_element(prob_joint.begin(), prob_joint.end());
int prob_index = distance(prob_joint.begin(), prob_max);
double prob_depth = depth_saved[prob_index];
#ifdef FUSION_DEBUG
// DEBUG<<"INTERVAL: "<<6*sigma_w<<"\tSTEP: "<<step<<endl;
// vector<double>::iterator prob_orf_max = max_element(prob_orf.begin(), prob_orf.end());
// int prob_orf_index = distance(prob_orf.begin(), prob_orf_max);
// vector<double>::iterator prob_stereo_max = max_element(prob_stereo.begin(), prob_stereo.end());
// int prob_stereo_index = distance(prob_stereo.begin(), prob_stereo_max);
// DEBUG<<"Point ("<<i<<","<<j<<"): \tMax joint index: "<<prob_index<<" \tMax orf index: "<<prob_orf_index<<"\tMax stereo index: "<<prob_stereo_index<<endl;
// DEBUG<<"Point ("<<i<<","<<j<<"): \tOriginal ORF depth"<<d<<"m \t\tNew depth: "<<prob_depth<<"m"<<endl;
// pointcloud.push_back(newPointT);
// rgbcloud.push_back(Vec3b(255,0,0));
#endif
// -- STEP 9: Transform to orthonormal coordinate system --
newPointFus = orf->triangulateOrf(j*ORF_CLOUD_DOWNSAMPLING, i*ORF_CLOUD_DOWNSAMPLING, prob_depth);
dF.at<unsigned short>(i,j) = ((unsigned short)(prob_depth / 0.00061))<<2;
pointcloud.push_back(newPointFus);
rgbcloud.push_back(Vec3b(0,0,255));
}
// else {
// pointcloud.push_back(newPointT);
// rgbcloud.push_back(Vec3b(0,0,255));
// }
}
// else {
// pointcloud.push_back(newPointT);
// rgbcloud.push_back(Vec3b(0,0,255));
// }
}
// else {
// pointcloud.push_back(newPointT);
// rgbcloud.push_back(Vec3b(0,0,255));
// }
}
}
#ifdef FUSION_DEBUG
// DEBUG<<"Sigmaw max: "<<sigma_w_max<<"\tSigma_t max: "<<sigma_t_max<<"\tSigma_s max: "<<sigma_s_max<<endl;
// resize(sigma_s_m, sigma_s_m, Size((int)sigma_s_m.cols*ORF_CLOUD_DOWNSAMPLING, (int)sigma_s_m.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("sigma_s_m", sigma_s_m);
// cvWaitKey(0);
// Display point cloud
// Mat R = Mat::zeros(3, 3, CV_64F);
// R.at<double>(0,0) = 1;
// R.at<double>(1,1) = 1;
// R.at<double>(2,2) = 1;
// visualizerShowCamera(R, Vec3f(0,0,0), 255,0,0,0.02,"ORF camera");
// visualizerShowCamera(R, Vec3f(-0.25, 0, 0), 0,255,0,0.02,"Stereo camera");
// RunVisualization(pointcloudf, rgbcloudf);
// visualizerShowCamera(R, Vec3f(0,0,0), 255,0,0,0.02,"ORF camera");
// visualizerShowCamera(R, Vec3f(-0.25, 0, 0), 0,255,0,0.02,"Stereo camera");
// RunVisualization(pointcloud, rgbcloud);
#endif
return pointcloud;
}
vector<Point3d> HaloTriangulator::fusion(Mat& dT, Mat& cT, Mat& iL, Mat& iR, vector<Vec3b>& rgbcloud, int flag)
{
// TODO Correct downsampling
resize(dT, dT, Size((int)dT.cols/ORF_CLOUD_DOWNSAMPLING, (int)dT.rows/ORF_CLOUD_DOWNSAMPLING));
resize(cT, cT, Size((int)cT.cols/ORF_CLOUD_DOWNSAMPLING, (int)cT.rows/ORF_CLOUD_DOWNSAMPLING));
// Probability parameters
double sigma_i= estimateNoiseVariance(iL, iR);
double meanD, sigma_t, sigma_s, sigma_w;
int meanIL, meanIR;
vector<double> ci;
double prob_stereo_sum;
vector<double> prob_orf, prob_stereo_temp, prob_stereo, prob_joint;
vector<double> depth_saved;
// Algorithm parameters
vector<Point3d> pointcloud, pointcloudf;
vector<Vec3b> rgbcloudf;
double d, di, step, m;
int sum, sum1, sum2;
Point3d newPointT, newPointFus;
Mat newPointLi, newPointTi, newPointRi;
Point2d camCoordL, camCoordR;
// For each points of the image
for (int i=0; i<dT.rows; i++) {
for (int j=0; j<dT.cols; j++) {
// -- STEP 0 : Compute point coordinates --
d = ((dT.at<unsigned short>(i, j)>>2) & 0x3FFF)*0.00061;
newPointT = orf->triangulateOrf(j*ORF_CLOUD_DOWNSAMPLING, i*ORF_CLOUD_DOWNSAMPLING, d);
// -- STEP 1: if the point is framed by the three cameras --
if ( newPointT.z > (b *a1)/(a1 + a2) && (newPointT.z > 0.4 && newPointT.z < 3)) {
if ( -(newPointT.z - b)/a2 < newPointT.x){
if ( abs(newPointT.y) < newPointT.z/max(a3, a4)) {
// -- STEP 2: Calculate variances --
// 2.1 Compute sigma_t as a function of confidency
sigma_t = (((65535 - cT.at<unsigned short>(i,j))>>2) & 0x3FFF)*0.00061/10;
// 2.2 Compute sigma_s as a function of neighborhood
if ( i > VAR_KER && j > VAR_KER && i < (dT.rows - VAR_KER) && j < (dT.cols - VAR_KER) ) {
// 2.2.1 Compute map of mean values with kernel
sum = 0.0;
for(int k=-VAR_KER; k<VAR_KER+1; k++) {
for(int l=-VAR_KER; l<VAR_KER+1; l++) {
sum = sum + dT.at<unsigned short>(i+k,j+l);
}
}
meanD = sum/((2*VAR_KER+1)*(2*VAR_KER+1));
// 2.2.2 Compute map of variance values with kernel
sum = 0.0;
for(int k=-VAR_KER; k<VAR_KER+1; k++) {
for(int l=-VAR_KER; l<VAR_KER+1; l++) {
sum = sum + pow((dT.at<unsigned short>(i+k,j+l) - meanD), 2);
//cout<<j<<". Sum: "<<sum<<" and dist: "<<(dT.at<unsigned short>(i+k,j+l) - meanD)<<endl;
}
}
sigma_s = (((unsigned short)sqrt(sum/((2*VAR_KER+1)*(2*VAR_KER+1)))>>2) & 0x3FFF)*0.00061/4;
}
// 2.3 Compute sigma_w as the max of sigma_s and sigma_t
sigma_w = max(sigma_t, sigma_s) / 4;
// -- STEP 3: Create a discrete interval --
// 3.1 Initialize
prob_orf.clear();
prob_stereo_temp.clear();
prob_stereo.clear();
prob_joint.clear();
ci.clear();
depth_saved.clear();
m = 0;
// 3.2 Create the interval and begin a loop
step= pow(d, 2) / (baseline * OM_FOCUS) * OM_PREC / K_INT;
di = d;
bool di_inc = true;
do {
// -- STEP 4: Compute ORF probability --
prob_orf.push_back(exp(- pow((di - d), 2)/(2*pow(sigma_w, 2))) / sqrt(2 * PI * pow(sigma_w, 2)));
// -- STEP 5: Recover Stereo coordinates --
// 5.1 Get values in T coordinate system
newPointTi = orf->triangulateOrfMat(j*ORF_CLOUD_DOWNSAMPLING, i*ORF_CLOUD_DOWNSAMPLING, di);
// 5.2 Move from T to L
newPointLi = MLT * newPointTi;
// 5.3 Project into uv coordinates
stereo->reprojectStereo(camCoordL, camCoordR, newPointLi.at<double>(0)*1000, newPointLi.at<double>(1)*1000, newPointLi.at<double>(2)*1000);
// -- STEP 6: Compute stereo probability --
// 6.1 Compute sum of absolute differences
double sum3 = 0.0;
for(int k=-TAD_KER; k<TAD_KER+1; k++) {
for(int l=-TAD_KER; l<TAD_KER+1; l++) {
sum3 = sum3 + abs((double)iL.at<uchar>(camCoordL.y+k,camCoordL.x+l)/255.0 - (double)iR.at<uchar>(camCoordL.y+k,camCoordL.x+l)/255.0);
}
}
// 6.2 Threshold comparaison
ci.push_back(min((int)sum3, 25));
// 6.3 Compute stereo probability
prob_stereo.push_back(exp(- ci[m] / sigma_i));;
// -- STEP 7: Compute joint probability --
prob_joint.push_back(prob_stereo[m]*prob_orf[m]);
m++;
depth_saved.push_back(di);
if (di_inc == true) {
di = di + step;
if (di > d + 3 * sigma_w) {
di = d;
di_inc = false;
}
} else {
di = di - step;
}
} while (di_inc == true || di > d - 3 * sigma_w);
// -- STEP 8: Select depth which maximize probability --
vector<double>::iterator prob_max = max_element(prob_joint.begin(), prob_joint.end());
int prob_index = distance(prob_joint.begin(), prob_max);
double prob_depth = depth_saved[prob_index];
// -- STEP 9: Transform to orthonormal coordinate system --
newPointFus = orf->triangulateOrf(j*ORF_CLOUD_DOWNSAMPLING, i*ORF_CLOUD_DOWNSAMPLING, prob_depth);
pointcloud.push_back(newPointFus);
rgbcloud.push_back(Vec3b(0,0,255));
}
}
}
}
}
return pointcloud;
}
void HaloTriangulator::computeInterParams()
{
baseline = 0.08;//-projMatrixStereo.at<double>(0,3) / 1000;
a1 = tan(PI/2 - PI*ORF_FOV_H/360);
a2 = tan(PI/2 - PI*CAM_FOV_H/360);
a3 = tan(PI/2 - PI*ORF_FOV_V/360);
a4 = tan(PI/2 - PI*CAM_FOV_V/360);
b = -MRT.at<double>(0,3) * a2;
}
double HaloTriangulator::estimateNoiseVariance(Mat& iL, Mat& iR)
{
int widthL = iL.cols;
int heightL = iL.rows;
int widthR = iR.cols;
int heightR = iR.rows;
Mat iLdst, iRdst;
double sigmaL, sigmaR;
Point anchor = Point( -1, -1 );
double delta = 0;
int ddepth = -1;
Mat kernel = Mat::zeros(3, 3, CV_8S);
kernel.at<char>(0,0) = 1;
kernel.at<char>(0,1) = -2;
kernel.at<char>(0,2) = 1;
kernel.at<char>(1,0) = -2;
kernel.at<char>(1,1) = 4;
kernel.at<char>(1,2) = -2;
kernel.at<char>(2,0) = 1;
kernel.at<char>(2,1) = -2;
kernel.at<char>(2,2) = 1;
filter2D(iL, iLdst, ddepth , kernel, anchor, delta, BORDER_DEFAULT);
sigmaL = sum(abs(iLdst))[0];
sigmaL = sigmaL * sqrt(0.5 * PI) / (6 * (widthL-2) * (heightL-2));
filter2D(iR, iRdst, ddepth , kernel, anchor, delta, BORDER_DEFAULT);
sigmaR = sum(abs(iRdst))[0];
sigmaR = sigmaR * sqrt(0.5 * PI) / (6 * (widthR-2) * (heightR-2));
return max(sigmaL, sigmaR);
}
// #ifdef FUSION_DEBUG
// Add keypoints
// keyL.push_back(KeyPoint(camCoordL.x, camCoordL.y, 15));
// keyR.push_back(KeyPoint(camCoordR.x, camCoordR.y, 15));
// // computing descriptors
// SurfDescriptorExtractor extractor;
// Mat descriptorsL, descriptorsR;
// extractor.compute(iL, keyL, descriptorsL);
// extractor.compute(iR, keyR, descriptorsR);
//
// // matching descriptors
// BruteForceMatcher<L2<float> > matcher;
// vector<DMatch> matches;
// cout<<matches[1]<<endl;
// matcher.match(descriptorsL, descriptorsR, matches);
// cout<<matches.size()<<endl;
//
//
// // Display
// DEBUG<<"DISTANCES: [";
// for( int v = 0; v < descriptorsL.rows; v++ ) {
// dist.push_back((double)matches[v].distance);
// cout<<dist[v]<<" ";
// }
// cout<<"]"<<endl;
// DEBUG<<"PROB: [";
// for( int v = 0; v < m+1; v++ ) {
// cout<<prob_stereo_temp[m]<<" ";
// }
// cout<<"]"<<endl;
// Mat img_matches;
// drawMatches(iL2, keyL, iR2, keyR, matches, img_matches);
// imshow("matches", img_matches);
// waitKey(0);
// #endif
// if (newPointT.x>0.05 && newPointT.x<0.45 && newPointT.y>0.05 && newPointT.y<0.20) {
// circle(iL2, Point(camCoordL.x, camCoordL.y), 10, (255,255,255));
// circle(iR2, Point(camCoordR.x, camCoordR.y), 10, (255,255,255));
// imshow("aa", iL2);
// imshow("bb", iR2);
// cvWaitKey(0);
// }
//
// resize(iR2, iR2, Size((int)iR2.cols*2, (int)iR2.rows*2));
// imshow("aa", iR2);
// cvWaitKey(0);
// resize(sigma_t_m, sigma_t_m, Size((int)sigma_t_m.cols*ORF_CLOUD_DOWNSAMPLING, (int)sigma_t_m.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("sigma_t_m", sigma_t_m);
// resize(sigma_w_m, sigma_w_m, Size((int)sigma_w_m.cols*ORF_CLOUD_DOWNSAMPLING, (int)sigma_w_m.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("sigma_w_m", sigma_w_m);
//
// resize(cT, cT, Size((int)cT.cols*ORF_CLOUD_DOWNSAMPLING, (int)cT.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("Confidency", cT);
// resize(cim, cim, Size((int)cim.cols*ORF_CLOUD_DOWNSAMPLING, (int)cim.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("cim ", cim);
// resize(meanILm, meanILm, Size((int)meanILm.cols*ORF_CLOUD_DOWNSAMPLING, (int)meanILm.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("IL mean ", meanILm);
// resize(meanIRm, meanIRm, Size((int)meanIRm.cols*ORF_CLOUD_DOWNSAMPLING, (int)meanIRm.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("IR mean ", meanIRm);
// resize(dT, dT, Size((int)dT.cols*ORF_CLOUD_DOWNSAMPLING, (int)dT.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("Prev depth ", dT);
// resize(dF, dF, Size((int)dF.cols*ORF_CLOUD_DOWNSAMPLING, (int)dF.rows*ORF_CLOUD_DOWNSAMPLING));
// imshow("New depth ", dF);
// cvWaitKey(0);
//
// DEBUG<<"Prob ORF: [";
// copy(prob_orf.begin(), prob_orf.end(),ostream_iterator<double>(cout, "; "));
// cout<<"]\nAnd Prob Stereo: [";
// copy(prob_stereo.begin(), prob_stereo.end(),ostream_iterator<double>(cout, "; "));
// cout<<"]\nAnd Prob Joint: [";
// copy(prob_joint.begin(), prob_joint.end(),ostream_iterator<double>(cout, "; "));
// cout<<"]\nAnd CI: [";
// copy(ci.begin(), ci.end(),ostream_iterator<double>(cout, "; "));
// cout<<"]"<<endl;