-
Notifications
You must be signed in to change notification settings - Fork 2
/
slanted_stixels.cpp
1210 lines (999 loc) · 34.9 KB
/
slanted_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 "slanted_stixels.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
////////////////////////////////////////////////////////////////////////////////////
// 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;
// maximum cost
static constexpr float Cinf = std::numeric_limits<float>::max();
// model complexity prior
static constexpr float Cmc = 4;
// 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;
// sigma for plane prior
static constexpr float sigma_aG = 1.f;
static constexpr float sigma_bG = 1.f;
static constexpr float sigmaSq_aG = sigma_aG * sigma_aG;
static constexpr float sigmaSq_bG = sigma_bG * sigma_bG;
// 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;
// semantic cost weight
static constexpr float wsem = 0.5f;
// default cost
static constexpr float Cdef = 0;
////////////////////////////////////////////////////////////////////////////////////
// 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;
};
struct PlaneSAT
{
PlaneSAT(int n) : sxx(0), sxy(0), syy(0), sx(0), sy(0), sw(0)
{
SATxx.resize(n);
SATxy.resize(n);
SATyy.resize(n);
SATx.resize(n);
SATy.resize(n);
SATw.resize(n);
}
inline void add(int v, float d, float w)
{
const float x = static_cast<float>(v);
const float y = d;
if (d < 0)
w = 0;
sxx += w * x * x;
sxy += w * x * y;
syy += w * y * y;
sx += w * x;
sy += w * y;
sw += w;
SATxx[v] = sxx;
SATxy[v] = sxy;
SATyy[v] = syy;
SATx[v] = sx;
SATy[v] = sy;
SATw[v] = sw;
}
inline void setInterval(int vB)
{
sxx = SATxx[vB];
sxy = SATxy[vB];
syy = SATyy[vB];
sx = SATx[vB];
sy = SATy[vB];
sw = SATw[vB];
}
inline void setInterval(int vB, int vT)
{
sxx = SATxx[vB] - SATxx[vT - 1];
sxy = SATxy[vB] - SATxy[vT - 1];
syy = SATyy[vB] - SATyy[vT - 1];
sx = SATx[vB] - SATx[vT - 1];
sy = SATy[vB] - SATy[vT - 1];
sw = SATw[vB] - SATw[vT - 1];
}
std::vector<float> SATxx, SATxy, SATyy, SATx, SATy, SATw;
float sxx, sxy, syy, sx, sy, sw;
};
////////////////////////////////////////////////////////////////////////////////////
// Static functions
////////////////////////////////////////////////////////////////////////////////////
static constexpr float squared(float x)
{
return x * x;
}
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);
}
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);
}
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 = static_cast<float>(scale[0]);
const float b = static_cast<float>(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 std::pair<Line, float> LSFitGrd(float sxx, float sxy, float syy, float sx, float sy,
float sw, float ma, float mb)
{
constexpr float wa = sigmaDSq[G] / sigmaSq_aG;
constexpr float wb = sigmaDSq[G] / sigmaSq_bG;
// solve below linear equation
// | sxx + wa : sx ||a| = | sxy + wa x ua |
// | sx : sw + wb ||b| | sy + wb x ub |
// apply prior
sxx += wa;
sw += wb;
sxy += wa * ma;
sy += wb * mb;
const float det = sxx * sw - sx * sx;
if (det < std::numeric_limits<float>::epsilon())
return { Line(), Cdef };
// compute solution
const float invdet = 1 / det;
const float a = invdet * (sw * sxy - sx * sy);
const float b = invdet * (sxx * sy - sx * sxy);
// compute fitting error
const float A = sxx * a * a + 2 * sx * a * b + sw * b * b;
const float B = -2 * (sxy * a + sy * b);
const float C = syy + wa * ma * ma + wb * mb * mb;
return { Line(a, b), A + B + C };
}
static inline std::pair<Line, float> LSFitObj(float syy, float sy, float sw)
{
if (sw < std::numeric_limits<float>::epsilon())
return { Line(), Cdef };
// compute solution
const float b = sy / sw;
// compute fitting error
const float A = sw * b * b;
const float B = -2 * sy * b;
const float C = syy;
return { Line(0, b), A + B + C };
}
static void calcDisparitySigmaGrd(cv::Mat1f& invSigmaGSq, int vmax, const CameraParameters& camera,
const Line& road)
{
invSigmaGSq.create(1, vmax);
const float bf = camera.baseline * camera.fu;
const float invHcam = 1.f / camera.height;
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;
invSigmaGSq(v) = 1.f / sigmaGSq;
}
}
static void calcDisparitySigmaObj(cv::Mat1f& invSigmaOSq, int dmax, const CameraParameters& camera)
{
invSigmaOSq.create(1, dmax);
const float bf = camera.baseline * camera.fu;
const float invDeltaD = deltaZ / bf;
for (int fn = 0; fn < dmax; fn++)
{
const float sigmaZSq = squared(invDeltaD * fn * fn);
const float sigmaOSq = sigmaDSq[O] + sigmaZSq;
invSigmaOSq(fn) = 1.f / sigmaOSq;
}
}
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);
dispG = { 0, 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);
dispG = { 0, 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, const Line& line)
{
static_assert(C1 == G, "C1 must be class Grd");
if (cost < costs[C1])
{
costs[C1] = cost;
points[C1] = packIndex(C2, vT - 1);
dispG = { line.a, line.b };
}
}
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, const Line& line, int label)
{
static_assert(C1 == G, "C1 must be class Grd");
if (cost < costs[C1])
{
costs[C1] = cost;
points[C1] = packIndex(C2, vT - 1);
labels[C1] = label;
dispG = { line.a, line.b };
}
}
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;
cv::Vec2f dispG;
float dispO;
};
static void processOneColumn(int u, const cv::Mat1f& disparity, const cv::Mat1f& confidence,
const Line& road, const cv::Mat1f& invSigmaGSq, const cv::Mat1f& invSigmaOSq,
cv::Mat3f& costTable, cv::Mat3s& indexTable, cv::Mat2f& dispTableG, cv::Mat1f& dispTableO)
{
const int vmax = disparity.cols;
const int dmax = invSigmaOSq.cols;
const int vhor = static_cast<int>(road.vhor());
// compute Summed Area Tables (SAT)
const float* disparityU = disparity.ptr<float>(u);
const float* confidenceU = confidence.ptr<float>(u);
PlaneSAT SAT(vmax);
for (int v = 0; v < vmax; v++)
SAT.add(v, disparityU[v], confidenceU[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);
cv::Vec2f* dispTableGU = dispTableG.ptr<cv::Vec2f>(u);
float* dispTableOU = dispTableO.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 sums within the range of vB to vT
SAT.setInterval(vB);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const float errorS = SAT.syy;
const int d = std::min(cvRound(lineO.b), dmax - 1);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = invSigmaOSq(d) * errorO;
const float dataCostS = invSigmaDSq[S] * errorS;
// initialize best cost
bestCost.init({ dataCostG, dataCostO, dataCostS }, lineO.b);
}
for (int vT = 1; vT <= vB; vT++)
{
// compute sums within the range of vB to vT
SAT.setInterval(vB, vT);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const float errorS = SAT.syy;
const int d = std::min(cvRound(lineO.b), dmax - 1);
// compute data cost
const float dataCostO = invSigmaOSq(d) * errorO;
const float dataCostS = invSigmaDSq[S] * errorS;
// compute total cost
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float prevDispO = dispTableOU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(lineO.b, prevDispO);
const float costOS = dataCostO + prevCost[S] + Cmc;
const float costSO = dataCostS + prevCost[O] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, lineO.b);
bestCost.update<O, S>(vT, costOS, lineO.b);
bestCost.update<S, O>(vT, costSO);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
dispTableGU[vB] = bestCost.dispG;
dispTableOU[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 sums within the range of vB to vT
SAT.setInterval(vB);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const int d = std::min(cvRound(lineO.b), dmax - 1);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = invSigmaOSq(d) * errorO;
const float dataCostS = Cinf;
// initialize best cost
bestCost.init({ dataCostG, dataCostO, dataCostS }, lineO.b);
}
// process vT = 1 to vhor
// in this range, transition from/to ground is not allowed
for (int vT = 1; vT <= vhor; vT++)
{
// compute sums within the range of vB to vT
SAT.setInterval(vB, vT);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const int d = std::min(cvRound(lineO.b), dmax - 1);
// compute data cost
const float dataCostO = invSigmaOSq(d) * errorO;
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float prevDispO = dispTableOU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(lineO.b, prevDispO);
const float costOS = dataCostO + prevCost[S] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, lineO.b);
bestCost.update<O, S>(vT, costOS, lineO.b);
}
// process vT = vhor + 1 to vB
// in this range, transition from sky is not allowed
for (int vT = vhor + 1; vT <= vB; vT++)
{
// compute sums within the range of vB to vT
SAT.setInterval(vB, vT);
// least squares fit and compute fit error
const auto [lineG, errorG] = LSFitGrd(SAT.sxx, SAT.sxy, SAT.syy, SAT.sx, SAT.sy, SAT.sw, road.a, road.b);
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const int d = std::min(cvRound(lineO.b), dmax - 1);
// compute data cost
const float dataCostG = invSigmaGSq(vB) * errorG;
const float dataCostO = invSigmaOSq(d) * errorO;
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float prevDispG = Line(dispTableGU[vT - 1])(vT - 1);
const float prevDispO = dispTableOU[vT - 1];
// compute total cost
const float costGG = dataCostG + prevCost[G] + Cmc + priorCostGG(lineG(vT), prevDispG);
const float costGO = dataCostG + prevCost[O] + Cmc + priorCostGO(lineG(vT), prevDispO);
const float costOG = dataCostO + prevCost[G] + Cmc + priorCostOG(lineO.b, prevDispG);
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(lineO.b, prevDispO);
// update best cost
bestCost.update<G, G>(vT, costGG, lineG);
bestCost.update<G, O>(vT, costGO, lineG);
bestCost.update<O, G>(vT, costOG, lineO.b);
bestCost.update<O, O>(vT, costOO, lineO.b);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
dispTableGU[vB] = bestCost.dispG;
dispTableOU[vB] = bestCost.dispO;
}
}
std::pair<float, int> calcMinCostAndLabel(const cv::Mat1f& SATsem,
const std::vector<int>& labels, int vB, int vT = 0)
{
float minCost = Cinf;
int minLabel = -1;
for (int label : labels)
{
const float cost = vT > 0 ? SATsem(label, vB) - SATsem(label, vT - 1) : SATsem(label, vB);
if (cost < minCost)
{
minCost = cost;
minLabel = label;
}
}
return { minCost, minLabel };
}
static void processOneColumn(int u, const cv::Mat1f& disparity, const cv::Mat1f& confidence,
const Line& road, const cv::Mat1f& invSigmaGSq, const cv::Mat1f& invSigmaOSq,
const cv::Mat1f& SATsem, const std::vector<int> G2L[], cv::Mat3f& costTable,
cv::Mat3s& indexTable, cv::Mat3b& labelTable, cv::Mat2f& dispTableG, cv::Mat1f& dispTableO)
{
const int vmax = disparity.cols;
const int dmax = invSigmaOSq.cols;
const int vhor = static_cast<int>(road.vhor());
// compute Summed Area Tables (SAT) for slanted plane
const float* disparityU = disparity.ptr<float>(u);
const float* confidenceU = confidence.ptr<float>(u);
PlaneSAT SAT(vmax);
for (int v = 0; v < vmax; v++)
SAT.add(v, disparityU[v], confidenceU[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);
cv::Vec2f* dispTableGU = dispTableG.ptr<cv::Vec2f>(u);
float* dispTableOU = dispTableO.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 sums within the range of vB to vT
SAT.setInterval(vB);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const float errorS = SAT.syy;
const int d = std::min(cvRound(lineO.b), dmax - 1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB);
const auto [minSemCostS, minLabelS] = calcMinCostAndLabel(SATsemU, G2L[S], vB);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = invSigmaOSq(d) * errorO + wsem * minSemCostO;
const float dataCostS = invSigmaDSq[S] * errorS + wsem * minSemCostS;
// initialize best cost
bestCost.init(cv::Vec3f(dataCostG, dataCostO, dataCostS),
cv::Vec3b(0, minLabelO, minLabelS), lineO.b);
}
for (int vT = 1; vT <= vB; vT++)
{
// compute sums within the range of vB to vT
SAT.setInterval(vB, vT);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const float errorS = SAT.syy;
const int d = std::min(cvRound(lineO.b), dmax - 1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB, vT);
const auto [minSemCostS, minLabelS] = calcMinCostAndLabel(SATsemU, G2L[S], vB, vT);
// compute data cost
const float dataCostO = invSigmaOSq(d) * errorO + wsem * minSemCostO;
const float dataCostS = invSigmaDSq[S] * errorS + wsem * minSemCostS;
// compute total cost
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float prevDispO = dispTableOU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(lineO.b, prevDispO);
const float costOS = dataCostO + prevCost[S] + Cmc;
const float costSO = dataCostS + prevCost[O] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, lineO.b, minLabelO);
bestCost.update<O, S>(vT, costOS, lineO.b, minLabelO);
bestCost.update<S, O>(vT, costSO, minLabelS);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
labelTableU[vB] = bestCost.labels;
dispTableGU[vB] = bestCost.dispG;
dispTableOU[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 sums within the range of vB to vT
SAT.setInterval(vB);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const int d = std::min(cvRound(lineO.b), dmax - 1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB);
// compute data cost
const float dataCostG = Cinf;
const float dataCostO = invSigmaOSq(d) * errorO + wsem * minSemCostO;
const float dataCostS = Cinf;
// initialize best cost
bestCost.init(cv::Vec3f(dataCostG, dataCostO, dataCostS),
cv::Vec3b(0, minLabelO, 0), lineO.b);
}
// process vT = 1 to vhor
// in this range, transition from/to ground is not allowed
for (int vT = 1; vT <= vhor; vT++)
{
// compute sums within the range of vB to vT
SAT.setInterval(vB, vT);
// least squares fit and compute fit error
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const int d = std::min(cvRound(lineO.b), dmax - 1);
// minimization over the semantic labels
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB, vT);
// compute data cost
const float dataCostO = invSigmaOSq(d) * errorO + wsem * minSemCostO;
// compute total cost
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float prevDispO = dispTableOU[vT - 1];
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(lineO.b, prevDispO);
const float costOS = dataCostO + prevCost[S] + Cmc;
// update best cost
bestCost.update<O, O>(vT, costOO, lineO.b, minLabelO);
bestCost.update<O, S>(vT, costOS, lineO.b, minLabelO);
}
// process vT = vhor + 1 to vB
// in this range, transition from sky is not allowed
for (int vT = vhor + 1; vT <= vB; vT++)
{
// compute sums within the range of vB to vT
SAT.setInterval(vB, vT);
// least squares fit and compute fit error
const auto [lineG, errorG] = LSFitGrd(SAT.sxx, SAT.sxy, SAT.syy, SAT.sx, SAT.sy, SAT.sw, road.a, road.b);
const auto [lineO, errorO] = LSFitObj(SAT.syy, SAT.sy, SAT.sw);
const int d = std::min(cvRound(lineO.b), dmax - 1);
// minimization over the semantic labels
const auto [minSemCostG, minLabelG] = calcMinCostAndLabel(SATsemU, G2L[G], vB, vT);
const auto [minSemCostO, minLabelO] = calcMinCostAndLabel(SATsemU, G2L[O], vB, vT);
// compute data cost
const float dataCostG = invSigmaGSq(vB) * errorG + wsem * minSemCostG;
const float dataCostO = invSigmaOSq(d) * errorO + wsem * minSemCostO;
// compute total cost
const cv::Vec3f& prevCost = costTableU[vT - 1];
const float prevDispG = Line(dispTableGU[vT - 1])(vT - 1);
const float prevDispO = dispTableOU[vT - 1];
const float costGG = dataCostG + prevCost[G] + Cmc + priorCostGG(lineG(vT), prevDispG);
const float costGO = dataCostG + prevCost[O] + Cmc + priorCostGO(lineG(vT), prevDispO);
const float costOG = dataCostO + prevCost[G] + Cmc + priorCostOG(lineO.b, prevDispG);
const float costOO = dataCostO + prevCost[O] + Cmc + priorCostOO(lineO.b, prevDispO);
// update best cost
bestCost.update<G, G>(vT, costGG, lineG, minLabelG);
bestCost.update<G, O>(vT, costGO, lineG, minLabelG);
bestCost.update<O, G>(vT, costOG, lineO.b, minLabelO);
bestCost.update<O, O>(vT, costOO, lineO.b, minLabelO);
}
costTableU[vB] = bestCost.costs;
indexTableU[vB] = bestCost.points;
labelTableU[vB] = bestCost.labels;
dispTableGU[vB] = bestCost.dispG;
dispTableOU[vB] = bestCost.dispO;
}
}
static void extractStixels(const cv::Mat3f& costTable, const cv::Mat3s& indexTable,
const cv::Mat2f& dispTableG, const cv::Mat1f& dispTableO, std::vector<Stixel>& stixels)
{
const int umax = costTable.rows;
const int vmax = costTable.cols;
for (int u = 0; u < umax; u++)
{
float minCost = std::numeric_limits<float>::max();
cv::Point minPos;
for (int c = 0; c < 3; c++)
{
const float cost = costTable(u, vmax - 1)[c];
if (cost < minCost)
{
minCost = cost;
minPos = cv::Point(c, vmax - 1);
}
}
while (minPos.y > 0)
{
const int geoId = minPos.x;
const int v = minPos.y;
const cv::Point p1 = minPos;
const cv::Point p2 = unpackIndex(indexTable(u, v)[geoId]);
Stixel stixel;
stixel.uL = u;
stixel.vT = p2.y + 1;
stixel.vB = p1.y + 1;
stixel.width = 1;
stixel.geoId = geoId;
stixel.semId = 0;
if (geoId == G)
stixel.disp = dispTableG(u, v);
if (geoId == O)
stixel.disp = cv::Vec2f(0, dispTableO(u, v));
if (geoId == S)
stixel.disp = cv::Vec2f(0, 0);
stixels.push_back(stixel);
minPos = p2;
}
}
}
static void extractStixels(const cv::Mat3f& costTable, const cv::Mat3s& indexTable, const cv::Mat3b& labelTable,
const cv::Mat2f& dispTableG, const cv::Mat1f& dispTableO, std::vector<Stixel>& stixels)
{
const int umax = costTable.rows;
const int vmax = costTable.cols;
for (int u = 0; u < umax; u++)
{
float minCost = std::numeric_limits<float>::max();
cv::Point minPos;
for (int c = 0; c < 3; c++)
{
const float cost = costTable(u, vmax - 1)[c];
if (cost < minCost)
{
minCost = cost;
minPos = cv::Point(c, vmax - 1);
}
}
while (minPos.y > 0)
{
const int geoId = minPos.x;
const int v = minPos.y;
const cv::Point p1 = minPos;
const cv::Point p2 = unpackIndex(indexTable(u, v)[geoId]);
Stixel stixel;
stixel.uL = u;
stixel.vT = p2.y + 1;
stixel.vB = p1.y + 1;
stixel.width = 1;
stixel.geoId = geoId;
stixel.semId = labelTable(u, v)[geoId];
if (geoId == G)
stixel.disp = dispTableG(u, v);