-
Notifications
You must be signed in to change notification settings - Fork 2
/
DCFG.cpp
1890 lines (1601 loc) · 64.2 KB
/
DCFG.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 "DCFG.h"
#include <Windows.h>
#define DFCG_DEBUG 1
std::vector<std::vector<cv::Point>> findBiggestContour(cv::Mat& img, int numOfContours);
//-----------------
//-- timing debug
//-----------------
timeval tic, toc, tic_total, toc_total;
float rt_preProc; // pre Processing time
float rt_motionComp; // motion Compensation time
float rt_modelUpdate; // model update time
float rt_total; // Background Subtraction time
#if defined _WIN32 || defined _WIN64
int gettimeofday(struct timeval* tp, int* tz)
{
LARGE_INTEGER tickNow;
static LARGE_INTEGER tickFrequency;
static BOOL tickFrequencySet = FALSE;
if (tickFrequencySet == FALSE) {
QueryPerformanceFrequency(&tickFrequency);
tickFrequencySet = TRUE;
}
QueryPerformanceCounter(&tickNow);
tp->tv_sec = (long)(tickNow.QuadPart / tickFrequency.QuadPart);
tp->tv_usec = (long)(((tickNow.QuadPart % tickFrequency.QuadPart) * 1000000L) / tickFrequency.QuadPart);
return 0;
}
#else
#include <sys/time.h>
#endif
//-----------------
DCFG::DCFG(const cv::Mat& frame) : frameSize(frame.size()) {
//------------------------
//-- Global variables
//------------------------
cv::cvtColor(frame, frameGray, CV_RGB2GRAY);
//cv::medianBlur(frameGray, frameGray, 5);
frameGray.copyTo(prevGray);
frame.copyTo(colorFrame);
frame.copyTo(prevColorFrame);
oldMask = cv::Mat(frameSize, CV_8UC1,0.0);
fgMaskDilate = cv::Mat(frameSize, CV_8UC1,0.0);
fgCandidate = cv::Mat(frameSize, CV_8UC1,0.0);
blockMag = cv::Mat(frameSize, CV_32FC1,0.0);
blockAng = cv::Mat(frameSize, CV_32FC1,0.0);
//------------------------
//-- Gaussian model
//------------------------
m_DistImg = 0;
for (int i = 0; i < NUM_MODELS; ++i) {
m_Mean[i] = 0;
m_Var[i] = 0;
m_Age[i] = 0;
m_Mean_Temp[i] = 0;
m_Var_Temp[i] = 0;
m_Age_Temp[i] = 0;
}
for (int i = 0; i < NUM_MODELS_FG; ++i) {
f_Mean[i] = 0;
f_Var[i] = 0;
f_Age[i] = 0;
m_DistImg_FG[i] = 0;
}
obsWidth = frame.cols; //-- observation (frame) width
obsHeight = frame.rows;
obsSize = obsWidth * obsHeight;
modelWidth = obsWidth / BLOCK_SIZE; //-- bg model width (which is smaller because we use blocks instead of pixels)
modelHeight = obsHeight / BLOCK_SIZE;
modelSize = modelWidth * modelHeight; //-- total number of pixels in the bg model (bg image)
//-- Initialize Storage
m_DistImg = new float[obsSize] {};
m_ModelIdx = new int[modelSize] {};
for (int i = 0; i < NUM_MODELS_FG; ++i)
m_DistImg_FG[i] = new float[obsSize] {};
for (int i = 0; i < NUM_MODELS; ++i) {
m_Mean[i] = new float[modelSize] {};
m_Var[i] = new float[modelSize] {};
m_Age[i] = new float[modelSize] {};
m_Mean_Temp[i] = new float[modelSize] {};
m_Var_Temp[i] = new float[modelSize] {};
m_Age_Temp[i] = new float[modelSize] {};
}
//------------------------
//-- KLT
//------------------------
termcrit = cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 20, 0.03);
winSize = cv::Size(WIN_SIZE, WIN_SIZE);
//-- Init homography
for (int i = 0; i < 9; i++)
matH[i] = i / 3 == i % 3 ? 1 : 0;
//------------------------
//-- ViBe
//------------------------
//originalVibe_Init_BGR(frame);
originalVibe_Init_GRAY(frameGray);
//outFile.open("D:/Project/shadow/results/log.txt");
}
DCFG::~DCFG() {
delete[] m_Mean;
delete[] m_Var;
delete[] m_Age;
delete[] m_Mean_Temp;
delete[] m_Var_Temp;
delete[] m_Age_Temp;
delete[] m_DistImg;
delete[] m_DistImg_FG;
delete[] m_ModelIdx;
outFile.close();
}
//------------------------
//-- MCD
//------------------------
void DCFG::apply(const cv::Mat& frame, cv::Mat& fgMask, cv::Mat& bg)
{
frameNum++;
fgCandidate.setTo(0);
#if DFCG_DEBUG
frame.copyTo(colorFrame);
cv::cvtColor(frame, frameGray, CV_RGB2GRAY);
cv::medianBlur(frameGray, frameGray, 5);
if (!fgMask.empty()) cv::dilate(fgMask, fgMaskDilate, cv::Mat(), cv::Point(-1, -1), 8, 1, 1);
gettimeofday(&tic, NULL);
#endif
//-- pre processing
#if DFCG_DEBUG
gettimeofday(&toc, NULL);
rt_preProc = (float)(toc.tv_usec - tic.tv_usec) / 1000.0;
gettimeofday(&tic, NULL);
#endif
//-- optical flow
KLT(frameGray);
//-- motion compensation
motionCompensate(matH);
#if DFCG_DEBUG
gettimeofday(&toc, NULL);
rt_motionComp = (float)(toc.tv_usec - tic.tv_usec) / 1000.0;
//-- background model
gettimeofday(&tic, NULL);
#endif
//-- Update BG Model and Detect
update(fgMask);
#if DFCG_DEBUG
gettimeofday(&toc, NULL);
rt_modelUpdate = (float)(toc.tv_usec - tic.tv_usec) / 1000.0;
rt_total = rt_preProc + rt_motionComp + rt_modelUpdate;
#endif
//-- Illumination change estimation (paper: Scene conditional background update for moving object detection in a moving camera)
cv::Scalar frameMean = cv::mean(frameGray, 255 - fgMask);
bgMean /= modelSize;
illum = frameMean[0] - bgMean;
//std::cout << "frame: " << frameMean[0] << " bg: " << bgMean << std::endl;
#if DFCG_DEBUG
// Debug display of individual maps
//cv::Mat mean = cv::Mat(modelHeight, modelWidth, CV_32F, m_Mean[0]);
//cv::resize(mean, mean, cv::Size(), BLOCK_SIZE, BLOCK_SIZE);
//cv::imshow("mean", mean / 255.0);
//cv::Mat var = cv::Mat(modelHeight, modelWidth, CV_32F, m_Var[0]);
//cv::resize(var, var, cv::Size(), BLOCK_SIZE, BLOCK_SIZE);
//cv::imshow("var", var / 2550.0);
//cv::Mat age = cv::Mat(modelHeight, modelWidth, CV_32F, m_Age[0]);
//cv::resize(age, age, cv::Size(), BLOCK_SIZE, BLOCK_SIZE);
//cv::imshow("age", age / 50.0);
//-- draw normal chart
//const int graph_width = 640;
//const int graph_height = 140;
//cv::Mat graph(graph_height, graph_width, CV_8UC3, cv::Scalar(255, 255, 255));
//cv::Scalar color = cv::Scalar(0, 0, 0);
//draw_1d_gaussian_mixture(graph, f_Mean, f_Var, f_Weight, f_Num);
//cv::imshow("graph", graph);
//////////////////////////////////////////////////////////////////////////
// Debug Output
for (int i = 0; i < 100; ++i) {
printf("\b");
}
printf("PP: %.2f(ms)\tOF: %.2f(ms)\tBGM: %.2f(ms)\tTotal time: \t%.2f(ms)", MAX(0.0, rt_preProc), MAX(0.0, rt_motionComp), MAX(0.0, rt_modelUpdate), MAX(0.0, rt_total));
frame.copyTo(prevColorFrame);
//-- red mask
//cv::Mat redMask = cv::Mat::zeros(frame.rows, frame.cols, CV_8UC3);
//redMask.setTo(cv::Scalar(0, 0, 255), fgMask);
//debugImg = frame + .5 * redMask;
//cv::imshow("debugImg", debugImg);
//-- fgMask & frame
cv::Mat fgMaskBlur;
cv::medianBlur(fgMask, fgMaskBlur, 5);
cv::hconcat(frameGray, fgMaskBlur, debugImg);
cv::hconcat(debugImg, oldMask, debugImg);
cv::imshow("debugImg", debugImg);
cv::Mat fgAndCandid(frameSize, CV_8UC1, 0.0);
fgAndCandid.setTo(128, cv::Mat(fgCandidate == 0 & fgMaskBlur == 0));
fgAndCandid.setTo(0, cv::Mat(fgCandidate == 255));
fgAndCandid.setTo(255, cv::Mat(fgMaskBlur == 255));
cv::imshow("fgAndCandid", fgAndCandid);
cv::Mat markers;
fgAndCandid.convertTo(markers, CV_32S);
cv::watershed(colorFrame, markers);
cv::Mat mark;
markers.convertTo(mark, CV_8U);
fgMask.setTo(255, cv::Mat(mark == 255));
//-- bounding box
//cv::Mat fgMaskDilate;
//cv::dilate(fgMask, fgMaskDilate, cv::Mat(), cv::Point(-1, -1), 16, 1, 1);
//cv::erode(fgMask, fgMaskDilate, cv::Mat(), cv::Point(-1, -1), 1, 1, 1);
//cv::dilate(fgMask, fgMaskDilate, cv::Mat(), cv::Point(-1, -1), 4, 1, 1);
//cv::imshow("fgMaskDilate", fgMaskDilate);
//cv::RNG rng(12345);
//std::vector<std::vector<cv::Point> > contours;
////cv::findContours(fgMaskDilate, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
//contours = findBiggestContour(fgMaskDilate, 1);
//std::vector<std::vector<cv::Point> > contours_poly(contours.size());
//std::vector<cv::Rect> boundRect(contours.size());
//std::vector<cv::Point2f>centers(contours.size());
//std::vector<cv::RotatedRect> minEllipse(contours.size());
//std::vector<float>radius(contours.size());
//for (size_t i = 0; i < contours.size(); i++)
//{
// approxPolyDP(contours[i], contours_poly[i], 3, true);
// boundRect[i] = boundingRect(contours_poly[i]);
// minEnclosingCircle(contours_poly[i], centers[i], radius[i]);
// //minEllipse[i] = fitEllipse(contours[i]);
//}
//cv::Mat drawing = cv::Mat::zeros(fgMaskDilate.size(), CV_8UC3);
//frame.copyTo(drawing);
//for (size_t i = 0; i < contours.size(); i++)
//{
// if (contours[i].size() > 10) {
// //cv::Scalar color = cv::Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
// cv::Scalar color = cv::Scalar(0, 0, 255);
// //drawContours(drawing, contours_poly, (int)i, color);
// rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2);
// //circle(drawing, centers[i], (int)radius[i], color, 2);
// //circle(drawing, centers[i], 70, cv::Scalar(100, 0, 255), 2);
// //ellipse(drawing, minEllipse[i], color, 2);
// }
//}
//
//drawing.copyTo(debugImg);
//imshow("debugImg", debugImg);
#endif
frameGray.copyTo(prevGray);
bg = cv::Mat(modelHeight, modelWidth, CV_32F, m_Mean[0]);
cv::resize(bg, bg, cv::Size(), BLOCK_SIZE, BLOCK_SIZE);
bg.convertTo(bg, CV_8U);
}
//------------------------
//-- Gaussian model
//------------------------
void DCFG::motionCompensate(double h[9]) {
//-- compensate models for the current view
for (int j = 0; j < modelHeight; ++j) {
for (int i = 0; i < modelWidth; ++i) {
//-- x and y coordinates for current model (center of each block)
float X, Y;
float W = 1.0;
X = BLOCK_SIZE * i + BLOCK_SIZE / 2.0;
Y = BLOCK_SIZE * j + BLOCK_SIZE / 2.0;
//-- transformed coordinates with h
float newW = h[6] * X + h[7] * Y + h[8];
float newX = (h[0] * X + h[1] * Y + h[2]) / newW;
float newY = (h[3] * X + h[4] * Y + h[5]) / newW;
//-- transformed i,j coordinates of old position
float newI = newX / BLOCK_SIZE;
float newJ = newY / BLOCK_SIZE;
int idxNewI = floor(newI);
int idxNewJ = floor(newJ);
float di = newI - ((float)(idxNewI)+0.5);
float dj = newJ - ((float)(idxNewJ)+0.5);
float w_H{ 0.0 }, w_V{ 0.0 }, w_HV{ 0.0 }, w_self{ 0.0 }, sumW{ 0.0 };
int idxNow = i + j * modelWidth;
#define WARP_MIX
//-- For Mean and Age
{
float temp_mean[4][NUM_MODELS] = {};
float temp_age[4][NUM_MODELS] = {};
#ifdef WARP_MIX
//-- Horizontal Neighbor
if (di != 0) {
int idx_new_i = idxNewI;
int idx_new_j = idxNewJ;
idx_new_i += di > 0 ? 1 : -1;
if (idx_new_i >= 0 && idx_new_i < modelWidth && idx_new_j >= 0 && idx_new_j < modelHeight) {
w_H = fabs(di) * (1.0 - fabs(dj)); //-- intersected area
sumW += w_H;
int idxNew = idx_new_i + idx_new_j * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_mean[0][m] = w_H * m_Mean[m][idxNew];
temp_age[0][m] = w_H * m_Age[m][idxNew];
}
}
}
//-- Vertical Neighbor
if (dj != 0) {
int idx_new_i = idxNewI;
int idx_new_j = idxNewJ;
idx_new_j += dj > 0 ? 1 : -1;
if (idx_new_i >= 0 && idx_new_i < modelWidth && idx_new_j >= 0 && idx_new_j < modelHeight) {
w_V = fabs(dj) * (1.0 - fabs(di));
sumW += w_V;
int idxNew = idx_new_i + idx_new_j * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_mean[1][m] = w_V * m_Mean[m][idxNew];
temp_age[1][m] = w_V * m_Age[m][idxNew];
}
}
}
//-- HV Neighbor
if (dj != 0 && di != 0) {
int idx_new_i = idxNewI;
int idx_new_j = idxNewJ;
idx_new_i += di > 0 ? 1 : -1;
idx_new_j += dj > 0 ? 1 : -1;
if (idx_new_i >= 0 && idx_new_i < modelWidth && idx_new_j >= 0 && idx_new_j < modelHeight) {
w_HV = fabs(di) * fabs(dj);
sumW += w_HV;
int idxNew = idx_new_i + idx_new_j * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_mean[2][m] = w_HV * m_Mean[m][idxNew];
temp_age[2][m] = w_HV * m_Age[m][idxNew];
}
}
}
#endif
//-- Self
if (idxNewI >= 0 && idxNewI < modelWidth && idxNewJ >= 0 && idxNewJ < modelHeight) {
w_self = (1.0 - fabs(di)) * (1.0 - fabs(dj));
sumW += w_self;
int idxNew = idxNewI + idxNewJ * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_mean[3][m] = w_self * m_Mean[m][idxNew];
temp_age[3][m] = w_self * m_Age[m][idxNew];
}
}
if (sumW > 0) {
for (int m = 0; m < NUM_MODELS; ++m) {
#ifdef WARP_MIX
m_Mean_Temp[m][idxNow] = (temp_mean[0][m] + temp_mean[1][m] + temp_mean[2][m] + temp_mean[3][m]) / sumW;
m_Age_Temp[m][idxNow] = (temp_age[0][m] + temp_age[1][m] + temp_age[2][m] + temp_age[3][m]) / sumW;
#else
m_Mean_Temp[m][idxNow] = temp_mean[3][m] / sumW;
m_Age_Temp[m][idxNow] = temp_age[3][m] / sumW;
#endif
}
}
}
//-- For Variance
{
float temp_var[4][NUM_MODELS];
memset(temp_var, 0, sizeof(float) * 4 * NUM_MODELS);
#ifdef WARP_MIX
//-- Horizontal Neighbor
if (di != 0) {
int idx_new_i = idxNewI;
int idx_new_j = idxNewJ;
idx_new_i += di > 0 ? 1 : -1;
if (idx_new_i >= 0 && idx_new_i < modelWidth && idx_new_j >= 0 && idx_new_j < modelHeight) {
int idxNew = idx_new_i + idx_new_j * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_var[0][m] = w_H * (m_Var[m][idxNew] + VARIANCE_INTERPOLATE_PARAM * (pow(m_Mean_Temp[m][idxNow] - m_Mean[m][idxNew], (int)2)));
}
}
}
//-- Vertical Neighbor
if (dj != 0) {
int idx_new_i = idxNewI;
int idx_new_j = idxNewJ;
idx_new_j += dj > 0 ? 1 : -1;
if (idx_new_i >= 0 && idx_new_i < modelWidth && idx_new_j >= 0 && idx_new_j < modelHeight) {
int idxNew = idx_new_i + idx_new_j * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_var[1][m] = w_V * (m_Var[m][idxNew] + VARIANCE_INTERPOLATE_PARAM * (pow(m_Mean_Temp[m][idxNow] - m_Mean[m][idxNew], (int)2)));
}
}
}
// HV Neighbor
if (dj != 0 && di != 0) {
int idx_new_i = idxNewI;
int idx_new_j = idxNewJ;
idx_new_i += di > 0 ? 1 : -1;
idx_new_j += dj > 0 ? 1 : -1;
if (idx_new_i >= 0 && idx_new_i < modelWidth && idx_new_j >= 0 && idx_new_j < modelHeight) {
int idxNew = idx_new_i + idx_new_j * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_var[2][m] = w_HV * (m_Var[m][idxNew] + VARIANCE_INTERPOLATE_PARAM * (pow(m_Mean_Temp[m][idxNow] - m_Mean[m][idxNew], (int)2)));
}
}
}
#endif
//-- Self
if (idxNewI >= 0 && idxNewI < modelWidth && idxNewJ >= 0 && idxNewJ < modelHeight) {
int idxNew = idxNewI + idxNewJ * modelWidth;
for (int m = 0; m < NUM_MODELS; ++m) {
temp_var[3][m] = w_self * (m_Var[m][idxNew] + VARIANCE_INTERPOLATE_PARAM * (pow(m_Mean_Temp[m][idxNow] - m_Mean[m][idxNew], (int)2)));
}
}
if (sumW > 0) {
for (int m = 0; m < NUM_MODELS; ++m) {
#ifdef WARP_MIX
m_Var_Temp[m][idxNow] = (temp_var[0][m] + temp_var[1][m] + temp_var[2][m] + temp_var[3][m]) / sumW;
#else
m_Var_Temp[m][idxNow] = (temp_var[3][m]) / sumW;
#endif
}
}
}
//-- Limitations and Exceptions
for (int m = 0; m < NUM_MODELS; ++m) {
m_Var_Temp[m][i + j * modelWidth] = MAX(m_Var_Temp[m][i + j * modelWidth], MIN_BG_VAR);
}
if (idxNewI < 1 || idxNewI >= modelWidth - 1 || idxNewJ < 1 || idxNewJ >= modelHeight - 1) {
for (int m = 0; m < NUM_MODELS; ++m) {
m_Var_Temp[m][i + j * modelWidth] = INIT_BG_VAR;
m_Age_Temp[m][i + j * modelWidth] = 0;
}
}
else {
for (int m = 0; m < NUM_MODELS; ++m) {
m_Age_Temp[m][i + j * modelWidth] =
MIN(m_Age_Temp[m][i + j * modelWidth] * exp(-VAR_DEC_RATIO * MAX(0.0, m_Var_Temp[m][i + j * modelWidth] - VAR_MIN_NOISE_T)), MAX_BG_AGE);
}
}
//-- debug
//----------------------------------------
//----------------------------------------
//if (i * BLOCK_SIZE == 240 && j * BLOCK_SIZE == 240) {
// cv::Rect r = cv::Rect(X, Y, BLOCK_SIZE, BLOCK_SIZE);
// cv::Rect r2 = cv::Rect(newX, newY, BLOCK_SIZE, BLOCK_SIZE);
// cv::rectangle(prevColorFrame, r, cv::Scalar(255, 0, 0), 1);
// cv::rectangle(colorFrame, r2, cv::Scalar(0, 0, 255), 1);
// cv::putText(prevColorFrame, "Mean[0]: " + std::to_string(m_Mean[0][idxNow]), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255), 3);
// cv::putText(prevColorFrame, "Mean[0]: " + std::to_string(m_Mean[0][idxNow]), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 0, 0), 2);
// cv::putText(colorFrame, "Mean[1]: " + std::to_string(m_Mean[1][idxNow]), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255), 3);
// cv::putText(colorFrame, "Mean[1]: " + std::to_string(m_Mean[1][idxNow]), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(0, 0, 255), 2);
// cv::putText(prevColorFrame, "Age[0]: " + std::to_string(m_Age[0][idxNow]), cv::Point(20, 40), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255), 3);
// cv::putText(prevColorFrame, "Age[0]: " + std::to_string(m_Age[0][idxNow]), cv::Point(20, 40), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 0, 0), 2);
// cv::putText(colorFrame, "Age[1]: " + std::to_string(m_Age[1][idxNow]), cv::Point(20, 40), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255), 3);
// cv::putText(colorFrame, "Age[1]: " + std::to_string(m_Age[1][idxNow]), cv::Point(20, 40), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(0, 0, 255), 2);
// cv::hconcat(prevColorFrame, colorFrame, debugImg);
// std::cout << "\n====================================\n" <<
// "m_Mean[0] = " << m_Mean[0][idxNow] << " m_Mean_Temp[0]: " << m_Mean_Temp[0][idxNow] <<
// " m_Var[0]: " << m_Var[0][idxNow] << " m_Var_Temp[0]: " << m_Var_Temp[0][idxNow] <<
// " m_Age[0]: " << m_Age[0][idxNow] << " m_Age_Temp[0]: " << m_Age_Temp[0][idxNow] << std::endl <<
// "m_Mean[1] = " << m_Mean[1][idxNow] << " m_Mean_Temp[1]: " << m_Mean_Temp[1][idxNow] <<
// " m_Var[1]: " << m_Var[1][idxNow] << " m_Var_Temp[1]: " << m_Var_Temp[1][idxNow] <<
// " m_Age[1]: " << m_Age[1][idxNow] << " m_Age_Temp[1]: " << m_Age_Temp[1][idxNow] << std::endl;
// outFile << "\n====================================\n" <<
// "m_Mean[0] = " << m_Mean[0][idxNow] << " m_Mean_Temp[0]: " << m_Mean_Temp[0][idxNow] <<
// " m_Var[0]: " << m_Var[0][idxNow] << " m_Var_Temp[0]: " << m_Var_Temp[0][idxNow] <<
// " m_Age[0]: " << m_Age[0][idxNow] << " m_Age_Temp[0]: " << m_Age_Temp[0][idxNow] << std::endl <<
// "m_Mean[1] = " << m_Mean[1][idxNow] << " m_Mean_Temp[1]: " << m_Mean_Temp[1][idxNow] <<
// " m_Var[1]: " << m_Var[1][idxNow] << " m_Var_Temp[1]: " << m_Var_Temp[1][idxNow] <<
// " m_Age[1]: " << m_Age[1][idxNow] << " m_Age_Temp[1]: " << m_Age_Temp[1][idxNow] << std::endl;
//}
//----------------------------------------
//----------------------------------------
//----------------------------------------
}
}
}
void DCFG::update(cv::Mat& pOutputImg) {
pOutputImg = cv::Mat(frameSize, CV_8UC1, 0.0);
uchar* pOut = (uchar*)pOutputImg.data;
uchar* pOldOut = (uchar*)oldMask.data;
uchar* pCur = (uchar*)frameGray.data;
uchar* pDilatedFG = (uchar*)fgMaskDilate.data;
uchar* pCandidateFG = (uchar*)fgCandidate.data;
//////////////////////////////////////////////////////////////////////////
//-- Find Matching Model
for (int bIdx_j = 0; bIdx_j < modelHeight; bIdx_j++) {
for (int bIdx_i = 0; bIdx_i < modelWidth; bIdx_i++) {
int curIndex = bIdx_i + bIdx_j * modelWidth;
//-- base (i,j) for this block in the frame
int idx_base_i = ((float)bIdx_i) * BLOCK_SIZE;
int idx_base_j = ((float)bIdx_j) * BLOCK_SIZE;
float cur_mean = 0;
float elem_cnt = 0;
for (int jj = 0; jj < BLOCK_SIZE; ++jj) {
for (int ii = 0; ii < BLOCK_SIZE; ++ii) {
int idx_i = idx_base_i + ii; //-- corresponding position of bg image in the frame
int idx_j = idx_base_j + jj;
if (idx_i < 0 || idx_i >= obsWidth || idx_j < 0 || idx_j >= obsHeight)
continue;
cur_mean += pCur[idx_i + idx_j * obsWidth];
elem_cnt += 1.0;
}
} //loop for pixels
cur_mean /= elem_cnt; //-- mean of the pixels in the frame which correspond to the current block
//////////////////////////////////////////////////////////////////////////
//-- Make Oldest Idx to 0 (swap)
int oldIdx = 0;
float oldAge = 0;
//-- find the oldest model
for (int m = 0; m < NUM_MODELS; ++m) {
float fAge = m_Age_Temp[m][curIndex];
if (fAge >= oldAge) {
oldIdx = m;
oldAge = fAge;
}
}
//-- if there is an older model than the current model (model 0), swap them
if (oldIdx != 0) {
m_Mean_Temp[0][curIndex] = m_Mean_Temp[oldIdx][curIndex];
m_Mean_Temp[oldIdx][curIndex] = cur_mean;
m_Var_Temp[0][curIndex] = m_Var_Temp[oldIdx][curIndex];
m_Var_Temp[oldIdx][curIndex] = INIT_BG_VAR;
m_Age_Temp[0][curIndex] = m_Age_Temp[oldIdx][curIndex];
m_Age_Temp[oldIdx][curIndex] = 0;
}
//////////////////////////////////////////////////////////////////////////
// Select Model
// Check Match against 0
if (pow(cur_mean - m_Mean_Temp[0][curIndex], (int)2) < VAR_THRESH_MODEL_MATCH * m_Var_Temp[0][curIndex]) {
m_ModelIdx[curIndex] = 0;
}
// Check Match against 1
else if (pow(cur_mean - m_Mean_Temp[1][curIndex], (int)2) < VAR_THRESH_MODEL_MATCH * m_Var_Temp[1][curIndex]) {
m_ModelIdx[curIndex] = 1;
}
// If No match, set 1 age to zero and match = 1
else {
//-- update foreground model
int count = 0;
unsigned char pixel = m_Mean_Temp[1][curIndex];
// Let the pixel compare with the backgroundModel set
for (std::vector<cv::Mat>::iterator it = backgroundModel.begin(); it != backgroundModel.end(); it++)
if (abs(int(pixel) - int((*it).at<uchar>(bIdx_j, bIdx_i))) < distanceThreshold)
count++;
if (count < this->minMatch)
updateFG(m_Mean_Temp[1][curIndex]);
//-- initialize the candid bg model
m_ModelIdx[curIndex] = 1;
m_Age_Temp[1][curIndex] = 0;
}
#if DFCG_DEBUG
//int debugX = 240;
//int debugY = 240;
//int n = 1;
//cv::Mat myImg;
//colorFrame.copyTo(myImg);
//if (idx_base_i == debugX && idx_base_j == debugY) {
// std::cout << "\ncur_mean: " << cur_mean << std::endl;
// for (int i = -n; i <= n; i++) {
// for (int j = -n; j <= n; j++) {
// int idX = debugX + i * BLOCK_SIZE;
// int idY = debugY + j * BLOCK_SIZE;
// int curIndex1 = (bIdx_i + i) + (bIdx_j + j) * modelWidth;
// cv::Rect rect(idX, idY, BLOCK_SIZE, BLOCK_SIZE);
// cv::rectangle(myImg, rect, cv::Scalar(255, 0, 0));
// std::cout << "idX: " << idX << "\tidY: " << idY << "\tm_Mean_Temp[0]: " << m_Mean_Temp[0][curIndex1] << "\tm_Mean_Age[0]: " << m_Age_Temp[0][curIndex1]
// << "\tm_Mean_Temp[1]: " << m_Mean_Temp[1][curIndex1] << "\tm_Mean_Age[1]: " << m_Age_Temp[1][curIndex1] << std::endl;
// }
// }
//
// cv::imshow("myImg", myImg);
//}
//
#endif
}
} //-- loop for models
//-- update with current observation
float obs_mean[NUM_MODELS];
float obs_var[NUM_MODELS];
//-- update mean
for (int bIdx_j = 0; bIdx_j < modelHeight; bIdx_j++) {
for (int bIdx_i = 0; bIdx_i < modelWidth; bIdx_i++) {
int curIndex = bIdx_i + bIdx_j * modelWidth;
// base (i,j) for this block
int idx_base_i = ((float)bIdx_i) * BLOCK_SIZE;
int idx_base_j = ((float)bIdx_j) * BLOCK_SIZE;
int nMatchIdx = m_ModelIdx[curIndex]; //-- the model it has been matched with (0 or 1)
//-- obtain observation mean
memset(obs_mean, 0, sizeof(float) * NUM_MODELS);
int nElemCnt[NUM_MODELS] = {};
for (int jj = 0; jj < BLOCK_SIZE; ++jj) {
for (int ii = 0; ii < BLOCK_SIZE; ++ii) {
int idx_i = idx_base_i + ii;
int idx_j = idx_base_j + jj;
if (idx_i < 0 || idx_i >= obsWidth || idx_j < 0 || idx_j >= obsHeight)
continue;
obs_mean[nMatchIdx] += pCur[idx_i + idx_j * obsWidth];
++nElemCnt[nMatchIdx];
}
}
for (int m = 0; m < NUM_MODELS; ++m) {
if (nElemCnt[m] <= 0) {
m_Mean[m][curIndex] = m_Mean_Temp[m][curIndex];
}
else {
//-- learning rate for this block
float age = m_Age_Temp[m][curIndex];
float alpha = age / (age + 1.0);
obs_mean[m] /= ((float)nElemCnt[m]);
//-- update with this mean
if (age < 1) {
m_Mean[m][curIndex] = illum + obs_mean[m];
}
else if (blockMag.at<float>(idx_base_j, idx_base_i) < BLOCK_SIZE)
m_Mean[m][curIndex] = illum + obs_mean[m];
else
m_Mean[m][curIndex] = alpha * (illum + m_Mean_Temp[m][curIndex]) + (1.0 - alpha) * obs_mean[m];
}
}
bgMean += m_Mean[0][curIndex];
}
}
//-- update variance & classify
for (int bIdx_j = 0; bIdx_j < modelHeight; bIdx_j++) {
for (int bIdx_i = 0; bIdx_i < modelWidth; bIdx_i++) {
int curIndex = bIdx_i + bIdx_j * modelWidth;
// TODO: OPTIMIZE THIS PART SO THAT WE DO NOT CALCULATE THIS (LUT)
// base (i,j) for this block
int idx_base_i = ((float)bIdx_i) * BLOCK_SIZE;
int idx_base_j = ((float)bIdx_j) * BLOCK_SIZE;
int nMatchIdx = m_ModelIdx[curIndex];
// obtain observation variance
memset(obs_var, 0, sizeof(float) * NUM_MODELS);
int nElemCnt[NUM_MODELS] = {};
float* fgDist = new float[NUM_MODELS_FG];
for (int jj = 0; jj < BLOCK_SIZE; ++jj) {
for (int ii = 0; ii < BLOCK_SIZE; ++ii) {
int idx_i = idx_base_i + ii;
int idx_j = idx_base_j + jj;
nElemCnt[nMatchIdx]++;
if (idx_i < 0 || idx_i >= obsWidth || idx_j < 0 || idx_j >= obsHeight) {
continue;
}
float pixelDist = 0.0;
float fDiff = pCur[idx_i + idx_j * obsWidth] - m_Mean[nMatchIdx][curIndex];
pixelDist += pow(fDiff, (int)2);
//-- classification
//-- pCur[idx_i + idx_j * obsWidth] : I_j in the paper section 2.4
//-- m_Var_Temp[0][curIndex] : mu_ {A,i} in the paper section 2.4
m_DistImg[idx_i + idx_j * obsWidth] = pow(pCur[idx_i + idx_j * obsWidth] - m_Mean[0][curIndex], (int)2);
for (int i = 0; i < f_Num; i++) {
m_DistImg_FG[i][idx_i + idx_j * obsWidth] = pow(pCur[idx_i + idx_j * obsWidth] - f_Mean[i], (int)2);
}
if (!pOutputImg.empty() && m_Age_Temp[0][curIndex] > 1) {
uchar valOldOut = m_DistImg[idx_i + idx_j * obsWidth] > VAR_THRESH_BG_DETERMINE * m_Var_Temp[0][curIndex] ? 255 : 0;
pOldOut[idx_i + idx_j * obsWidth] = valOldOut;
//---------------------------------------------------------------
//uchar valOut = m_DistImg[idx_i + idx_j * obsWidth] > m_DistImg_FG[idx_i + idx_j * obsWidth] ? 255 : 0;
//pOut[idx_i + idx_j * obsWidth] = valOut;
//---------------------------------------------------------------
/*float bgDist = m_DistImg[idx_i + idx_j * obsWidth] - m_Var_Temp[0][curIndex];
int count = 0;
for (int i = 0; i < f_Num; i++) {
if (bgDist < fgDist[i])
count++;
}
uchar valOut = (count < 2) ? 255 : 0;
pOut[idx_i + idx_j * obsWidth] = valOut;*/
//---------------------------------------------------------------
//uchar valOut = ((m_DistImg[idx_i + idx_j * obsWidth] > VAR_THRESH_BG_DETERMINE* m_Var_Temp[0][curIndex]) &&
// (m_DistImg_FG[0][idx_i + idx_j * obsWidth] < VAR_THRESH_BG_DETERMINE* f_Var[0]))? 255 : 0;
//pOut[idx_i + idx_j * obsWidth] = valOut;
//---------------------------------------------------------------
//int count = 0;
//for (int i = 0; i < f_Num; i++) {
// if (m_DistImg[idx_i + idx_j * obsWidth] < m_DistImg_FG[i][idx_i + idx_j * obsWidth])
// count++;
//}
//uchar valOut = (count > 1) ? 0 : 255;
//pOut[idx_i + idx_j * obsWidth] = valOut;
//---------------------------------------------------------------
uchar valOut = 0;
if (m_DistImg[idx_i + idx_j * obsWidth] > VAR_THRESH_BG_DETERMINE* m_Var_Temp[0][curIndex]) {
pCandidateFG[idx_i + idx_j * obsWidth] = 255;
for (int i = 0; i < f_Num / 2; i++) {
//if (m_DistImg_FG[i][idx_i + idx_j * obsWidth] < VAR_THRESH_FG_DETERMINE * f_Var[i])
if (m_DistImg_FG[i][idx_i + idx_j * obsWidth] < VAR_THRESH_FG_DETERMINE * m_Var_Temp[0][curIndex])
valOut = 255;
}
}
pOut[idx_i + idx_j * obsWidth] = valOut;
//---------------------------------------------------------------
//if (idx_base_i == 240 && idx_base_j == 240) {
////if (idx_base_i == 220 && idx_base_j == 180) {
// std::cout << "pCur: " << (int)pCur[idx_i + idx_j * obsWidth]
// << "\nm_Mean[0]: " << m_Mean[0][curIndex] << "\tm_Var_Temp[0]: " << m_Var_Temp[0][curIndex]
// << "\tm_DistImg = " << m_DistImg[idx_i + idx_j * obsWidth] << "\tvalOut: " << (int)valOut
// << "\n-------------------------------------------" << std::endl;
// //for (int i = 0; i < f_Num; i++) {
// // std::cout
// // << "fmean[" << i << "]: " << f_Mean[i] << "\tf_Var[" << i << "]: " << f_Var[i]
// // << "\tf_Age[" << i << "]: " << f_Age[i] << "\tf_Weight[" << i << "]: " << f_Weight[i] << std::endl
// // << "m_DistImg_FG[" << i << "]: " << m_DistImg_FG[i][idx_i + idx_j * obsWidth]
// // << "\n-------------------------------------------" << std::endl;
// //}
// std::cout << "\n=================================================================\n";
//
//}
}
obs_var[nMatchIdx] = MAX(obs_var[nMatchIdx], pixelDist);
//-- debug
//----------------------------------------------
//if (idx_base_i == 240 && idx_base_j == 240) {
//if (idx_base_i == 220 && idx_base_j == 180) {
//cv::Mat testBlocks = prevGray.clone();
//cv::Rect r = cv::Rect(idx_base_i, idx_base_j, BLOCK_SIZE, BLOCK_SIZE);
//cv::rectangle(testBlocks, r, cv::Scalar(255), 3);
//cv::imshow("testBlocks", testBlocks);
//std::cout << "m_DistImg = " << m_DistImg[idx_i + idx_j * obsWidth] << "\tm_Var_Temp[0]: " << m_Var_Temp[0][curIndex] <<
// "\tm_Mean[0]: " << m_Mean[0][curIndex] << "\tpCur: " << (int)pCur[idx_i + idx_j * obsWidth] << "\tpOut: " << (int)pOut[idx_i + idx_j * obsWidth] <<
// "\tm_DistImg_FG = " << m_DistImg_FG[idx_i + idx_j * obsWidth] <<
// "\n-------------------------------------------" << std::endl;
//outFile << "m_DistImg = " << m_DistImg[idx_i + idx_j * obsWidth] << "\tm_Var_Temp[0]: " << m_Var_Temp[0][curIndex] <<
// "\tm_Mean[0]: " << m_Mean[0][curIndex] << "\tpCur: " << (int)pCur[idx_i + idx_j * obsWidth] << "\tpOut: " << (int)pOut[idx_i + idx_j * obsWidth] <<
// "\n-------------------------------------------" << std::endl;
//}
//----------------------------------------------
//----------------------------------------------
}
}
for (int m = 0; m < NUM_MODELS; ++m) {
if (nElemCnt[m] > 0) {
float age = m_Age_Temp[m][curIndex];
float alpha = age / (age + 1.0);
//-- update with this variance
if (age == 0) {
m_Var[m][curIndex] = MAX(obs_var[m], INIT_BG_VAR);
}
else {
float alpha_var = alpha; //MIN(alpha, 1.0 - MIN_NEW_VAR_OBS_PORTION);
m_Var[m][curIndex] = alpha_var * m_Var_Temp[m][curIndex] + (1.0 - alpha_var) * obs_var[m];
m_Var[m][curIndex] = MAX(m_Var[m][curIndex], MIN_BG_VAR);
}
//-- Update Age
m_Age[m][curIndex] = MIN(m_Age_Temp[m][curIndex] + 1.0, MAX_BG_AGE);
}
else {
m_Var[m][curIndex] = m_Var_Temp[m][curIndex];
m_Age[m][curIndex] = m_Age_Temp[m][curIndex];
}
}
}
}
//std::cout << "\nvalout: " << (int)pOutputImg.at<uchar>(240, 240);
}
//------------------------
//-- KLT
//------------------------
void DCFG::KLT(cv::Mat& imgGray) {
#if DFCG_DEBUG
cv::RNG rng(12345);
colorFrame.copyTo(testImg);
#endif
if (!points[0].empty())
{
calcOpticalFlowPyrLK(prevGray, imgGray, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.001);
size_t i;
for (i = count = 0; i < points[1].size(); i++)
{
if (!status[i])
continue;
nMatch[count++] = i;
#if DFCG_DEBUG
cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
circle(testImg, points[0][i], 3, color, -1, 8);
circle(testImg, points[1][i], 3, color, -1, 8);
cv::arrowedLine(testImg, points[0][i], points[1][i], color, 1);
#endif
}
}
//---------------------------------------------
//estimate Optical Flow
//cv::Mat flow, ang, mag, hsv, rgb;
//std::vector<cv::Mat> flowChannels(2);
//cv::calcOpticalFlowFarneback(prevGray, imgGray, flow, 0.5, 3, 21, 20, 5, 1.1,0);
//cv::add(flow, cumulativeFlow, cumulativeFlow);
//flowSeq.push_back(flow.clone());
//if (flowSeq.size() > 5) {
// cv::subtract(cumulativeFlow, flowSeq.front(), cumulativeFlow);
// flowSeq.pop_front();
//}
//frameSeq.push_back(imgGray.clone());
//if (frameSeq.size() > 5)
// frameSeq.pop_front();
//cv::split(cumulativeFlow, flowChannels);
//cv::cartToPolar(flowChannels[0], flowChannels[1], mag, ang, false);
//cv::Mat sat = cv::Mat(imgGray.rows, imgGray.cols, CV_8UC1, 255.0);
//cv::normalize(mag, mag, 0, 255, cv::NORM_MINMAX, -1, cv::noArray());
//ang = ang * 180 / CV_PI / 2;
////double minVal, maxVal;
////cv::minMaxLoc(ang, &minVal, &maxVal); //find minimum and maximum intensities
////ang.convertTo(ang, CV_8U, 255.0 / (maxVal - minVal), -minVal);
//mag.convertTo(mag, CV_8U);
//ang.convertTo(ang, CV_8U);
//std::vector<cv::Mat> hsvChannels;
//hsvChannels.push_back(ang);
//hsvChannels.push_back(sat);
//hsvChannels.push_back(mag);
//cv::merge(hsvChannels, hsv);
//cv::cvtColor(hsv, rgb, CV_HSV2BGR);
//cv::imshow("rgb", rgb);
//cv::imshow("h", hsvChannels[0]);
//cv::imshow("s", hsvChannels[1]);
//cv::imshow("v", hsvChannels[2]);
//for (int y = 0; y < prevGray.rows; y ++) {
// for (int x = 0; x < prevGray.cols; x ++) {
// //const cv::Point2f flowatxy = cumulativeFlow.at<cv::Point2f>(y, x);
// //ang.at<float>(y,x) = (atan2(flowatxy.y, flowatxy.x) + CV_PI) * (180 / (float)CV_PI);
// std::cout << (int)ang.at<uchar>(y, x) << " ";
// //mag.at<float>(y, x) = sqrt(pow(flowatxy.x, 2) + pow(flowatxy.y, 2)) * 10;
// std::cout << (int)mag.at<uchar>(y, x) << std::endl;
// }
//}
//ang.convertTo(ang, CV_8UC1);
//mag.convertTo(mag, CV_8UC1);
//cv::imshow("ang", ang);
//cv::imshow("mag", mag);
//-------------------- draw optical flow vectors
//cv::Mat motionSeg = colorFrame.clone();
//for (int y = 0; y < prevGray.rows; y += 10) {
// for (int x = 0; x < prevGray.cols; x += 10) {
// // get the flow from y, x position * 10 for better visibility
// const cv::Point2f flowatxy = cumulativeFlow.at<cv::Point2f>(y, x) * 10;
// // draw line at flow direction
// cv::line(motionSeg, cv::Point(x, y), cv::Point(cvRound(x + flowatxy.x), cvRound(y + flowatxy.y)), cv::Scalar(25 * (int)flowatxy.x, 25 * (int)flowatxy.y, 0));
// // draw initial point
// cv::circle(motionSeg, cv::Point(x, y), 1, cv::Scalar(0, 0, 0), -1);
// }
//}
//// draw the results
//cv::namedWindow("motionSeg", cv::WINDOW_AUTOSIZE);