-
Notifications
You must be signed in to change notification settings - Fork 6
/
semantic_stixels.cpp
1362 lines (1128 loc) · 38.3 KB
/
semantic_stixels.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "semantic_stixels.h"
#include "draw.h"
#define _USE_MATH_DEFINES
#include <math.h>
#define USE_OPENMP
#if defined(_OPENMP) && defined(USE_OPENMP)
#ifdef _WIN32
#define OMP_PARALLEL_FOR __pragma(omp parallel for schedule(dynamic))
#else
#define OMP_PARALLEL_FOR _Pragma("omp parallel for schedule(dynamic)")
#endif
#else
#define OMP_PARALLEL_FOR
#endif
////////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////////
// maximum cost
static constexpr float Cinf = std::numeric_limits<float>::max();
// model complexity prior
static constexpr float Cmc = 10;
// mathematical constants
static constexpr float PI = static_cast<float>(M_PI);
static constexpr float SQRT2 = static_cast<float>(M_SQRT2);
static constexpr float SQRTPI = static_cast<float>(2. / M_2_SQRTPI);
// geometric class id
static constexpr int G = GEOMETRIC_ID_GROUND;
static constexpr int O = GEOMETRIC_ID_OBJECT;
static constexpr int S = GEOMETRIC_ID_SKY;
static constexpr int GEO_ID_BIT = 2;
static constexpr int NO_GEOMETRY = 3;
// structural priors
static constexpr float alphaGapP = 1;
static constexpr float betaGapP = 0;
static constexpr float alphaGapN = 1;
static constexpr float betaGapN = 0;
static constexpr float alphaGravP = 1;
static constexpr float betaGravP = 1;
static constexpr float alphaGravN = 1;
static constexpr float betaGravN = 1;
static constexpr float alphaOrd = 1;
static constexpr float betaOrd = 1;
// disparity measurement uncertainty
static constexpr float sigmaD[3] =
{
1.f,
1.f,
2.f
};
static constexpr float sigmaDSq[3] =
{
sigmaD[G] * sigmaD[G],
sigmaD[O] * sigmaD[O],
sigmaD[S] * sigmaD[S]
};
static constexpr float invSigmaDSq[3] =
{
1.f / sigmaDSq[G],
1.f / sigmaDSq[O],
1.f / sigmaDSq[S]
};
// range of depth into witch objects are allowed to extend
static constexpr float deltaZ = 0.3f;
// camera height and tilt uncertainty
constexpr float sigmaH = 0.05f;
constexpr float sigmaA = 0.005f;
constexpr float sigmaHSq = sigmaH * sigmaH;
constexpr float sigmaASq = sigmaA * sigmaA;
// outlier rate
static constexpr float pOutG = 0.15f;
static constexpr float pOutO = 0.15f;
static constexpr float pOutS = 0.4f;
// probability of invalid disparity
static constexpr float pInvD = 0.25f;
static constexpr float pInvG = 0.34f;
static constexpr float pInvO = 0.3f;
static constexpr float pInvS = 0.36f;
static constexpr float pC = 1.f / 3;
// semantic cost weight
static constexpr float wsem = 0.5f;
static constexpr float squared(float x)
{
return x * x;
}
template <typename T>
static constexpr float floatCast(T x)
{
return static_cast<float>(x);
}
////////////////////////////////////////////////////////////////////////////////////
// Type definitions
////////////////////////////////////////////////////////////////////////////////////
struct Line
{
Line(float a = 0, float b = 0) : a(a), b(b) {}
Line(const cv::Vec2f& vec) : a(vec[0]), b(vec[1]) {}
Line(const cv::Point2f& pt1, const cv::Point2f& pt2)
{
a = (pt2.y - pt1.y) / (pt2.x - pt1.x);
b = -a * pt1.x + pt1.y;
}
inline float operator()(int x) const { return a * x + b; }
float vhor() const { return -b / a; }
float a, b;
};
////////////////////////////////////////////////////////////////////////////////////
// Cost functions
////////////////////////////////////////////////////////////////////////////////////
using Parameters = SemanticStixels::Parameters;
struct NegativeLogDataTermGrd
{
NegativeLogDataTermGrd(int dmin, int dmax, const CameraParameters& camera, int vmax, const Line& road)
{
init(dmin, dmax, camera, vmax, road);
}
inline float operator()(float d, int v) const
{
if (d < 0.f)
return nLogPInvD;
// [Experimental] this error saturation suppresses misdetection like "object below ground"
const float error = std::max(d - fn_[v], 0.f);
const float nLogPGaussian = cb_[v] + ca_[v] * squared(error);
const float nLogPData = std::min(nLogPUniform, nLogPGaussian);
return nLogPData + nLogPValD;
}
// pre-compute constant terms
void init(int dmin, int dmax, const CameraParameters& camera, int vmax, const Line& road)
{
const float bf = camera.baseline * camera.fu;
const float invHcam = 1.f / camera.height;
// uniform distribution term
nLogPUniform = logf(floatCast(dmax - dmin)) - logf(pOutG);
// probability of invalid and valid disparity
const float pInv = pInvG * pInvD / pC;
nLogPInvD = -logf(pInv);
nLogPValD = -logf(1.f - pInv);
// gaussian distribution term
cb_.resize(vmax);
ca_.resize(vmax);
// expected disparity
fn_.resize(vmax);
for (int v = 0; v < vmax; v++)
{
const float fn = std::max(road(v), 0.f);
const float sigmaRSq = squared(invHcam * fn) * sigmaHSq + squared(invHcam * bf) * sigmaASq;
const float sigmaGSq = sigmaDSq[G] + sigmaRSq;
const float sigma = sqrtf(sigmaGSq);
const float tau = SQRT2 * sigma;
const float itau = 1.f / tau;
// normalize function
const float Z = 0.5f * (erff(itau * (dmax - fn)) - erff(itau * (dmin - fn)));
// gaussian distribution term
ca_[v] = squared(itau);
cb_[v] = logf(Z) + logf(tau * SQRTPI) - logf(1.f - pOutG);
// expected disparity
fn_[v] = fn;
}
}
float nLogPUniform, nLogPInvD, nLogPValD;
std::vector<float> cb_, ca_, fn_;
};
struct NegativeLogDataTermObj
{
NegativeLogDataTermObj(int dmin, int dmax, const CameraParameters& camera)
{
init(dmin, dmax, camera);
}
inline float operator()(float d, int fn) const
{
if (d < 0.f)
return nLogPInvD;
const float error = d - fn;
const float nLogPGaussian = cb_[fn] + ca_[fn] * squared(error);
const float nLogPData = std::min(nLogPUniform, nLogPGaussian);
return nLogPData + nLogPValD;
}
// pre-compute constant terms
void init(int dmin, int dmax, const CameraParameters& camera)
{
const int fnmax = dmax;
const float bf = camera.baseline * camera.fu;
const float invDeltaD = deltaZ / bf;
// uniform distribution term
nLogPUniform = logf(floatCast(dmax - dmin)) - logf(pOutO);
// probability of invalid and valid disparity
const float pInv = pInvO * pInvD / pC;
nLogPInvD = -logf(pInv);
nLogPValD = -logf(1.f - pInv);
// gaussian distribution term
cb_.resize(fnmax);
ca_.resize(fnmax);
for (int fn = 0; fn < fnmax; fn++)
{
const float sigmaZSq = squared(invDeltaD * fn * fn);
const float sigmaOSq = sigmaDSq[O] + sigmaZSq;
const float sigma = sqrtf(sigmaOSq);
const float tau = SQRT2 * sigma;
const float itau = 1.f / tau;
// normalize function
const float Z = 0.5f * (erff(itau * (dmax - fn)) - erff(itau * (dmin - fn)));
// gaussian distribution term
ca_[fn] = squared(itau);
cb_[fn] = logf(Z) + logf(tau * SQRTPI) - logf(1.f - pOutO);
}
}
float nLogPUniform, nLogPInvD, nLogPValD;
std::vector<float> cb_, ca_;
};
struct NegativeLogDataTermSky
{
NegativeLogDataTermSky(int dmin, int dmax, const CameraParameters& camera)
{
init(dmin, dmax, camera);
}
inline float operator()(float d) const
{
if (d < 0.f)
return nLogPInvD;
const float error = d;
const float nLogPGaussian = cb_ + ca_ * squared(error);
const float nLogPData = std::min(nLogPUniform, nLogPGaussian);
return nLogPData + nLogPValD;
}
// pre-compute constant terms
void init(int dmin, int dmax, const CameraParameters& camera)
{
// uniform distribution term
nLogPUniform = logf(floatCast(dmax - dmin)) - logf(pOutS);
// probability of invalid and valid disparity
const float pInv = pInvS * pInvD / pC;
nLogPInvD = -logf(pInv);
nLogPValD = -logf(1.f - pInv);
const float tau = SQRT2 * sigmaD[S];
const float itau = 1.f / tau;
// normalize function
const float fn = 0;
const float Z = 0.5f * (erff(itau * (dmax - fn)) - erff(itau * (dmin - fn)));
// gaussian distribution term
ca_ = squared(itau);
cb_ = logf(Z) + logf(tau * SQRTPI) - logf(1.f - pOutS);
}
float nLogPUniform, nLogPInvD, nLogPValD;
float cb_, ca_;
};
////////////////////////////////////////////////////////////////////////////////////
// Static functions
////////////////////////////////////////////////////////////////////////////////////
static cv::Mat1f getch(const cv::Mat1f& src, int id)
{
return cv::Mat1f(src.size[1], src.size[2], (float*)src.ptr<float>(id));
}
static void create3d(cv::Mat1f& mat, int size0, int size1, int size2)
{
const int sizes[3] = { size0, size1, size2 };
mat.create(3, sizes);
}
static float calcSum(const cv::Mat1f& src, int srcu, int srcv, int w, int h)
{
float sum = 0;
for (int dv = 0; dv < h; dv++)
for (int du = 0; du < w; du++)
sum += src(srcv + dv, srcu + du);;
return sum;
}
static float calcMean(const cv::Mat1f& src, int srcu, int srcv, int w, int h, int threshold)
{
float sum = 0;
int cnt = 0;
for (int dv = 0; dv < h; dv++)
{
for (int du = 0; du < w; du++)
{
const float d = src(srcv + dv, srcu + du);
if (d >= 0)
{
sum += d;
cnt++;
}
}
}
return cnt >= threshold ? sum / cnt : -1;
}
static void reduceTranspose(const cv::Mat1f& src, cv::Mat1f& dst, int stixelW, int stixelH,
bool hasInvalidValue = false)
{
const int umax = src.cols / stixelW;
const int vmax = src.rows / stixelH;
dst.create(umax, vmax);
if (hasInvalidValue)
{
const int threshold = stixelW * stixelW / 2;
for (int dstv = 0, srcv = 0; dstv < vmax; dstv++, srcv += stixelH)
for (int dstu = 0, srcu = 0; dstu < umax; dstu++, srcu += stixelW)
dst(dstu, dstv) = calcMean(src, srcu, srcv, stixelW, stixelH, threshold);
}
else
{
const float invArea = 1.f / (stixelW * stixelH);
for (int dstv = 0, srcv = 0; dstv < vmax; dstv++, srcv += stixelH)
for (int dstu = 0, srcu = 0; dstu < umax; dstu++, srcu += stixelW)
dst(dstu, dstv) = invArea * calcSum(src, srcu, srcv, stixelW, stixelH);
}
}
static void reduceTranspose(const cv::Mat1f& src, cv::Mat1f& dst, int ch, int stixelW, int stixelH,
bool hasInvalidValue = false)
{
const cv::Mat1f _src = getch(src, ch);
cv::Mat1f _dst = getch(dst, ch);
reduceTranspose(_src, _dst, stixelW, stixelH, hasInvalidValue);
}
// estimate road model from camera tilt and height
static Line calcRoadModelCamera(const CameraParameters& camera)
{
const float sinTilt = sinf(camera.tilt);
const float cosTilt = cosf(camera.tilt);
const float a = (camera.baseline / camera.height) * cosTilt;
const float b = (camera.baseline / camera.height) * (camera.fu * sinTilt - camera.v0 * cosTilt);
return Line(a, b);
}
// estimate road model from v-disparity
static Line calcRoadModelVD(const cv::Mat1f& disparity, const CameraParameters& camera, int stixelH,
int samplingStep = 2, int minDisparity = 10, int maxIterations = 32, float inlierRadius = 1, float maxCameraHeight = 5)
{
const int w = disparity.rows;
const int h = disparity.cols;
// sample v-disparity points
std::vector<cv::Point2f> points;
points.reserve(h * w);
for (int u = 0; u < w; u += samplingStep)
for (int v = 0; v < h; v += samplingStep)
if (disparity(u, v) >= minDisparity)
points.push_back(cv::Point2f(floatCast(stixelH * v), disparity(u, v)));
if (points.empty())
return Line(0, 0);
// estimate line by RANSAC
cv::RNG random;
Line bestLine;
int maxInliers = 0;
for (int iter = 0; iter < maxIterations; iter++)
{
// sample 2 points and get line parameters
const cv::Point2f& pt1 = points[random.next() % points.size()];
const cv::Point2f& pt2 = points[random.next() % points.size()];
if (pt1.x == pt2.x)
continue;
const Line line(pt1, pt2);
// estimate camera tilt and height
const float tilt = atanf((line.a * camera.v0 + line.b) / (camera.fu * line.a));
const float height = camera.baseline * cosf(tilt) / line.a;
// skip if not within valid range
if (height <= 0.f || height > maxCameraHeight)
continue;
// count inliers within a radius and update the best line
int inliers = 0;
for (const auto& pt : points)
if (fabsf(line.a * pt.x + line.b - pt.y) <= inlierRadius)
inliers++;
if (inliers > maxInliers)
{
maxInliers = inliers;
bestLine = line;
}
}
// apply least squares fitting using inliers around the best line
double sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0;
int n = 0;
for (const auto& pt : points)
{
const float x = pt.x;
const float y = pt.y;
const float yhat = bestLine.a * x + bestLine.b;
if (fabsf(yhat - y) <= inlierRadius)
{
sx += x;
sy += y;
sxx += x * x;
syy += y * y;
sxy += x * y;
n++;
}
}
const float a = static_cast<float>((n * sxy - sx * sy) / (n * sxx - sx * sx));
const float b = static_cast<float>((sxx * sy - sxy * sx) / (n * sxx - sx * sx));
return Line(a, b);
}
static cv::Vec2d calcCostScale(const cv::Mat1f& predict)
{
const int chns = predict.size[0];
std::vector<double> minvs(chns);
std::vector<double> maxvs(chns);
OMP_PARALLEL_FOR
for (int ch = 0; ch < chns; ch++)
cv::minMaxIdx(getch(predict, ch), &minvs[ch], &maxvs[ch]);
const double minv = *std::min_element(std::begin(minvs), std::end(minvs));
const double maxv = *std::max_element(std::begin(maxvs), std::end(maxvs));
const double a = -255. / (maxv - minv);
const double b = -a * maxv;
return cv::Vec2d(a, b);
}
static void calcSAT(const cv::Mat1f& src, cv::Mat1f& dst, int ch, const cv::Vec2d& scale)
{
const cv::Mat1f channel = getch(src, ch);
const int umax = src.size[1];
const int vmax = src.size[2];
const float a = floatCast(scale[0]);
const float b = floatCast(scale[1]);
for (int u = 0; u < umax; u++)
{
const float* ptrSrc = channel.ptr<float>(u);
float* ptrDst = dst.ptr<float>(u, ch);
float tmpSum = 0.f;
for (int v = 0; v < vmax; v++)
{
tmpSum += a * ptrSrc[v] + b;
ptrDst[v] = tmpSum;
}
}
}
static inline float priorCostGG(float dGrdB, float dGrdT)
{
const float delta = dGrdB - dGrdT;
if (delta > 0)
return alphaGapP + betaGapP * delta;
if (delta < 0)
return alphaGapN - betaGapN * delta;
return 0.f;
}
static inline float priorCostGO(float dGrdB, float dObjT)
{
const float delta = dGrdB - dObjT;
if (delta > 0)
return alphaGravP + betaGravP * delta;
if (delta < 0)
return alphaGravN - betaGravN * delta;
return 0.f;
}
static inline float priorCostOG(float dObjB, float dGrdT)
{
const float delta = dObjB - dGrdT;
if (delta < 0)
return Cinf;
return 0.f;
}
static inline float priorCostOO(float dObjB, float dObjT)
{
const float delta = dObjT - dObjB;
if (delta > 0)
return alphaOrd + betaOrd * delta;
return 0.f;
}
static inline short packIndex(int geoId, int v)
{
return (v << GEO_ID_BIT) | geoId;
}
static inline cv::Point unpackIndex(short packed)
{
return { packed & ((1 << GEO_ID_BIT) - 1), packed >> GEO_ID_BIT };
}
struct BestCost
{
inline void init(const cv::Vec3f& _costs, float _dispO)
{
costs = _costs;
points[G] = packIndex(G, 0);
points[O] = packIndex(O, 0);
points[S] = packIndex(S, 0);
dispO = _dispO;
}
inline void init(const cv::Vec3f& _costs, const cv::Vec3b& _labels, float _dispO)
{
costs = _costs;
labels = _labels;
points[G] = packIndex(G, 0);
points[O] = packIndex(O, 0);
points[S] = packIndex(S, 0);
dispO = _dispO;
}
template <int C1, int C2>
inline void update(int vT, float cost)
{
if (cost < costs[C1])
{
costs[C1] = cost;
points[C1] = packIndex(C2, vT - 1);
}
}
template <int C1, int C2>
inline void update(int vT, float cost, float disp)
{
static_assert(C1 == O, "C1 must be class Obj");
if (cost < costs[C1])
{
costs[C1] = cost;
points[C1] = packIndex(C2, vT - 1);
dispO = disp;
}
}
template <int C1, int C2>
inline void update(int vT, float cost, int label)
{
if (cost < costs[C1])
{
costs[C1] = cost;
points[C1] = packIndex(C2, vT - 1);
labels[C1] = label;
}
}
template <int C1, int C2>
inline void update(int vT, float cost, float disp, int label)
{
static_assert(C1 == O, "C1 must be class Obj");
if (cost < costs[C1])
{
costs[C1] = cost;
points[C1] = packIndex(C2, vT - 1);
labels[C1] = label;
dispO = disp;
}
}
cv::Vec3f costs;
cv::Vec3s points;
cv::Vec3b labels;
float dispO;
};
using DataTermG = NegativeLogDataTermGrd;
using DataTermO = NegativeLogDataTermObj;
using DataTermS = NegativeLogDataTermSky;
struct DispCostLUT
{
DispCostLUT(const DataTermG& dataTermG, const DataTermO& dataTermO, const DataTermS& dataTermS,
int vmax, int dmax) : dataTermG(dataTermG), dataTermO(dataTermO), dataTermS(dataTermS), dmax(dmax)
{
costG.create(1, vmax);
costO.create(dmax, vmax);
costS.create(1, vmax);
sumD.create(1, vmax);
cntD.create(1, vmax);
tmpSumG = 0.f;
tmpSumS = 0.f;
tmpSumO.assign(dmax, 0.f);
tmpSumD = 0.f;
tmpCntD = 0;
}
inline void add(int v, float d)
{
// pre-computation for ground costs
tmpSumG += dataTermG(d, v);
costG(v) = tmpSumG;
// pre-computation for sky costs
tmpSumS += dataTermS(d);
costS(v) = tmpSumS;
// pre-computation for object costs
for (int fn = 0; fn < dmax; fn++)
{
tmpSumO[fn] += dataTermO(d, fn);
costO(fn, v) = tmpSumO[fn];
}
// pre-computation for mean disparity of stixel
if (d >= 0.f)
{
tmpSumD += d;
tmpCntD++;
}
sumD(v) = tmpSumD;
cntD(v) = tmpCntD;
}
inline float sumG(int vB) const
{
return costG(vB);
}
inline float sumG(int vB, int vT) const
{
return costG(vB) - costG(vT - 1);
}
inline float sumO(int fn, int vB) const
{
return costO(fn, vB);
}
inline float sumO(int fn, int vB, int vT) const
{
return costO(fn, vB) - costO(fn, vT - 1);
}
inline float sumS(int vB) const
{
return costS(vB);
}
inline float sumS(int vB, int vT) const
{
return costS(vB) - costS(vT - 1);
}
inline float meanD(int vB) const
{
return sumD(vB) / std::max(cntD(vB), 1);
}
inline float meanD(int vB, int vT) const
{
return (sumD(vB) - sumD(vT - 1)) / std::max((cntD(vB) - cntD(vT - 1)), 1);
}
const DataTermG& dataTermG;
const DataTermO& dataTermO;
const DataTermS& dataTermS;
cv::Mat1f costG, costO, costS, sumD;
cv::Mat1i cntD;
float tmpSumG, tmpSumS, tmpSumD;
int tmpCntD, dmax;
std::vector<float> tmpSumO;
};
static void processOneColumn(int u, int vmax, int dmax, const cv::Mat1f& disparity, const Line& road,
const DataTermG& dataTermG, const DataTermO& dataTermO, const DataTermS& dataTermS,
cv::Mat3f& costTable, cv::Mat3s& indexTable, cv::Mat1f& dispTable)
{
const int vhor = static_cast<int>(road.vhor());
// compute data cost LUT
DispCostLUT LUT(dataTermG, dataTermO, dataTermS, vmax, dmax);
const float* disparityU = disparity.ptr<float>(u);
for (int v = 0; v < vmax; v++)
LUT.add(v, disparityU[v]);
////////////////////////////////////////////////////////////////////////////////////////////
// compute cost tables
//
// for paformance optimization, loop is split at vhor and unnecessary computation is ommited
////////////////////////////////////////////////////////////////////////////////////////////
cv::Vec3f* costTableU = costTable.ptr<cv::Vec3f>(u);
cv::Vec3s* indexTableU = indexTable.ptr<cv::Vec3s>(u);
float* dispTableU = dispTable.ptr<float>(u);
// process vB = 0 to vhor
// in this range, the class ground is not evaluated
for (int vB = 0; vB <= vhor; vB++)
{
BestCost bestCost;
// process vT = 0
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB);
const int fn = cvRound(dO1);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = LUT.sumO(fn, vB);
const float dataCostS = LUT.sumS(vB);
// initialize best cost
bestCost.init({ dataCostG, dataCostO, dataCostS }, dO1);
}
for (int vT = 1; vT <= vB; vT++)
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB, vT);
const float dO2 = dispTableU[vT - 1];
const int fn = cvRound(dO1);
// compute data cost
const float dataCostO = LUT.sumO(fn, vB, vT);
const float dataCostS = LUT.sumS(vB, vT);
// compute total cost
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(dO1, dO2);
const float costOS = dataCostO + prevCost[S] + Cmc;
const float costSO = dataCostS + prevCost[O] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, dO1);
bestCost.update<O, S>(vT, costOS, dO1);
bestCost.update<S, O>(vT, costSO);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
dispTableU[vB] = bestCost.dispO;
}
// process vhor + 1 to vmax
// in this range, the class sky is not evaluated
for (int vB = vhor + 1; vB < vmax; vB++)
{
BestCost bestCost;
// process vT = 0
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB);
const int fn = cvRound(dO1);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = LUT.sumO(fn, vB);
const float dataCostS = Cinf;
// initialize best cost
bestCost.init({ dataCostG, dataCostO, dataCostS }, dO1);
}
// process vT = 1 to vhor
// in this range, transition from/to ground is not allowed
for (int vT = 1; vT <= vhor; vT++)
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB, vT);
const float dO2 = dispTableU[vT - 1];
const int fn = cvRound(dO1);
// compute data cost
const float dataCostO = LUT.sumO(fn, vB, vT);
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(dO1, dO2);
const float costOS = dataCostO + prevCost[S] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, dO1);
bestCost.update<O, S>(vT, costOS, dO1);
}
// process vT = vhor + 1 to vB
// in this range, transition from sky is not allowed
for (int vT = vhor + 1; vT <= vB; vT++)
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB, vT);
const float dO2 = dispTableU[vT - 1];
const int fn = cvRound(dO1);
const float dG1 = road(vT);
const float dG2 = road(vT - 1);
// compute data cost
const float dataCostG = LUT.sumG(vB, vT);
const float dataCostO = LUT.sumO(fn, vB, vT);
const cv::Vec3f& prevCost = costTableU[vT - 1];
// compute total cost
const float costGG = dataCostG + prevCost[G] + Cmc + priorCostGG(dG1, dG2);
const float costGO = dataCostG + prevCost[O] + Cmc + priorCostGO(dG1, dO2);
const float costOG = dataCostO + prevCost[G] + Cmc + priorCostOG(dO1, dG2);
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(dO1, dO2);
// update best cost
bestCost.update<G, G>(vT, costGG);
bestCost.update<G, O>(vT, costGO);
bestCost.update<O, G>(vT, costOG, dO1);
bestCost.update<O, O>(vT, costOO, dO1);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
dispTableU[vB] = bestCost.dispO;
}
}
std::pair<float, int> calcMinCostAndLabel(const cv::Mat1f& SATsem,
const std::vector<int>& labels, int vB, int vT, int iniLabel = -1)
{
auto calcCost = [&](int label)
{
return vT > 0 ? SATsem(label, vB) - SATsem(label, vT - 1) : SATsem(label, vB);
};
float minCost = iniLabel >= 0 ? calcCost(iniLabel) : Cinf;
int minLabel = iniLabel;
for (int label : labels)
{
const float cost = calcCost(label);
if (cost < minCost)
{
minCost = cost;
minLabel = label;
}
}
return { minCost, minLabel };
}
static void processOneColumn(int u, int vmax, int dmax, const cv::Mat1f& disparity, const Line& road,
const DataTermG& dataTermG, const DataTermO& dataTermO, const DataTermS& dataTermS,
const cv::Mat1f& SATsem, const std::vector<int> G2L[],
cv::Mat3f& costTable, cv::Mat3s& indexTable, cv::Mat3b& labelTable, cv::Mat1f& dispTable)
{
const int vhor = static_cast<int>(road.vhor());
const int iniLabel = !G2L[NO_GEOMETRY].empty() ? G2L[NO_GEOMETRY].front() : -1;
// compute data cost LUT
DispCostLUT LUT(dataTermG, dataTermO, dataTermS, vmax, dmax);
const float* disparityU = disparity.ptr<float>(u);
for (int v = 0; v < vmax; v++)
LUT.add(v, disparityU[v]);
////////////////////////////////////////////////////////////////////////////////////////////
// compute cost tables
//
// for paformance optimization, loop is split at vhor and unnecessary computation is ommited
////////////////////////////////////////////////////////////////////////////////////////////
cv::Mat1f SATsemU = getch(SATsem, u);
cv::Vec3f* costTableU = costTable.ptr<cv::Vec3f>(u);
cv::Vec3s* indexTableU = indexTable.ptr<cv::Vec3s>(u);
cv::Vec3b* labelTableU = labelTable.ptr<cv::Vec3b>(u);
float* dispTableU = dispTable.ptr<float>(u);
// process vB = 0 to vhor
// in this range, the class ground is not evaluated
for (int vB = 0; vB <= vhor; vB++)
{
BestCost bestCost;
// process vT = 0
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB);
const int fn = cvRound(dO1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB, 0, iniLabel);
const auto [minSemCostS, minLabelS] = calcMinCostAndLabel(SATsemU, G2L[S], vB, 0, iniLabel);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = LUT.sumO(fn, vB) + wsem * minSemCostO;
const float dataCostS = LUT.sumS(vB) + wsem * minSemCostS;
// initialize best cost
bestCost.init({ dataCostG, dataCostO, dataCostS }, cv::Vec3b(0, minLabelO, minLabelS), dO1);
}
for (int vT = 1; vT <= vB; vT++)
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB, vT);
const float dO2 = dispTableU[vT - 1];
const int fn = cvRound(dO1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB, vT, iniLabel);
const auto [minSemCostS, minLabelS] = calcMinCostAndLabel(SATsemU, G2L[S], vB, vT, iniLabel);
// compute data cost
const float dataCostO = LUT.sumO(fn, vB, vT) + wsem * minSemCostO;
const float dataCostS = LUT.sumS(vB, vT) + wsem * minSemCostS;
// compute total cost
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(dO1, dO2);
const float costOS = dataCostO + prevCost[S] + Cmc;
const float costSO = dataCostS + prevCost[O] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, dO1, minLabelO);
bestCost.update<O, S>(vT, costOS, dO1, minLabelO);
bestCost.update<S, O>(vT, costSO, minLabelS);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
labelTableU[vB] = bestCost.labels;
dispTableU[vB] = bestCost.dispO;
}
// process vhor + 1 to vmax
// in this range, the class sky is not evaluated
for (int vB = vhor + 1; vB < vmax; vB++)
{
BestCost bestCost;
// process vT = 0
{
// compute mean disparity within the range of vB to vT
const float dO1 = LUT.meanD(vB);
const int fn = cvRound(dO1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB, 0, iniLabel);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = LUT.sumO(fn, vB) + wsem * minSemCostO;
const float dataCostS = Cinf;