-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSCBT_Edge.cpp
More file actions
2073 lines (1630 loc) · 76.6 KB
/
SCBT_Edge.cpp
File metadata and controls
2073 lines (1630 loc) · 76.6 KB
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <opencv2/opencv.hpp>
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/SVD>
#include <eigen3/Eigen/Dense>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
#include "Timer.h"
//#include "LS.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <map>
#include <ctime>
#include <sstream>
//#include "Timer.h"
#include "EdgeMap.h"
#include "EDLib.h"
#include "CommonFunctions.h"
#define DivideErrThre_e 1.4//edge breaking threshold
#define DivideErrThre_m 5
#define BufferThre 20//Bowl(30) GarbageBin(30) buffer area width
#define edgeLenThre 12//Bowl(15) GarbageBin(25)//edges longer than 12 pixels are saved
#define areaCnst 0.9//Bowl(0.9) GarbageBin(0.9) area similarity threshold
#define lenCnst 0.9//Bowl(0.9) GarbageBin(0.9) length similarity threshold
#define ddOverLen 0.7 //Bowl(0.7) GarbageBin(0.7) MarkCup(1) BookStnd(1) MarkCupContour 0.7
#define searchLineThre 1.0//possible threhosld: GarbagBin(0.3)
//(w_len*length+w_dd*distDiff)/length = w_len + w_dd*distDiff/length
#define w_dd 1.0
#define w_len 1.0
using namespace cv;
using namespace std;
//using namespace Eigen;
struct lessXY{
bool operator()(const int* lhs, const int*rhs) const{
return (lhs[0] == rhs[0]) ? (lhs[1] < rhs[1]) : (lhs[0] < rhs[0]);
}
};
struct EdgeFragment{//divided edge fragments from detected edge segments
//index
int seg_id;//edge fragements belong to the seg_id th detected segment
int start_idx;// its start index in the seg_id th detected segment
int end_idx;// its end index in the seg_id th detected segment
};
struct GraphEdge{//edge constructed by detected edge fragments
EdgeFragment EF;
Pixel sp;
Pixel ep;
float length;
float distDiff;
};
struct GraphEdgeDT{//edge constructed generated by delaunay triangulation
int s_se;//start or end point of the GraphEdge
int e_se;
float length;
float distDiff;
};
struct Graph_GE_GEDT{
vector<GraphEdge> graphEdge;
vector<GraphEdgeDT> graphEdgeDT;
std::map<int*, int, lessXY> mapGEDT;
};
struct Graph_GEDT{//store the GEDT part of the graph optimization result
//int idx;//indicates the prior EF index in EF vector
Pixel sp;
Pixel ep;
};
struct Graph_RSLT{
vector<EdgeFragment> EFs;
vector<Graph_GEDT> GEDTs;
float area;
float length;
};
struct GraphEdgeUniform{
int seg_id1;
int pixel_idx1;
int seg_id2;
int pixel_idx2;
float length;
float distDiff;
};
double totalTime = 0;
int video_proc = 0;//video_proc: 0 read from webcam, 1 read from video file
int k, pk=1;
int pki=1;
bool tracking_flg = false;
vector<Point> points; //contour of pre-shape
Point pt_lbd, pt_mv;
int mbd = 0;//flag of right button click
double ave_fps = 0;//average fps
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( event == EVENT_LBUTTONDOWN )
{
//Point* pt = (Point*) userdata;
if(!tracking_flg){
if(mbd == 0){
pt_lbd.x = x;
pt_lbd.y = y;
points.push_back(pt_lbd);
}
}
//cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_RBUTTONDOWN )
{
//cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MBUTTONDOWN )
{
mbd = 1;//stop polygon selection
//cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MOUSEMOVE )
{
if(!tracking_flg){
pt_mv.x = x;
pt_mv.y = y;
}
}
return;
}
//measure the least square error of line fitting
int EdgeBreakFit(float xyh[3][4]){
/*
//normalize coordinates of pixels
VectorXf centroid = xyh.rowwise().mean();
MatrixXf trslt_xyh = xyh.colwise() - centroid;
double mean_dist = (trslt_xyh.array() * trslt_xyh.array()).colwise().sum().sqrt().mean();
double norm_scale = sqrt(2) / mean_dist;
MatrixXf norm_xyh;
norm_xyh = trslt_xyh*norm_scale;
//save inverse of normalizing transformation matrix
MatrixXf inv_norm_mat(3,3);
inv_norm_mat.setIdentity();
inv_norm_mat(0, 0) = inv_norm_mat(1, 1) = (1.0 / norm_scale);edgeLenThre
inv_norm_mat(0, 2) = centroid(0);
inv_norm_mat(1, 2) = centroid(1);
MatrixXf A;
A = MatrixXf::Ones(3, norm_xyh.cols());
A.row(0) = norm_xyh.row(0);
A.row(1) = norm_xyh.row(1);
//JacobiSVD<MatrixXf> svd(A.transpose(), ComputeFullU | ComputeFullV);
//MatrixXf V = svd.matrixV();
*/
//Timer timer;
//timer.Start();
//Vector3f v1(xyh[0][0],xyh[1][0],1);
//Vector3f v2(xyh[0][2],xyh[1][2],1);
//Vector3f V = v1.cross(v2);
//MatrixXf abcX(1,1);
float abcXe = 0;//end point test
float abcXm = 0;//middle point test
//Vector3f edpt(xyh[0][3],xyh[1][3],1);
float a=0, b=0, c=0;
a = xyh[1][0]-xyh[1][2];
b = xyh[0][2]-xyh[0][0];
c = xyh[0][0]*xyh[1][2]-xyh[1][0]*xyh[0][2];
//timer.Stop();
//totalTime = totalTime + timer.ElapsedTime();
//cout<<" time of each leastSquareLineFit "<<totalTime<<endl;
//Timer timer;
//timer.Start();
//abcX = V.transpose()*edpt;//(1*1);
abcXe = a*xyh[0][3]+b*xyh[1][3]+c;
abcXm = a*xyh[0][1]+b*xyh[1][1]+c;
//timer.Stop();
//cout<<" time of each leastSquareLineFit "<<timer.ElapsedTime()<<endl;
//float err = (abcX.cwiseAbs().sum())/sqrt(pow(V(0),2)+pow(V(1),2));
//float err = (abcX.cwiseAbs().sum())/(xyh.cols()*sqrt(pow(V(0),2)+pow(V(1),2)));
float erre = abs(abcXe)/sqrt(pow(a,2)+pow(b,2));
float errm = abs(abcXm)/sqrt(pow(a,2)+pow(b,2));
//cout<<" abc: "<<a<<" "<<b<<" "<<c;
//cout<<" xe: "<<xyh[0][3]<<" "<<xyh[1][3];
//cout<<" xm: "<<xyh[0][1]<<" "<<xyh[1][1];
//cout<<" erre: "<<erre;
//cout<<" errm: "<<errm<<endl;
//float *err;
//err = new float[2];
//err[0] = erre;
//err[1] = errm;
if(erre<DivideErrThre_e && errm<DivideErrThre_m){
return 1;
}else{
return 0;
}
//return err;
}
//divide an edge segment into fragements
/*vector<EdgeFragment> DivideIntoEdgeFragements(EdgeMap *map){
vector<EdgeFragment> dividedEF;//dmapGEDTivided Edge Fragments
for (int i = 0; i < map->noSegments; i++){
int iEgStrt = 0;
int newEdge = 1;// start a new small edge segment searching
//vector<Pixel> Pixel_tmp;
EdgeFragment EF_tmp;
//MatrixXf pts;//3*4
float PT[3][4];
if(map->segments[i].noPixels < 5){
EF_tmp.seg_id = i;
EF_tmp.start_idx = iEgStrt;
EF_tmp.end_idx = map->segments[i].noPixels-1;
dividedEF.push_back(EF_tmp);
}else{
//every two new pixels will be tested by line fitframe_edgeting
for (int iEgEnd = 4; iEgEnd < map->segments[i].noPixels;){
if(newEdge){//start a new small edge segment searching
EF_tmp.seg_id = i;
EF_tmp.start_idx = iEgStrt;
//add the start pixel
//pts = MatrixXf::OmapGEDTnes(3, 4);//start pixel, middle pixel, the thir/*d last pixel and the last pixel
//pts(0,0) = map->segments[i].pixels[iEgStrt].r;
//pts(1,0) = map->segments[i].pixels[iEgStrt].c;
//pts(2,0) = 1;
PT[0][0] = map->segments[i].pixels[iEgStrt].r;
PT[1][0] = map->segments[i].pixels[iEgStrt].c;
PT[2][0] = 1;
}
PT[0][1] = map->segments[i].pixels[(iEgEnd-iEgStrt)/2].r;
PT[1][1] = map->segments[i].pixels[(iEgEnd-iEgStrt)/2].c;
PT[2][1] = 1;
PT[0][2] = map->segments[i].pixels[iEgEnd-2].r;
PT[1][2] = map->segments[i].pixels[iEgEnd-2].c;
PT[2][2] = 1;
PT[0][3] = map->segments[i].pixels[iEgEnd].r;
PT[1][3] = map->segments[i].pixels[iEgEnd].c;
PT[2][3] = 1;
//add the middle pixel
//pts(0,1) = map->segments[i].pixels[(iEgEnd-iEgStrt)/2].r;
//pts(1,1) = map->segments[i].pixels[(iEgEnd-iEgStrt)/2].c;
//pts(2,1) = 1;
//pts(0,2) = map->segments[i].pixels[iEgEnd-2].r;
//pts(1,2) = map->segments[i].pixels[iEgEnd-2].c;
//pts(2,2) = 1;
//pts(0,3) = map->segments[i].pixels[iEgEnd].r;
//pts(1,3) = map->segments[i].pixels[iEgEnd].c;
//pts(2,3) = 1;
if(EdgeBreakFit(PT)<DivideErrThre){
if(iEgEnd < map->segments[i].noPixels-2){
newEdge = 0;
iEgEnd +=2;
}else{
newEdge = 1;
EF_tmp.end_idx = map->segments[i].noPixels-1;
dividedEF.push_back(EF_tmp);// find the last small edge segment
break;
}
nlines
}else{
newEdge = 1;//start a new small edge
EF_tmp.end_idx = iEgEnd - 2;
dividedEF.push_back(EF_tmp);// find a new small edge segment
iEgStrt = iEgEnd-1;
EF_tmp.start_idx = iEgStrt;
if(map->segments[i].noPixels-iEgStrt < 5){// there are less than 5 remaining pixels in this edge segment
EF_tmp.end_idx = map->segments[i].noPixels-1;
dividedEF.push_back(EF_tmp);
break;
}else{// there are less than 3 remaining pixels in this edge segment and start a new small edge segment searching
iEgEnd = iEgStrt + 4;
}
}//end if(LeastSquaresLineFit < DividedErrThre){} else{}
} //end-for map->noSegments
}//if(map->segments[i].noPixels < 5)
} //end-for map->segments[i]
return dividedEF;
}
*/
vector<EdgeFragment> EdgeFilterBreak(EdgeMap *map, Mat Dist){
vector<EdgeFragment> EdgeFilters;//divided Edge Fragments
//cout<<" Debug----------------3.1 "<<endl;
//edge segments filtering according to buffer region of the prior shape
for(int i = 0; i < map->noSegments; i++){
int iEgStrt = -1;
EdgeFragment EdgeFilter;
//float PT[3][4];
for (int j = 0; j < map->segments[i].noPixels; j++){
if(Dist.at<float>(map->segments[i].pixels[j].r,map->segments[i].pixels[j].c) < BufferThre){
if(iEgStrt==-1){
iEgStrt = j;
}else{
if(j == (map->segments[i].noPixels-1)){
EdgeFilter.seg_id = i;
EdgeFilter.start_idx = iEgStrt;
EdgeFilter.end_idx = j;
EdgeFilters.push_back(EdgeFilter);
iEgStrt = -1;
}
}
}else{
if(iEgStrt!=-1){
EdgeFilter.seg_id = i;
EdgeFilter.start_idx = iEgStrt;
EdgeFilter.end_idx = j - 1;
EdgeFilters.push_back(EdgeFilter);
iEgStrt = -1;
}
}
}//for(int j)mapGEDT
}//for(int i)
//output for paper editing===========================
/*
int rc, gc, bc;
int lowest=100, highest=255;
int range=(highest-lowest)+1;
Mat all_edges;
all_edges = Mat::zeros(Dist.size().height, Dist.size().width, CV_8UC3);////frame.clone();
for(int i = 0; i<EdgeFilters.size(); i++){
int tmpi = EdgeFilters[i].seg_id;
rc = lowest+int(rand()%range);
gc = lowest+int(rand()%range);
bc = lowest+int(rand()%range);
for( int j = EdgeFilters[i].start_idx; j<EdgeFilters[i].end_idx + 1;j++){
int r = map->segments[tmpi].pixels[j].r;
int c = map->segments[tmpi].pixels[j].c;
Vec3b & color = all_edges.at<Vec3b>(r,c);
color[0] = bc;
color[1] = gc;
color[2] = rc;
}
}
imwrite("Buffer_edges.png",all_edges);
cvWaitKey(0);
*/ //output for paper editing--------------------------
//cout<<" Debug----------------3.2 "<<endl;
//--------------------------------------------------------------------------
//edge segments breaking according to "turning distance"
vector<EdgeFragment> dividedEF;//divided Edge Fragments
for(int i = 0; i < EdgeFilters.size(); i++){
int newEdge = 1;// start a new small edge segment searching
int iEgStrt = EdgeFilters[i].start_idx;
EdgeFragment EF_tmp;
//EF_tmp.seg_id = EdgeFilters[i].seg_id;
float PT[3][4];
int noPixels = EdgeFilters[i].end_idx-EdgeFilters[i].start_idx+1;
if(noPixels < 5 && noPixels > 1){
/*EF_tmp.seg_id = EdgeFilters[i].seg_id;
EF_tmp.start_idx = iEgStrt;
EF_tmp.end_idx = EdgeFilters[i].end_idx;noSegments
dividedEF.push_back(EF_tmp);*/
}else{
//every two new pixels will be tested by line fitting
for (int iEgEnd = iEgStrt + 4; iEgEnd < EdgeFilters[i].end_idx + 1;){
if(newEdge){//start a new small edge segment searching
EF_tmp.seg_id = EdgeFilters[i].seg_id;
EF_tmp.start_idx = iEgStrt;
PT[0][0] = map->segments[EdgeFilters[i].seg_id].pixels[iEgStrt].r;
PT[1][0] = map->segments[EdgeFilters[i].seg_id].pixels[iEgStrt].c;
PT[2][0] = 1;
}
PT[0][1] = map->segments[EdgeFilters[i].seg_id].pixels[(iEgEnd+iEgStrt)/2].r;
PT[1][1] = map->segments[EdgeFilters[i].seg_id].pixels[(iEgEnd+iEgStrt)/2].c;
PT[2][1] = 1;
PT[0][2] = map->segments[EdgeFilters[i].seg_id].pixels[iEgEnd-2].r;
PT[1][2] = map->segments[EdgeFilters[i].seg_id].pixels[iEgEnd-2].c;
PT[2][2] = 1;
PT[0][3] = map->segments[EdgeFilters[i].seg_id].pixels[iEgEnd].r;
PT[1][3] = map->segments[EdgeFilters[i].seg_id].pixels[iEgEnd].c;
PT[2][3] = 1;
if(EdgeBreakFit(PT)==1/*<DivideErrThre_e*/){
if(iEgEnd < EdgeFilters[i].end_idx - 1 ){
newEdge = 0;
iEgEnd +=2;
}else{
newEdge = 1;
EF_tmp.end_idx = EdgeFilters[i].end_idx;
if((EF_tmp.end_idx-EF_tmp.start_idx+1)>edgeLenThre/*EF_tmp.end_idx!=EF_tmp.start_idx*/){//
dividedEF.push_back(EF_tmp);// find the last small edge segment
}
break;
}
}else{
newEdge = 1;//start a new small edge
EF_tmp.end_idx = iEgEnd - 2;
if((EF_tmp.end_idx-EF_tmp.start_idx+1)>edgeLenThre){//
dividedEF.push_back(EF_tmp);// find a new small edge segment
}//
iEgStrt = iEgEnd-1;
EF_tmp.start_idx = iEgStrt;
if(EdgeFilters[i].end_idx-iEgStrt < 4){// there are less than 5 remaining pixels in this edge segment
EF_tmp.end_idx = EdgeFilters[i].end_idx;
if((EF_tmp.end_idx-EF_tmp.start_idx+1)>edgeLenThre/*EF_tmp.end_idx!=EF_tmp.start_idx*/){
dividedEF.push_back(EF_tmp);
}
break;
}else{// there are less than 3 remaining pixels in this edge segment and start a new small edge segment searching
iEgEnd = iEgStrt + 4;
}
}//end if(LeastSquaresLineFit < DividedErrThre){} else{}
}//for(int iEgEnd)
}//if(noPixels < 5)
}//for(int i)
//output for paper editing===========================
/*
Mat broken_edges;
broken_edges = Mat::zeros(Dist.size().height, Dist.size().width, CV_8UC3);////frame.clone();
for(int i = 0; i<dividedEF.size(); i++){
int tmpi = dividedEF[i].seg_id;
rc = lowest+int(rand()%range);
gc = lowest+int(rand()%range);
bc = lowest+int(rand()%range);
for( int j = dividedEF[i].start_idx; j<dividedEF[i].end_idx + 1;j++){
int r = map->segments[tmpi].pixels[j].r;
int c = map->segments[tmpi].pixels[j].c;
Vec3b & color = broken_edges.at<Vec3b>(r,c);
color[0] = bc;
color[1] = gc;
color[2] = rc;
}
}
imwrite("broken_edges.png",broken_edges);
cvWaitKey(0);
*/ //output for paper editing--------------------------
//cout<<" Debug----------------3.3 "<<endl;
//=============handle overlapping end points=================
Mat dst;//test overlapping pixels
dst = Mat::zeros(Dist.size().height,Dist.size().width,CV_8UC1);
//cout<<" Debug----------------3.4 "<<endl;
for(int i = 0; i < dividedEF.size(); i++){
//cout<<i<<" of "<<dividedEF.size()<<endl;
int tmpID = dividedEF[i].seg_id;
int sidx = dividedEF[i].start_idx;
int eidx = dividedEF[i].end_idx;
//cout<<" Debug----------------3.5 "<<endl;
int rs = map->segments[tmpID].pixels[sidx].r;
int cs = map->segments[tmpID].pixels[sidx].c;
//cout<<" Debug----------------3.6 "<<endl;
if(dst.at<unsigned char>(rs,cs)==0){
dst.at<unsigned char>(rs,cs)=1;
}else{
while(dst.at<unsigned char>(rs,cs)==1 && sidx<eidx){
sidx++;
rs = map->segments[tmpID].pixels[sidx].r;
cs = map->segments[tmpID].pixels[sidx].c;
}//while
dividedEF[i].start_idx = sidx;
dst.at<unsigned char>(rs,cs)=1;
}
int re = map->segments[tmpID].pixels[eidx].r;
int ce = map->segments[tmpID].pixels[eidx].c;
//cout<<" Debug----------------3.7 "<<endl;
if(dst.at<unsigned char>(re,ce)==0){
dst.at<unsigned char>(re,ce)=1;
}else{
while(dst.at<unsigned char>(re,ce)==1 && eidx>sidx){
eidx--;
re = map->segments[tmpID].pixels[eidx].r;
ce = map->segments[tmpID].pixels[eidx].c;
}//while
dividedEF[i].end_idx = eidx;
dst.at<unsigned char>(re,ce)=1;
}//else
//cout<<" Debug----------------3.8 "<<endl;
}//for
//cout<<" Debug----------------3.9 "<<endl;
return dividedEF;
}
Graph_GE_GEDT GraphConstruct(EdgeMap *map, vector<EdgeFragment> edgeFragments, Mat Dist,Mat frame_edge){
//transform the edge fragments into graph edges and compute their weights: length and distance differences
//map: detected edges by Edge Drawing, edgeFragments: filtered and broken edge fragments, Dist: distance tranform of the prior shape
//Timer timer_solid;
//timer_solid.Start();
Graph_GE_GEDT graph_GE_GEDT;//store graph edge created by detected edges and Delaunay Triangulation
vector<GraphEdge> graphEdges;
//end points of edges for create delaunay triangulation
vector<Point2f> points;
//create a map between points and their indices
std::map<int*, int, lessXY> mappts;
int* spoint;
int* epoint;
//cout<<" =======================solid edges======================"<<endl;
int num_EF = 0;
for(int i = 0; i < edgeFragments.size(); i++){
//cout<<" edge len: "<<(edgeFragments[i].end_idx - edgeFragments[i].start_idx + 1)<<endl;
//if((edgeFragments[i].end_idx - edgeFragments[i].start_idx + 1) > edgeLenThre){
int tmp_id = edgeFragments[i].seg_id;
float tmp_l = 0;//temp length
float tmp_dd = 0;//temp distance difference
for(int j = edgeFragments[i].start_idx; j < edgeFragments[i].end_idx; j++){
int rj = map->segments[tmp_id].pixels[j].r;
int cj = map->segments[tmp_id].pixels[j].c;
int rj1 = map->segments[tmp_id].pixels[j+1].r;
int cj1 = map->segments[tmp_id].pixels[j+1].c;
tmp_dd = tmp_dd + abs(Dist.at<float>(rj,cj)-Dist.at<float>(rj1,cj1));
if((abs(rj-rj1)+abs(cj-cj1)) == 2){//compute the length
tmp_l = tmp_l + sqrt(2);
}else{
tmp_l = tmp_l + 1;
}
}//for(int j)
if(tmp_dd/tmp_l < ddOverLen){
GraphEdge tmp_GE;
tmp_GE.EF = edgeFragments[i];
tmp_GE.sp = map->segments[tmp_id].pixels[edgeFragments[i].start_idx];
tmp_GE.ep = map->segments[tmp_id].pixels[edgeFragments[i].end_idx];
//get end points for delaunay triangulation
points.push_back(Point2f(float(tmp_GE.sp.c),float(tmp_GE.sp.r)));
points.push_back(Point2f(float(tmp_GE.ep.c),float(tmp_GE.ep.r)));
//cout<<" i: "<<i<<endl;
//map of i and end point
spoint = new int[2];
spoint[0] = tmp_GE.sp.c;//x
spoint[1] = tmp_GE.sp.r;//y
mappts[spoint] = 2*num_EF;//2*i;
//cout<<"endpt: "<<2*i<<" "<<spoint[0]<<" "<<spoint[1]<<endl;
epoint = new int[2];
epoint[0] = tmp_GE.ep.c;
epoint[1] = tmp_GE.ep.r;
mappts[epoint] = 2*num_EF+1;//2*i+1;
//cout<<"endpt: "<<2*i+1<<" "<<epoint[0]<<" "<<epoint[1]<<endl;
//cout<<" i: "<<i<<" 2*i: "<<2*i<<" 2*i+1: "<<2*i+1<<endl;
/* float tmp_l = 0;//temp length
float tmp_dd = 0;//temp distance difference
for(int j = edgeFragments[i].start_idx; j < edgeFragments[i].end_idx; j++){
int rj = map->segments[tmp_id].pixels[j].r;
int cj = map->segments[tmp_id].pixels[j].c;
int rj1 = map->segments[tmp_id].pixels[j+1].r;
int cj1 = map->segments[tmp_id].pixels[j+1].c;
tmp_dd = tmp_dd + abs(Dist.at<float>(rj,cj)-Dist.at<float>(rj1,cj1));
if((abs(rj-rj1)+abs(cj-cj1)) == 2){//compute the length
tmp_l = tmp_l + sqrt(2);
}else{
tmp_l = tmp_l + 1;
}
}//for(int j)*/
tmp_GE.length = tmp_l;
tmp_GE.distDiff = tmp_dd;
graphEdges.push_back(tmp_GE);
num_EF++;
}
//}
}//for(int i)
//cout<<" mappts.size(): "<<mappts.size()<<endl;
//timer_solid.Stop();
//cout<<" Graph Construction Solid: "<<timer_solid.ElapsedTime()<<endl;
//----------------------------------------------------------------------
//Use Delaunay Triangulation to generate gap filling line edge fragments
//Timer timer_DT;
//timer_DT.Start();
Rect rect(0,0,Dist.size().width,Dist.size().height);
Subdiv2D subdiv(rect);
subdiv.insert(points);
vector<Vec4f> edgeList;
subdiv.getEdgeList(edgeList);//edgeList[i]: sp.x,sp.y,epx,epy
//timer_DT.Stop();
//cout<<" Delaunay Triangualtion: "<<timer_DT.ElapsedTime()<<endl;
Timer timer_dashed;
timer_dashed.Start();
int width = Dist.size().width;
int height = Dist.size().height;
vector<GraphEdgeDT> graphEdgesDT;//save edges generated by delaunay triangulation
std::map<int*, int, lessXY> mapGEDT;//map nodes id to index, given node id retrive the corresponding edge id
int *nid;
//cout<<" points.size: "<<points.size()<<" mappts size: "<<mappts.size()<<endl;
int* p1;
int* p2;
//cout<<" ==============================dashed edges======================="<<endl;
//Mat frame_DT;//debugging-----------------
for(int i = 0; i < edgeList.size(); i++){
//cout<<" i "<<i<<" size(): "<<edgeList.size()<<endl;
//frame_DT = frame_edge.clone();//debugging--------------
GraphEdgeDT tmp_GEDT;
p1 = new int[2];
p2 = new int[2];
p1[0] = int(edgeList[i][0]);//x
p1[1] = int(edgeList[i][1]);//y
p2[0] = int(edgeList[i][2]);
p2[1] = int(edgeList[i][3]);
if(mappts[p1]/2 != mappts[p2]/2){
tmp_GEDT.s_se = mappts[p1];
tmp_GEDT.e_se = mappts[p2];
nid = new int[2];
if(tmp_GEDT.s_se < tmp_GEDT.e_se){
nid[0] = tmp_GEDT.s_se;
nid[1] = tmp_GEDT.e_se;
}else{
nid[0] = tmp_GEDT.e_se;
nid[1] = tmp_GEDT.s_se;
}
mapGEDT[nid] = graphEdgesDT.size();
tmp_GEDT.length = distance(float(points[tmp_GEDT.s_se].x),float(points[tmp_GEDT.s_se].y),
float(points[tmp_GEDT.e_se].x),float(points[tmp_GEDT.e_se].y));
LineIterator it(Dist, points[tmp_GEDT.s_se], points[tmp_GEDT.e_se], 8);
float tmp_distDiff = 0;
for(int l = 0; l < it.count - 1; l++){
Point pt_tmp1 = it.pos();
it++;
Point pt_tmp2 = it.pos();
tmp_distDiff = tmp_distDiff + abs(Dist.at<float>(pt_tmp1.y,pt_tmp1.x) - Dist.at<float>(pt_tmp2.y,pt_tmp2.x));
}
tmp_GEDT.distDiff = tmp_distDiff;
graphEdgesDT.push_back(tmp_GEDT);
}
}//for(int i)
graph_GE_GEDT.graphEdge = graphEdges;
graph_GE_GEDT.graphEdgeDT = graphEdgesDT;
graph_GE_GEDT.mapGEDT = mapGEDT;
//timer_dashed.Stop();
//cout<<" Dashed Edges: "<<timer_dashed.ElapsedTime()<<endl;
return graph_GE_GEDT;
}
//Graph_RSLT
Graph_RSLT GraphOptimize(EdgeMap *map, Graph_GE_GEDT graph_GE_GEDT,float areaPrior,float lenPrior, Mat frame_edge){//optimization
//=================================Debugging==================================
//test weight of each edgeedgeLenThre
/* Mat frame_wef,frame_wdt;
frame_wef = frame_edge.clone();
for(int i = 0; i< graph_GE_GEDT.graphEdge.size(); i++){
for(int tt = graph_GE_GEDT.graphEdge[i].EF.start_idx; tt < graph_GE_GEDT.graphEdge[i].EF.end_idx + 1; tt++){
int tmpi = graph_GE_GEDT.graphEdge[i].EF.seg_id;
int r = map->segments[tmpi].pixels[tt].r;
int c = map->segments[tmpi].pixels[tt].c;
Vec3b & color = frame_wef.at<Vec3b>(r,c);
color[0] = 0;
color[1] = 0;
color[2] = 255;
}
cout<<"Edge len: "<<graph_GE_GEDT.graphEdge[i].length<<" distDiff: "<<graph_GE_GEDT.graphEdge[i].distDiff<<" r: "<<
graph_GE_GEDT.graphEdge[i].distDiff/graph_GE_GEDT.graphEdge[i].length<<" dd/r^2: "
<<graph_GE_GEDT.graphEdge[i].distDiff/(graph_GE_GEDT.graphEdge[i].length*graph_GE_GEDT.graphEdge[i].length)<<endl;
namedWindow("frame_wef",1);
imshow("frame_wef",frame_wef);
cvWaitKey(0);
}
for(int i = 0; i< graph_GE_GEDT.graphEdgeDT.size(); i++){
frame_wdt = frame_edge.clone();
int s_se,e_se;
int r1,c1,r2,c2;
s_se = graph_GE_GEDT.graphEdgeDT[i].s_se;
e_se = graph_GE_GEDT.graphEdgeDT[i].e_se;
if(s_se%2==0){
r1 = graph_GE_GEDT.graphEdge[s_se/2].sp.r;
c1 = graph_GE_GEDT.graphEdge[s_se/2].sp.c;
}else{
r1 = graph_GE_GEDT.graphEdge[s_se/2].ep.r;
c1 = graph_GE_GEDT.graphEdge[s_se/2].ep.c;
}
if(e_se%2==0){
r2 = graph_GE_GEDT.graphEdge[e_se/2].sp.r;
c2 = graph_GE_GEDT.graphEdge[e_se/2].sp.c;
}else{
r2 = graph_GE_GEDT.graphEdge[e_se/2].ep.r;
c2 = graph_GE_GEDT.graphEdge[e_se/2].ep.c;
}
line(frame_wdt,Point(c1,r1),Point(c2,r2),Scalar(255,0,0),1,8,0);
cout<<"EdgeDT len: "<<graph_GE_GEDT.graphEdgeDT[i].length<<" distDiff: "<<graph_GE_GEDT.graphEdgeDT[i].distDiff<<" r: "<<
graph_GE_GEDT.graphEdgeDT[i].distDiff/graph_GE_GEDT.graphEdgeDT[i].length<<endl;
namedWindow("frame_wdt",1);
imshow("frame_wdt",frame_wdt);
cvWaitKey(0);
}
*/
//-----------------------------------Debugging------------------------------------
//Timer timer5;
//timer5.Start();
Graph_RSLT Optm_EF_GEDT;//results of graph optimization
//vector<GraphEdgeUniform> edge_a;//new
//vector<GraphEdgeUniform> edge_b;//new
//vector<GraphEdgeUniform> edge_a_tmp;//new
//vector<GraphEdgeUniform> edge_b_tmp;//new
vector<int> GE_idx;
vector<int> GEDT_idx;
vector<int> GE_idx_tmp;
vector<int> GEDT_idx_tmp;
int *nid;
typedef boost::property<boost::edge_weight_t, float> EdgeWeightProperty;
typedef boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS,
boost::no_property, EdgeWeightProperty > Graph;
typedef boost::graph_traits < Graph >::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits < Graph >::edge_descriptor edge_descriptor;
typedef std::pair<int, int> Edge;
//Create a graph
Graph g;
vector<Graph::vertex_descriptor> Vtx;
//add end points of graph_GE_GEDT.graphEdge as vertexes
//graph_GE_GEDT.graphEdge has two vertexes
for(int i = 0; i < graph_GE_GEDT.graphEdge.size(); i++){
Vtx.push_back(boost::add_vertex(g));
Vtx.push_back(boost::add_vertex(g));
}
//cout<<" finished Dijkstra: graph_GE_GEDT.graphEdge.size() "<< graph_GE_GEDT.graphEdge.size()<<endl;
//cout<<" finished Dijkstra: graph_GE_GEDT.graphEdgeDT.size() "<< graph_GE_GEDT.graphEdgeDT.size()<<endl;
//add edges to the Graph g
for(int i = 0; i < graph_GE_GEDT.graphEdge.size(); i++){
//>>>>>>>>>>>>>>>weights setting<<<<<<<<<<<<<
EdgeWeightProperty weighti(w_dd*graph_GE_GEDT.graphEdge[i].distDiff);
//EdgeWeightProperty weighti(w_dd*graph_GE_GEDT.graphEdge[i].distDiff/graph_GE_GEDT.graphEdge[i].length);
boost::add_edge(Vtx[2*i],Vtx[2*i+1],weighti,g);
}
for(int i = 0; i< graph_GE_GEDT.graphEdgeDT.size(); i++){
//>>>>>>>>>>>>>>>>>>>>weights setting<<<<<<<<<<<<<<<<<<<<
EdgeWeightProperty weighti(w_len*graph_GE_GEDT.graphEdgeDT[i].length+w_dd*graph_GE_GEDT.graphEdgeDT[i].distDiff);
//(w_len*length+w_dd*distDiff)/length
//EdgeWeightProperty weighti(w_len+w_dd*graph_GE_GEDT.graphEdgeDT[i].distDiff/graph_GE_GEDT.graphEdgeDT[i].length);//weights are gap length
boost::add_edge(Vtx[graph_GE_GEDT.graphEdgeDT[i].s_se],Vtx[graph_GE_GEDT.graphEdgeDT[i].e_se],weighti,g);
}
vector<int> cycle_id_a,cycle_id_b,cycle_id;//ID of vertex
//vector<int> cycle_id_a,cycle_id_b,cycle_id;//ID of vertex
vector<Point2f> cycle_points;
vector<Point2f> cycle_pts;//For compute area
float cycle_gap = 0;
float cycle_distDiff = 0;
float cycle_length = 0;
float cycle_area = 0;
float cycle_ratio = 0;
int num_Cadit = 0;
int BDSP_dubug = 0;
Mat frame_test;//Debugging
int CurEdgeDistDiff = 0;
for(int i = 0; i < graph_GE_GEDT.graphEdge.size(); i++){
if(graph_GE_GEDT.graphEdge[i].distDiff/graph_GE_GEDT.graphEdge[i].length < searchLineThre){
//cout<<" graphEdge.size(): "<<graph_GE_GEDT.graphEdge.size()<<endl;
//cout<<" i "<<i<<endl;
//set the edge weight of the current line to infinite
boost::remove_edge(Vtx[2*i], Vtx[2*i+1],g);
//>>>>>>>>>>>>>>>>>>>>weights setting<<<<<<<<<<<<<<<<<<<<
EdgeWeightProperty weightii(100000);
boost::add_edge(Vtx[2*i], Vtx[2*i+1], weightii, g);
//Bidirectional shortest path
//the shortest path of one direction
// Create things for Dijkstra
std::vector<vertex_descriptor> parents_a(boost::num_vertices(g)); // To store parents
std::vector<int> distances_a(boost::num_vertices(g)); // To store distances
// Compute shortest paths from v(2i) to all vertices, and store the output in parents and distances
boost::dijkstra_shortest_paths(g, Vtx[2*i], boost::predecessor_map(&parents_a[0]).distance_map(&distances_a[0]));
//the shortest path of the other direction
// Create things for Dijkstra
std::vector<vertex_descriptor> parents_b(boost::num_vertices(g)); // To store parents
std::vector<int> distances_b(boost::num_vertices(g)); // To store distances
// Compute shortest paths from v(2*i+1) to all vertices, and store the output in parents and distances
boost::dijkstra_shortest_paths(g, Vtx[2*i+1], boost::predecessor_map(&parents_b[0]).distance_map(&distances_b[0]));
//set the edge weight of the current line back to zero
boost::remove_edge(Vtx[2*i], Vtx[2*i+1],g);
//>>>>>>>>>>>>>>>>>>>>weights setting<<<<<<<<<<<<<<<<<<<<
EdgeWeightProperty weightiii(w_dd*graph_GE_GEDT.graphEdge[i].distDiff);
//EdgeWeightProperty weightiii(w_dd*graph_GE_GEDT.graphEdge[i].distDiff/graph_GE_GEDT.graphEdge[i].length);
boost::add_edge(Vtx[2*i], Vtx[2*i+1], weightiii, g);
cycle_distDiff += graph_GE_GEDT.graphEdge[i].distDiff;
cycle_length += graph_GE_GEDT.graphEdge[i].length;
CurEdgeDistDiff = graph_GE_GEDT.graphEdge[i].distDiff;
//--------------------Debugging------------------------
if(BDSP_dubug){
cout<<" draw current edge "<<endl;
cout<<2*i<<" "<<2*i+1<<" cycle_gap: "<<cycle_gap<<endl;
frame_test = frame_edge.clone();
for(int tt = graph_GE_GEDT.graphEdge[i].EF.start_idx; tt < graph_GE_GEDT.graphEdge[i].EF.end_idx + 1; tt++){
int tmpi = graph_GE_GEDT.graphEdge[i].EF.seg_id;
int r = map->segments[tmpi].pixels[tt].r;
int c = map->segments[tmpi].pixels[tt].c;
Vec3b & color = frame_test.at<Vec3b>(r,c);
color[0] = 0;
color[1] = 0;
color[2] = 255;
}
//namedWindow("Testing",1);
//imshow("Testing",frame_test);
//cvWaitKey(0);
}//--------------------Debugging-------------------------
for(int j = 0; j < 2*graph_GE_GEDT.graphEdge.size();){
int end = j;
int tmpa = end;
int tmpb = end;
if(end%2 == 0){
cycle_pts.push_back(Point2f(float(graph_GE_GEDT.graphEdge[tmpa/2].sp.c),float(graph_GE_GEDT.graphEdge[tmpa/2].sp.r)));
}
else if(end%2 == 1){
cycle_pts.push_back(Point2f(float(graph_GE_GEDT.graphEdge[tmpa/2].ep.c),float(graph_GE_GEDT.graphEdge[tmpa/2].ep.r)));
}
//-------------------Debugging------------------------
if(BDSP_dubug){
frame_test = frame_edge.clone();
}
//namedWindow("Testing",1);
///imshow("Testing",frame_test);
//cvWaitKey(0);
//-------------------Debugging------------------------
if(j!=2*i){
cycle_gap = w_dd*CurEdgeDistDiff + cycle_gap + distances_a[end] + distances_b[end];//store the gap length of the whole cycle
// cout<<" cycle_gap: +a+b "<<cycle_gap<<endl;
//-------------------Debugging------------------------
//draw the current edge
if(BDSP_dubug){
cout<<"==========i: "<<i<<endl;
cout<<"----------j: "<<j<<endl;