-
Notifications
You must be signed in to change notification settings - Fork 11
/
depthComp.cpp
4680 lines (2691 loc) · 323 KB
/
depthComp.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
// *****************************************************************************
/*
DepthComp : efficient depth filling appraoch built on segmentation
Requirements: depth image / segmented image
Implementation of DepthComp - Atapour-Abarghouei / Breckon, Proc. BMVC 2017.
Author : Amir Atapour-Abarghouei, [email protected]
Copyright (c) 2017 Engineering and Computing Sciences, Durham University
License : GPL - http://www.gnu.org/licenses/gpl.html
*********************************************************************************
*/
#include "depthComp.hpp"
// *****************************************************************************
DepthComp::DepthComp(){
case1 = 0;
case2 = 0;
case3 = 0;
case4 = 0;
case5 = 0;
case6 = 0;
case7 = 0;
case8 = 0;
case9 = 0;
case10 = 0;
case11 = 0;
case12 = 0;
times = 0; // this shows how many times we have run this over the image
holesExist = 0; // this is used to check if holes still exist after each run
holeFound = 0;
}
// *****************************************************************************
DepthComp::~DepthComp(){
// all deallocating is done automatically
}
// *****************************************************************************
Mat DepthComp::preProcess(Mat depthIn, const Mat& labelRszd, bool depthNormalize){
if (depthNormalize){
normalize(depthIn, depthIn, 0, 255, NORM_MINMAX);
}
depthIn.convertTo(depthIn, CV_16SC1);
filterSpeckles(depthIn, 0, 200, 1);
depthIn.convertTo(depthIn, CV_8UC1);
if (depthNormalize){
normalize(depthIn, depthIn, 0, 255, NORM_MINMAX);
}
// the goal is to remove wrong disparity values
for(int i=0; i < (depthIn.rows); i++) { // loop over the rows
for (int j = 1; j < (depthIn.cols); j++) { // loop over the column
if ((depthIn.at<uchar>(i, j + 1) == depthIn.at<uchar>(i, j - 1)) && (labelRszd.at<uchar>(i, j + 1) == labelRszd.at<uchar>(i, j - 1)) &&
(depthIn.at<uchar>(i, j + 1) != 0) && (depthIn.at<uchar>(i, j + 1) != depthIn.at<uchar>(i, j)) ) {
depthIn.at<uchar>(i, j) = 0;
}
if ((depthIn.at<uchar>(i, j + 2) == depthIn.at<uchar>(i, j - 1)) && (labelRszd.at<uchar>(i, j + 2) == labelRszd.at<uchar>(i, j - 1)) &&
(depthIn.at<uchar>(i, j + 2) != 0) && (depthIn.at<uchar>(i, j + 2) != depthIn.at<uchar>(i, j)) && (depthIn.at<uchar>(i, j) != 0) && (depthIn.at<uchar>(i, j + 1) != 0) ) {
depthIn.at<uchar>(i, j) = 0;
depthIn.at<uchar>(i, j + 1) = 0;
}
} // end of loop over the columns
} // end of loop over the rows
return depthIn;
}
// *****************************************************************************
Mat DepthComp::postProcess(Mat depthIn, const Mat& labelRszd, bool inpaintLeftOvers){
Mat mask(depthIn.rows, depthIn.cols, CV_8UC1);
mask.setTo(Scalar(0));
for(int i=0; i < (depthIn.rows); i++) { // loop over the rows
for (int j = 1; j < (depthIn.cols); j++) { // loop over the column
if (depthIn.at<uchar>(i, j) == 255) {
depthIn.at<uchar>(i, j) = 0;
}
if (depthIn.at<uchar>(i, j) == 0) {
mask.at<uchar>(i, j) = 255;
// the goal is to remove wrong disparity values
}
} // end of loop over the columns
} // end of loop over the rows
if (inpaintLeftOvers){
cv::inpaint(depthIn, mask, depthIn, 3, cv::INPAINT_TELEA);
}
return depthIn;
}
// *****************************************************************************
// *****************************************************************************
Mat DepthComp::identFillHoles(Mat depthIn, Mat labelRszd, bool logStats){
//to write time and result information to a file
ofstream myfile;
if (logStats){
myfile.open ("data.txt", ios::ate );
}
// the very main while loop to run several times with rotated image
// row-wise and then column-wise (with latter being rotation of former)
// with final secondary row-wise pass if needed
while((times == 0) || ((times == 1) && (holesExist == 1)) ||
((times == 2) && (holesExist == 2))) {
// we are running the same filling process one (more) time
times++;
// get start time
auto start = get_time::now();
// perform identification and filling of holes - one pass
depthIn = identFillHolesPass(depthIn, labelRszd);
// get end time
auto end = get_time::now();
auto diff = end - start;
// **** perform logging (file will be closed if logStats = false)
if(myfile.is_open()) {
myfile <<"********************************************************** RUN (( " << times << " )) *************************************************************\n\n";
myfile << "Important Information\n";
myfile << "For depth image and segmented image pair in the run number " << times << endl;
myfile << "\nIn the image, the number of ROWS is : " << "\t\t\t" << depthIn.rows << "\n\t\tAnd the number of COLUMNS is : " << "\t\t\t" << depthIn.cols << endl;
myfile <<"\nThe frequency of the cases in run number " << times << endl;
myfile << "\nnumber of case 1 holes = \t" << case1;
myfile << "\nnumber of case 2 holes = \t" << case2;
myfile << "\nnumber of case 3 holes = \t" << case3;
myfile << "\nnumber of case 4 holes = \t" << case4;
myfile << "\nnumber of case 5 holes = \t" << case5;
myfile << "\nnumber of case 6 holes = \t" << case6;
myfile << "\nnumber of case 7 holes = \t" << case7;
myfile << "\nnumber of case 8 holes = \t" << case8;
myfile << "\nnumber of case 9 holes = \t" << case9;
myfile << "\nnumber of case 10 holes = \t" << case10;
myfile << "\nnumber of case 11 holes = \t" << case11;
myfile << "\nnumber of case 12 holes = \t" << case12;
myfile <<"\n\nThe TIME:\n";
myfile << "Elapsed time is : " << std::chrono::duration_cast<std::chrono::milliseconds>(diff).count()<< " milliseconds " << "\n\n";
myfile <<"********************************************************** RUN (( " << times << " )) *************************************************************\n\n\n\n\n\n";
}
// **** end logging
// check to see of there are any hole pixels left in the image
for(int i=0; i < (depthIn.rows); i++) { // for loop checking rows for holes
for(int j = 0; j < (depthIn.cols); j++) { // for loop checking columns for holes
if(depthIn.at<uchar>(i,j) == 0) { // checking to see if holes remain
if (times == 1) { // the thing has been run ONCE and now we have to flip it
holesExist = 1; // this means holes still exist after the FIRST run
goto nestBreak; //goto is used to break out of a nested loop
} // end of if for when the thing has been run ONCE and we have to flip it
else if (times == 2) { // the thing has been run TWICE and flipped already so we now flip it back
holesExist = 2; // this means holes still exist after the SECOND run
goto nestBreak; //goto is used to break out of a nested loop
} // end of else of for when the thing has been run TWICE and flipped already so we now flip it back
else if (times == 3) { // the thing has been run THREE times and flipped twice already
holesExist = 3; // this means holes still exist after the SECOND run
goto nestBreak; //goto is used to break out of a nested loop
} // end of else if for the thing has been run THREE times and flipped twice already
} // end of if for checking if holes remain
} // end of for loop checking columns for holes
} // end of for loop checking rows for holes
nestBreak:
if((times == 1) && (holesExist == 0)) { // this means it has been run once and all holes are FILLED before any flipping
myfile << "process was run once only and ALL holes are filled." << endl;
} // end of else if for when it has been run once and all holes are FILLED before any flipping
else if((times == 1) && (holesExist == 1)) {
// this means it has been run once and now we must flip to do column-wise
// flipping all image clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 1);
flip(labelRszd, labelRszd, 1);
} // end of if for when it has been run once and now we must flip
else if((times == 2) && (holesExist == 2)) {
// this means it has been run twice and holes still exist and now we must
// flip again for for a secondary (final) row-wise pass
// flipping all image clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 1);
flip(labelRszd, labelRszd, 1);
} // end of else if for when it has been run twice and holes still exist and now we must flip
else if((times == 2) && (holesExist == 1)) {
// this means it has been run twice and all holes are FILLED and now we must flip back
// flipping all image counter-clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 0);
flip(labelRszd, labelRszd, 0);
} // end of else if for when it has been run twice and all holes are FILLED and now we must flip
else if((times == 3) && (holesExist == 3)) { // this means it has been run THREE and holes still exist
// flipping all image clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 1);
flip(labelRszd, labelRszd, 1);
// flipping all image clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 1);
flip(labelRszd, labelRszd, 1);
} // end of else if for when it has been run THREE and holes still exist
else if((times == 3) && (holesExist == 2)) { // this means it has been run THREE and holes still exist
// flipping all image clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 1);
flip(labelRszd, labelRszd, 1);
// flipping all image clockwise
transpose(depthIn, depthIn);
transpose(labelRszd, labelRszd);
flip(depthIn, depthIn, 1);
flip(labelRszd, labelRszd, 1);
} // end of else if for when it has been run THREE and holes still exist
}// end of the main while loop
// close logging file
if (logStats){
myfile.close();
cout << "Process Log: run-times + # filling cases saved in - \"data.txt\".\n";
}
// return final completed depth image (completion performed in place)
return depthIn;
}
// *****************************************************************************
Mat DepthComp::identFillHolesPass(Mat depthIn, const Mat& labelRszd){
int begin1; // beginning of hole in a row
int end1; // end of hole in a row
int hLen; // length of hole in a row
int cBegin1; // beginning of the sampling area
int cEnd1; // end of the sampling area
int rightInfo = 1; // this is used to show that information on the right does not include holes.
// When it is ZERO, there ARE HOLES, so the information is NOT AVAILABLE.
// When it is ONE, the are NO HOLES, so the information on the right is AVAILABLE.
int caseNo = 0; // specifies the case number as described in the notebook. When it is zero, no case number has been assigned.
double tempArray[depthIn.cols]; // this is used for temporarily holding the fillings to remove accumilating errors
// check the depth row by row until you reach a black pixel and set the variable 'begin1' as the position of the first black pixel.
// i is ROW
// j is COLUMN
for(int i=0; i < (depthIn.rows); i++) { // loop over the rows
for(int j = 0; j < (depthIn.cols); j++) { // loop over the columns
if(depthIn.at<uchar>(i,j) == 0) { // we have reached a hole in a line
//region caseIdentification
holeFound++;
begin1 = j; //beginning of the hole
if(begin1 != 0) { // when the hole does NOT begin1 in the begin1ning of the line
while ((labelRszd.at<uchar>(i, j) == labelRszd.at<uchar>(i, (j + 1))) && (depthIn.at<uchar>(i, j) == 0) && (j != (depthIn.cols - 1))) {
j++;
} // end1 of while
if ((j == (depthIn.cols - 1)) || (labelRszd.at<uchar>(i, j) != labelRszd.at<uchar>(i, j + 1))) {
// either the holes extend1s to the end1 of the line OR the object has end1ed but the hole has not.
if (depthIn.at<uchar>(i, j) == 0) { //when we have stopped on the hole
end1 = j;
hLen = end1 - begin1 + 1;
if ((begin1 - hLen) > 0) { // when there is enough information on the left of the hole
if (labelRszd.at<uchar>(i, ((begin1 - hLen) - 1)) == labelRszd.at<uchar>(i, begin1)) { // and more than enough information on the left is in the same object
caseNo = 1;
case1++;
} // end1 of if for case 1
else if (labelRszd.at<uchar>(i, ((begin1 - hLen) - 1)) != labelRszd.at<uchar>(i, begin1) &&
labelRszd.at<uchar>(i, (begin1 - hLen)) == labelRszd.at<uchar>(i, begin1)) {
// and Just enough information is in the same object
caseNo = 2;
case2++;
} // end1 of if for case 3
else if (labelRszd.at<uchar>(i, (begin1 - hLen)) != labelRszd.at<uchar>(i, begin1)) { // but ALL the information is NOT in the same object
if (labelRszd.at<uchar>(i, (begin1 - 1)) == labelRszd.at<uchar>(i, begin1)) { // there is at least one pixel on the left in the same label
caseNo = 3;
case3++;
} // end1 of if for case 3
else if (labelRszd.at<uchar>(i, (begin1 - 1)) != labelRszd.at<uchar>(i, begin1)) { // there are NO pixels on the left in the same label
caseNo = 12;
case12++;
} // end1 of if for case 12
} // end1 of else if for when ALL the information is NOT in the same object
} // end1 of if for when there is enough information on the left of the hole
else if ((begin1 - hLen) < 0) { //when there is NOT enough information on the left but some
if (labelRszd.at<uchar>(i, (begin1 - 1)) == labelRszd.at<uchar>(i, begin1)) { // there is at least one pixel on the left in the same label
caseNo = 3;
case3++;
} //end1 of if for case 3
else if (labelRszd.at<uchar>(i, (begin1 - 1)) != labelRszd.at<uchar>(i, begin1)) { // there are NO pixels on the left in the same label
caseNo = 12;
case12++;
} //end1 of if for case 12
} // end1 of if for when there is NOT enough information on the left but some
else if ((begin1 - hLen) == 0) { // when the amount of information on the left is EXACTLY the same as the length of the hole
if (labelRszd.at<uchar>(i, (begin1 - hLen)) == labelRszd.at<uchar>(i, begin1)) { // there is EXACTLY the same amount of info in the same label on the left
caseNo = 2;
case2++;
} //end1 of if for case 2
else if (labelRszd.at<uchar>(i, (begin1 - hLen)) != labelRszd.at<uchar>(i, begin1)) { // there is NOT enough information on the left in the same label
if (labelRszd.at<uchar>(i, (begin1 - 1)) == labelRszd.at<uchar>(i, begin1)) { // there is at least one pixel on the left in the same label
caseNo = 3;
case3++;
} //end1 of if for case 3
else if (labelRszd.at<uchar>(i, (begin1 - 1)) != labelRszd.at<uchar>(i, begin1)) { // there are NO pixels on the left in the same label
caseNo = 12;
case12++;
} //end1 of if for case 12
} //end1 of if for when there is NOT enough information on the left in the same label
}// end1 of else if for when the amount of information on the left is EXACTLY the same as the length of the hole
}// end1 of if for when when we have stopped on the hole
else if (depthIn.at<uchar>(i, j) != 0) { //when the hole does NOT continue to the end1 of the line, but to one pixel before the end1
end1 = j - 1;
hLen = end1 - begin1 + 1;
if ((begin1 - hLen) > 0) { // when there is enough information on the left of the hole
if ((labelRszd.at<uchar>(i, ((begin1 - hLen) - 1)) == labelRszd.at<uchar>(i, begin1)) && (labelRszd.at<uchar>(i,((begin1 - 1))) == labelRszd.at<uchar>(i,begin1))) {// and more than enough information on the left is in the same object
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = -1; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 4;
case4++;
} // end1 of if for case 4
else if(checkLeftLabel == 0) {
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = 0; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 5;
case5++;
} // end1 of if for case 5
else{
caseNo = 11;
case11++;
} // end1 of case 11
}
}// end1 of if for case 4
else if ((labelRszd.at<uchar>(i, ((begin1 - hLen) - 1)) != labelRszd.at<uchar>(i, begin1)) &&
(labelRszd.at<uchar>(i, (begin1 - hLen)) == labelRszd.at<uchar>(i, begin1)) &&
(labelRszd.at<uchar>(i,((begin1 - 1))) == labelRszd.at<uchar>(i,begin1))) { // and just enough information on the left is in the same object
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = 0; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 5;
case5++;
} // end1 of if for case 5
else{
caseNo = 11;
case11++;
} // end1 of case 11
}// end1 of if
else if ( ( (labelRszd.at<uchar>(i, ((begin1 - hLen) - 1)) == labelRszd.at<uchar>(i, begin1)) &&
(labelRszd.at<uchar>(i,((begin1 - 1))) != labelRszd.at<uchar>(i,begin1)) ) ||
((labelRszd.at<uchar>(i, (begin1 - hLen)) == labelRszd.at<uchar>(i, begin1)) &&
(labelRszd.at<uchar>(i,((begin1 - 1))) != labelRszd.at<uchar>(i,begin1)) ) ) {
// this means there are in fact no pixels available on the left
if(((end1 + hLen + 1) < depthIn.cols) && (labelRszd.at<uchar>(i,(end1 + hLen + 1)) == labelRszd.at<uchar>(i,begin1))) {
// there is more enough information on the right in the same label to fill the hole
rightInfo = 1; // we assume info on the right is available (without any holes), and then we check.
for(int rightInfoIndex = 0; rightInfoIndex < hLen + 1; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if(rightInfo == 1) { // case 8 .. nothing on the left , more ethan enough on the right
caseNo = 8;
case8++;
} // end1 of case 8
else if (rightInfo == 0) {
rightInfo = 1; // we assume info on the right is available, and then we check.
for (int rightInfoIndex = 0; rightInfoIndex < hLen; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if (rightInfo == 1) { // case 9... nothing on the left, JUST enough on the right
caseNo = 9;
case9++;
} // end1 of case 9
else if (rightInfo == 0) {
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some but less than enough on the right
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
}
}// end1 of if for when there is more enough information on the right in the same label to fill the hole
else if(((end1 + hLen + 1) < depthIn.cols) && (labelRszd.at<uchar>(i,(end1 + hLen + 1)) != labelRszd.at<uchar>(i,begin1))) {
// there is NOT MORE than enough info on the right to fill the hole
if(labelRszd.at<uchar>(i,(end1 + hLen)) == labelRszd.at<uchar>(i,begin1)) { // there is JUST enough info in the same label on the right
rightInfo = 1; // we assume info on the right is available, and then we check.
for (int rightInfoIndex = 0; rightInfoIndex < hLen; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if (rightInfo == 1) { // case 9 .. nothing on the left, JUST enough on the right
caseNo = 9;
case9++;
} // end1 of case 9
else if (rightInfo == 0) {
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some but not enough on the right
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
} // end1 of if for when there is JUST enough info on the right
else if(labelRszd.at<uchar>(i,(end1 + 1)) == labelRszd.at<uchar>(i,begin1)) { // there is at least one pixel on the right but NOT ENOUGH in the same object
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some but not enough on the right
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
else if(labelRszd.at<uchar>(i,(end1 + 1)) != labelRszd.at<uchar>(i,begin1)) { // there are NO pixels on the right in the same object
// case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
// end1 of case 12
} // end1 of else if for when there are NO pixels on the right in the same object
}// end1 of else if for when there is NOT MORE than enough info on the right to fill the hole
else if(((end1 + hLen + 1) > depthIn.cols) ) { // the information on the right is not enough
if(labelRszd.at<uchar>(i,(end1 + 1)) == labelRszd.at<uchar>(i,begin1)) { // there is at least one pixel on the right but NOT ENOUGH in the same object
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some on the right but not enough
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
else if(labelRszd.at<uchar>(i,(end1 + 1)) != labelRszd.at<uchar>(i,begin1)) { // there are NO pixels on the right in the same object
// case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
// end1 of case 12
} // end1 of else if for when there are NO pixels on the right in the same object
} // end1 of else if for when the information on the right is not enough
else if(((end1 + hLen + 1) == depthIn.cols) ) { // the information on the right is JUST enough
if(labelRszd.at<uchar>(i,(end1 + hLen)) == labelRszd.at<uchar>(i,begin1)) { // there is JUST enough info on the right in the same label
rightInfo = 1; // we assume info on the right is available, and then we check.
for (int rightInfoIndex = 0; rightInfoIndex < hLen; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if (rightInfo == 1) { // case 9 .. nothing on the left, just enough on the right
caseNo = 9;
case9++;
} // end1 of case 9
else if (rightInfo == 0) {
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some on the right but not enough
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
} // end1 of if for when there is JUST enough info on the right
else if(labelRszd.at<uchar>(i,(end1 + 1)) == labelRszd.at<uchar>(i,begin1)) { // there is at least one pixel on the right but NOT ENOUGH in the same object
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some on the right, but not enough
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
else if(labelRszd.at<uchar>(i,(end1 + 1)) != labelRszd.at<uchar>(i,begin1)) { // there are NO pixels on the right in the same object
// case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
// end1 of case 12
} // end1 of else if for when there are NO pixels on the right in the same object
} // end1 of else if for when the information on the right is JUST enough
} // end1 of else if for when there are in fact no pixels available on the left
else if (labelRszd.at<uchar>(i, (begin1 - hLen)) != labelRszd.at<uchar>(i, begin1)) { // but ALL the information is NOT in the same object
if (labelRszd.at<uchar>(i, (begin1 - 1)) == labelRszd.at<uchar>(i, begin1)) { // there is at least one pixel on the left in the same label
caseNo = 11;
case11++;
} // end1 of if for case 11
else if (labelRszd.at<uchar>(i, (begin1 - 1)) != labelRszd.at<uchar>(i, begin1)) { // there are NO pixels on the left in the same label
caseNo = 10;
case10++;
} // end1 of if for case 10
} // end1 of else if for when ALL the information is NOT in the same object
}// end1 of if for when there is enough information on the left of the hole
else if ((begin1 - hLen) < 0) { //when there is NOT enough information on the left but some
if (labelRszd.at<uchar>(i, (begin1 - 1)) == labelRszd.at<uchar>(i, begin1)) { // there is at least one pixel on the left in the same label
caseNo = 11;
case11++;
} //end1 of if for case 11
else if (labelRszd.at<uchar>(i, (begin1 - 1)) != labelRszd.at<uchar>(i, begin1)) { // there are NO pixels on the left in the same label
caseNo = 10;
case10++;
} //end1 of if for case 12
} // end1 of if for when there is NOT enough information on the left but some
else if ((begin1 - hLen) == 0) { // when the amount of information on the left is EXACTLY the same as the length of the hole
if (labelRszd.at<uchar>(i, (begin1 - hLen)) == labelRszd.at<uchar>(i, begin1)) { // there is EXACTLY the same amount of info in the same label on the left
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = 0; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 5;
case5++;
} // end1 of if for case 5
else{
caseNo = 11;
case11++;
} // end1 of case 11
} //end1 of if
else if (labelRszd.at<uchar>(i, (begin1 - hLen)) != labelRszd.at<uchar>(i, begin1)) { // there is NOT enough information on the left in the same label
if (labelRszd.at<uchar>(i, (begin1 - 1)) == labelRszd.at<uchar>(i, begin1)) { // there is at least one pixel on the left in the same label
caseNo = 11;
case11++;
} //end1 of if for case 11
else if (labelRszd.at<uchar>(i, (begin1 - 1)) != labelRszd.at<uchar>(i, begin1)) { // there are NO pixels on the left in the same label
caseNo = 10;
case10++;
} //end1 of if for case 10
} //end1 of if for when there is NOT enough information on the left in the same label
}// end1 of else if for when the amount of information on the left is EXACTLY the same as the length of the hole
}// end1 of else if for when the hole does NOT continue to the end1 of the line, but to one pixel before the end1
}// end1 of if that started after the while (when either the holes extend1s to the end1 of the line OR the object has end1ed but the hole has not..)
if(caseNo == 0) { // a condition in the "while" was not met and NO case number is assigned yet
if((depthIn.at<uchar>(i,j) != 0) && (labelRszd.at<uchar>(i,j) == labelRszd.at<uchar>(i,j+1))) { // the object has not end1ed but the hole has end1ed
// which means some stuff is available on the right
end1 = j - 1;
hLen = end1 - begin1 + 1;
if((begin1 - hLen) > 0) { // when there is enough information on the left of the hole
if((labelRszd.at<uchar>(i,((begin1 - hLen) - 1)) == labelRszd.at<uchar>(i,begin1)) && (labelRszd.at<uchar>(i,((begin1 - 1))) == labelRszd.at<uchar>(i,begin1))) {
// and more than enough information is in the same object
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = -1; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 4;
case4++;
} // end1 of if for case 4
else if(checkLeftLabel == 0) {
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = 0; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 5;
case5++;
} // end1 of if for case 5
else{
caseNo = 11;
case11++;
} // end1 of case 11
}
} // possibly and more than enough information is in the same object
else if((labelRszd.at<uchar>(i,((begin1 - hLen) - 1)) != labelRszd.at<uchar>(i,begin1)) && (labelRszd.at<uchar>(i,((begin1 - hLen))) == labelRszd.at<uchar>(i,begin1))
&& (labelRszd.at<uchar>(i,((begin1 - 1))) == labelRszd.at<uchar>(i,begin1))) {
// and JUST enough information on the left is in the same object
int checkLeftLabel = 1; // we assume the left info is all in the same label
for(int chindex = 0; chindex < hLen; chindex++) {
if((labelRszd.at<uchar>(i, begin1 - hLen + chindex) != labelRszd.at<uchar>(i, begin1)) || (depthIn.at<uchar>(i, begin1 - hLen + chindex) == 0) ) {
checkLeftLabel = 0;
}
}
if(checkLeftLabel == 1) {
caseNo = 5;
case5++;
} // end1 of if for case 5
else{
caseNo = 11;
case11++;
} // end1 of case 11
} // end1 of if
else if ( ( (labelRszd.at<uchar>(i, ((begin1 - hLen) - 1)) == labelRszd.at<uchar>(i, begin1)) &&
(labelRszd.at<uchar>(i,((begin1 - 1))) != labelRszd.at<uchar>(i,begin1)) ) ||
((labelRszd.at<uchar>(i, (begin1 - hLen)) == labelRszd.at<uchar>(i, begin1)) &&
(labelRszd.at<uchar>(i,((begin1 - 1))) != labelRszd.at<uchar>(i,begin1)) ) ) {
// this means there are in fact no pixels available on the left
if(((end1 + hLen + 1) < depthIn.cols) && (labelRszd.at<uchar>(i,(end1 + hLen + 1)) == labelRszd.at<uchar>(i,begin1))) {
// there is more enough information on the right in the same label to fill the hole
rightInfo = 1; // we assume info on the right is available (without any holes), and then we check.
for(int rightInfoIndex = 0; rightInfoIndex < hLen + 1; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if(rightInfo == 1) { // case 8 .. nothing on the left , more ethan enough on the right
caseNo = 8;
case8++;
} // end1 of case 8
else if (rightInfo == 0) {
rightInfo = 1; // we assume info on the right is available, and then we check.
for (int rightInfoIndex = 0; rightInfoIndex < hLen; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if (rightInfo == 1) { // case 9... nothing on the left, JUST enough on the right
caseNo = 9;
case9++;
} // end1 of case 9
else if (rightInfo == 0) {
if(depthIn.at<uchar>(i, (end1 + 1)) != 0) { // case 10 .. nothing on the left, some but less than enough on the right
caseNo = 10;
case10++;
} // end1 of case 10
else{ // case 12 ... nothing on the left, nothing on the right
caseNo = 12;
case12++;
} // end1 of case 12
} // end1 of last else if
}
}// end1 of if for when there is more enough information on the right in the same label to fill the hole
else if(((end1 + hLen + 1) < depthIn.cols) && (labelRszd.at<uchar>(i,(end1 + hLen + 1)) != labelRszd.at<uchar>(i,begin1))) {
// there is NOT MORE than enough info on the right to fill the hole
if(labelRszd.at<uchar>(i,(end1 + hLen)) == labelRszd.at<uchar>(i,begin1)) { // there is JUST enough info in the same label on the right
rightInfo = 1; // we assume info on the right is available, and then we check.
for (int rightInfoIndex = 0; rightInfoIndex < hLen; rightInfoIndex++) {
if ((depthIn.at<uchar>(i, (end1 + 1 + rightInfoIndex)) == 0) || (labelRszd.at<uchar>(i, (end1 + 1 + rightInfoIndex)) != labelRszd.at<uchar>(i, (begin1)) ) ) {
rightInfo = 0;
}
}
if (rightInfo == 1) { // case 9 .. nothing on the left, JUST enough on the right
caseNo = 9;
case9++;
} // end1 of case 9
else if (rightInfo == 0) {