forked from h3ct0r/fast_ellipse_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EllipseDetectorYaed.cpp
1909 lines (1521 loc) · 44.4 KB
/
EllipseDetectorYaed.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
/*
This code is intended for academic use only.
You are free to use and modify the code, at your own risk.
If you use this code, or find it useful, please refer to the paper:
Michele Fornaciari, Andrea Prati, Rita Cucchiara,
A fast and effective ellipse detector for embedded vision applications
Pattern Recognition, Volume 47, Issue 11, November 2014, Pages 3693-3708, ISSN 0031-3203,
http://dx.doi.org/10.1016/j.patcog.2014.05.012.
(http://www.sciencedirect.com/science/article/pii/S0031320314001976)
The comments in the code refer to the abovementioned paper.
If you need further details about the code or the algorithm, please contact me at:
last update: 23/12/2014
*/
#include "EllipseDetectorYaed.h"
CEllipseDetectorYaed::CEllipseDetectorYaed(void) : _times(6, 0.0), _timesHelper(6, 0.0)
{
// Default Parameters Settings
_szPreProcessingGaussKernelSize = Size(5, 5);
_dPreProcessingGaussSigma = 1.0;
_fThPosition = 1.0f;
_fMaxCenterDistance = 100.0f * 0.05f;
_fMaxCenterDistance2 = _fMaxCenterDistance * _fMaxCenterDistance;
_iMinEdgeLength = 16;
_fMinOrientedRectSide = 3.0f;
_fDistanceToEllipseContour = 0.1f;
_fMinScore = 0.4f;
_fMinReliability = 0.4f;
_uNs = 16;
srand(unsigned(time(NULL)));
}
CEllipseDetectorYaed::~CEllipseDetectorYaed(void)
{
}
void CEllipseDetectorYaed::SetParameters(Size szPreProcessingGaussKernelSize,
double dPreProcessingGaussSigma,
float fThPosition,
float fMaxCenterDistance,
int iMinEdgeLength,
float fMinOrientedRectSide,
float fDistanceToEllipseContour,
float fMinScore,
float fMinReliability,
int iNs
)
{
_szPreProcessingGaussKernelSize = szPreProcessingGaussKernelSize;
_dPreProcessingGaussSigma = dPreProcessingGaussSigma;
_fThPosition = fThPosition;
_fMaxCenterDistance = fMaxCenterDistance;
_iMinEdgeLength = iMinEdgeLength;
_fMinOrientedRectSide = fMinOrientedRectSide;
_fDistanceToEllipseContour = fDistanceToEllipseContour;
_fMinScore = fMinScore;
_fMinReliability = fMinReliability;
_uNs = iNs;
_fMaxCenterDistance2 = _fMaxCenterDistance * _fMaxCenterDistance;
}
uint inline CEllipseDetectorYaed::GenerateKey(uchar pair, ushort u, ushort v)
{
return (pair << 30) + (u << 15) + v;
};
int CEllipseDetectorYaed::FindMaxK(const int* v) const
{
int max_val = 0;
int max_idx = 0;
for (int i = 0; i<ACC_R_SIZE; ++i)
{
(v[i] > max_val) ? max_val = v[i], max_idx = i : NULL;
}
return max_idx + 90;
};
int CEllipseDetectorYaed::FindMaxN(const int* v) const
{
int max_val = 0;
int max_idx = 0;
for (int i = 0; i<ACC_N_SIZE; ++i)
{
(v[i] > max_val) ? max_val = v[i], max_idx = i : NULL;
}
return max_idx;
};
int CEllipseDetectorYaed::FindMaxA(const int* v) const
{
int max_val = 0;
int max_idx = 0;
for (int i = 0; i<ACC_A_SIZE; ++i)
{
(v[i] > max_val) ? max_val = v[i], max_idx = i : NULL;
}
return max_idx;
};
float CEllipseDetectorYaed::GetMedianSlope(vector<Point2f>& med, Point2f& M, vector<float>& slopes)
{
// med : vector of points
// M : centroid of the points in med
// slopes : vector of the slopes
unsigned iNofPoints = med.size();
//CV_Assert(iNofPoints >= 2);
unsigned halfSize = iNofPoints >> 1;
unsigned quarterSize = halfSize >> 1;
vector<float> xx, yy;
slopes.reserve(halfSize);
xx.reserve(iNofPoints);
yy.reserve(iNofPoints);
for (unsigned i = 0; i < halfSize; ++i)
{
Point2f& p1 = med[i];
Point2f& p2 = med[halfSize + i];
xx.push_back(p1.x);
xx.push_back(p2.x);
yy.push_back(p1.y);
yy.push_back(p2.y);
float den = (p2.x - p1.x);
float num = (p2.y - p1.y);
if (den == 0) den = 0.00001f;
slopes.push_back(num / den);
}
nth_element(slopes.begin(), slopes.begin() + quarterSize, slopes.end());
nth_element(xx.begin(), xx.begin() + halfSize, xx.end());
nth_element(yy.begin(), yy.begin() + halfSize, yy.end());
M.x = xx[halfSize];
M.y = yy[halfSize];
return slopes[quarterSize];
};
void CEllipseDetectorYaed::GetFastCenter(vector<Point>& e1, vector<Point>& e2, EllipseData& data)
{
data.isValid = true;
unsigned size_1 = unsigned(e1.size());
unsigned size_2 = unsigned(e2.size());
unsigned hsize_1 = size_1 >> 1;
unsigned hsize_2 = size_2 >> 1;
Point& med1 = e1[hsize_1];
Point& med2 = e2[hsize_2];
Point2f M12, M34;
float q2, q4;
{
// First to second
// Reference slope
float dx_ref = float(e1[0].x - med2.x);
float dy_ref = float(e1[0].y - med2.y);
if (dy_ref == 0) dy_ref = 0.00001f;
float m_ref = dy_ref / dx_ref;
data.ra = m_ref;
// Find points with same slope as reference
vector<Point2f> med;
med.reserve(hsize_2);
unsigned minPoints = (_uNs < hsize_2) ? _uNs : hsize_2;
vector<uint> indexes(minPoints);
if (_uNs < hsize_2)
{
unsigned iSzBin = hsize_2 / unsigned(_uNs);
unsigned iIdx = hsize_2 + (iSzBin / 2);
for (unsigned i = 0; i<_uNs; ++i)
{
indexes[i] = iIdx;
iIdx += iSzBin;
}
}
else
{
iota(indexes.begin(), indexes.end(), hsize_2);
}
for (uint ii = 0; ii<minPoints; ++ii)
{
uint i = indexes[ii];
float x1 = float(e2[i].x);
float y1 = float(e2[i].y);
uint begin = 0;
uint end = size_1 - 1;
float xb = float(e1[begin].x);
float yb = float(e1[begin].y);
float res_begin = ((xb - x1) * dy_ref) - ((yb - y1) * dx_ref);
int sign_begin = sgn(res_begin);
if (sign_begin == 0)
{
//found
med.push_back(Point2f((xb + x1)* 0.5f, (yb + y1)* 0.5f));
continue;
}
float xe = float(e1[end].x);
float ye = float(e1[end].y);
float res_end = ((xe - x1) * dy_ref) - ((ye - y1) * dx_ref);
int sign_end = sgn(res_end);
if (sign_end == 0)
{
//found
med.push_back(Point2f((xe + x1)* 0.5f, (ye + y1)* 0.5f));
continue;
}
if ((sign_begin + sign_end) != 0)
{
continue;
}
uint j = (begin + end) >> 1;
while (end - begin > 2)
{
float x2 = float(e1[j].x);
float y2 = float(e1[j].y);
float res = ((x2 - x1) * dy_ref) - ((y2 - y1) * dx_ref);
int sign_res = sgn(res);
if (sign_res == 0)
{
//found
med.push_back(Point2f((x2 + x1)* 0.5f, (y2 + y1)* 0.5f));
break;
}
if (sign_res + sign_begin == 0)
{
sign_end = sign_res;
end = j;
}
else
{
sign_begin = sign_res;
begin = j;
}
j = (begin + end) >> 1;
}
med.push_back(Point2f((e1[j].x + x1)* 0.5f, (e1[j].y + y1)* 0.5f));
}
if (med.size() < 2)
{
data.isValid = false;
return;
}
q2 = GetMedianSlope(med, M12, data.Sa);
}
{
// Second to first
// Reference slope
float dx_ref = float(med1.x - e2[0].x);
float dy_ref = float(med1.y - e2[0].y);
if (dy_ref == 0) dy_ref = 0.00001f;
float m_ref = dy_ref / dx_ref;
data.rb = m_ref;
// Find points with same slope as reference
vector<Point2f> med;
med.reserve(hsize_1);
uint minPoints = (_uNs < hsize_1) ? _uNs : hsize_1;
vector<uint> indexes(minPoints);
if (_uNs < hsize_1)
{
unsigned iSzBin = hsize_1 / unsigned(_uNs);
unsigned iIdx = hsize_1 + (iSzBin / 2);
for (unsigned i = 0; i<_uNs; ++i)
{
indexes[i] = iIdx;
iIdx += iSzBin;
}
}
else
{
iota(indexes.begin(), indexes.end(), hsize_1);
}
for (uint ii = 0; ii<minPoints; ++ii)
{
uint i = indexes[ii];
float x1 = float(e1[i].x);
float y1 = float(e1[i].y);
uint begin = 0;
uint end = size_2 - 1;
float xb = float(e2[begin].x);
float yb = float(e2[begin].y);
float res_begin = ((xb - x1) * dy_ref) - ((yb - y1) * dx_ref);
int sign_begin = sgn(res_begin);
if (sign_begin == 0)
{
//found
med.push_back(Point2f((xb + x1)* 0.5f, (yb + y1)* 0.5f));
continue;
}
float xe = float(e2[end].x);
float ye = float(e2[end].y);
float res_end = ((xe - x1) * dy_ref) - ((ye - y1) * dx_ref);
int sign_end = sgn(res_end);
if (sign_end == 0)
{
//found
med.push_back(Point2f((xe + x1)* 0.5f, (ye + y1)* 0.5f));
continue;
}
if ((sign_begin + sign_end) != 0)
{
continue;
}
uint j = (begin + end) >> 1;
while (end - begin > 2)
{
float x2 = float(e2[j].x);
float y2 = float(e2[j].y);
float res = ((x2 - x1) * dy_ref) - ((y2 - y1) * dx_ref);
int sign_res = sgn(res);
if (sign_res == 0)
{
//found
med.push_back(Point2f((x2 + x1)* 0.5f, (y2 + y1)* 0.5f));
break;
}
if (sign_res + sign_begin == 0)
{
sign_end = sign_res;
end = j;
}
else
{
sign_begin = sign_res;
begin = j;
}
j = (begin + end) >> 1;
}
med.push_back(Point2f((e2[j].x + x1)* 0.5f, (e2[j].y + y1)* 0.5f));
}
if (med.size() < 2)
{
data.isValid = false;
return;
}
q4 = GetMedianSlope(med, M34, data.Sb);
}
if (q2 == q4)
{
data.isValid = false;
return;
}
float invDen = 1 / (q2 - q4);
data.Cab.x = (M34.y - q4*M34.x - M12.y + q2*M12.x) * invDen;
data.Cab.y = (q2*M34.y - q4*M12.y + q2*q4*(M12.x - M34.x)) * invDen;
data.ta = q2;
data.tb = q4;
data.Ma = M12;
data.Mb = M34;
};
void CEllipseDetectorYaed::DetectEdges13(Mat1b& DP, VVP& points_1, VVP& points_3)
{
// Vector of connected edge points
VVP contours;
// Labeling 8-connected edge points, discarding edge too small
Labeling(DP, contours, _iMinEdgeLength);
int iContoursSize = int(contours.size());
// For each edge
for (int i = 0; i < iContoursSize; ++i)
{
VP& edgeSegment = contours[i];
#ifndef DISCARD_CONSTRAINT_OBOX
// Selection strategy - Step 1 - See Sect [3.1.2] of the paper
// Constraint on axes aspect ratio
RotatedRect oriented = minAreaRect(edgeSegment);
float o_min = min(oriented.size.width, oriented.size.height);
if (o_min < _fMinOrientedRectSide)
{
continue;
}
#endif
// Order edge points of the same arc
sort(edgeSegment.begin(), edgeSegment.end(), SortTopLeft2BottomRight);
int iEdgeSegmentSize = unsigned(edgeSegment.size());
// Get extrema of the arc
Point& left = edgeSegment[0];
Point& right = edgeSegment[iEdgeSegmentSize - 1];
// Find convexity - See Sect [3.1.3] of the paper
int iCountTop = 0;
int xx = left.x;
for (int k = 1; k < iEdgeSegmentSize; ++k)
{
if (edgeSegment[k].x == xx) continue;
iCountTop += (edgeSegment[k].y - left.y);
xx = edgeSegment[k].x;
}
int width = abs(right.x - left.x) + 1;
int height = abs(right.y - left.y) + 1;
int iCountBottom = (width * height) - iEdgeSegmentSize - iCountTop;
if (iCountBottom > iCountTop)
{ //1
points_1.push_back(edgeSegment);
}
else if (iCountBottom < iCountTop)
{ //3
points_3.push_back(edgeSegment);
}
}
};
void CEllipseDetectorYaed::DetectEdges24(Mat1b& DN, VVP& points_2, VVP& points_4 )
{
// Vector of connected edge points
VVP contours;
/// Labeling 8-connected edge points, discarding edge too small
Labeling(DN, contours, _iMinEdgeLength);
int iContoursSize = unsigned(contours.size());
// For each edge
for (int i = 0; i < iContoursSize; ++i)
{
VP& edgeSegment = contours[i];
#ifndef DISCARD_CONSTRAINT_OBOX
// Selection strategy - Step 1 - See Sect [3.1.2] of the paper
// Constraint on axes aspect ratio
RotatedRect oriented = minAreaRect(edgeSegment);
float o_min = min(oriented.size.width, oriented.size.height);
if (o_min < _fMinOrientedRectSide)
{
continue;
}
#endif
// Order edge points of the same arc
sort(edgeSegment.begin(), edgeSegment.end(), SortBottomLeft2TopRight);
int iEdgeSegmentSize = unsigned(edgeSegment.size());
// Get extrema of the arc
Point& left = edgeSegment[0];
Point& right = edgeSegment[iEdgeSegmentSize - 1];
// Find convexity - See Sect [3.1.3] of the paper
int iCountBottom = 0;
int xx = left.x;
for (int k = 1; k < iEdgeSegmentSize; ++k)
{
if (edgeSegment[k].x == xx) continue;
iCountBottom += (left.y - edgeSegment[k].y);
xx = edgeSegment[k].x;
}
int width = abs(right.x - left.x) + 1;
int height = abs(right.y - left.y) + 1;
int iCountTop = (width *height) - iEdgeSegmentSize - iCountBottom;
if (iCountBottom > iCountTop)
{
//2
points_2.push_back(edgeSegment);
}
else if (iCountBottom < iCountTop)
{
//4
points_4.push_back(edgeSegment);
}
}
};
// Most important function for detecting ellipses. See Sect[3.2.3] of the paper
void CEllipseDetectorYaed::FindEllipses( Point2f& center,
VP& edge_i,
VP& edge_j,
VP& edge_k,
EllipseData& data_ij,
EllipseData& data_ik,
vector<Ellipse>& ellipses
)
{
// Find ellipse parameters
// 0-initialize accumulators
memset(accN, 0, sizeof(int)*ACC_N_SIZE);
memset(accR, 0, sizeof(int)*ACC_R_SIZE);
memset(accA, 0, sizeof(int)*ACC_A_SIZE);
Tac(3); //estimation
// Get size of the 4 vectors of slopes (2 pairs of arcs)
int sz_ij1 = int(data_ij.Sa.size());
int sz_ij2 = int(data_ij.Sb.size());
int sz_ik1 = int(data_ik.Sa.size());
int sz_ik2 = int(data_ik.Sb.size());
// Get the size of the 3 arcs
size_t sz_ei = edge_i.size();
size_t sz_ej = edge_j.size();
size_t sz_ek = edge_k.size();
// Center of the estimated ellipse
float a0 = center.x;
float b0 = center.y;
// Estimation of remaining parameters
// Uses 4 combinations of parameters. See Table 1 and Sect [3.2.3] of the paper.
{
float q1 = data_ij.ra;
float q3 = data_ik.ra;
float q5 = data_ik.rb;
for (int ij1 = 0; ij1 < sz_ij1; ++ij1)
{
float q2 = data_ij.Sa[ij1];
float q1xq2 = q1*q2;
for (int ik1 = 0; ik1 < sz_ik1; ++ik1)
{
float q4 = data_ik.Sa[ik1];
float q3xq4 = q3*q4;
// See Eq. [13-18] in the paper
float a = (q1xq2 - q3xq4);
float b = (q3xq4 + 1)*(q1 + q2) - (q1xq2 + 1)*(q3 + q4);
float Kp = (-b + sqrt(b*b + 4 * a*a)) / (2 * a);
float zplus = ((q1 - Kp)*(q2 - Kp)) / ((1 + q1*Kp)*(1 + q2*Kp));
if (zplus >= 0.0f)
{
continue;
}
float Np = sqrt(-zplus);
float rho = atan(Kp);
int rhoDeg;
if (Np > 1.f)
{
Np = 1.f / Np;
rhoDeg = cvRound((rho * 180 / CV_PI) + 180) % 180; // [0,180)
}
else
{
rhoDeg = cvRound((rho * 180 / CV_PI) + 90) % 180; // [0,180)
}
int iNp = cvRound(Np * 100); // [0, 100]
if (0 <= iNp && iNp < ACC_N_SIZE &&
0 <= rhoDeg && rhoDeg < ACC_R_SIZE
)
{
++accN[iNp]; // Increment N accumulator
++accR[rhoDeg]; // Increment R accumulator
}
}
for (int ik2 = 0; ik2 < sz_ik2; ++ik2)
{
float q4 = data_ik.Sb[ik2];
float q5xq4 = q5*q4;
// See Eq. [13-18] in the paper
float a = (q1xq2 - q5xq4);
float b = (q5xq4 + 1)*(q1 + q2) - (q1xq2 + 1)*(q5 + q4);
float Kp = (-b + sqrt(b*b + 4 * a*a)) / (2 * a);
float zplus = ((q1 - Kp)*(q2 - Kp)) / ((1 + q1*Kp)*(1 + q2*Kp));
if (zplus >= 0.0f)
{
continue;
}
float Np = sqrt(-zplus);
float rho = atan(Kp);
int rhoDeg;
if (Np > 1.f)
{
Np = 1.f / Np;
rhoDeg = cvRound((rho * 180 / CV_PI) + 180) % 180; // [0,180)
}
else
{
rhoDeg = cvRound((rho * 180 / CV_PI) + 90) % 180; // [0,180)
}
int iNp = cvRound(Np * 100); // [0, 100]
if (0 <= iNp && iNp < ACC_N_SIZE &&
0 <= rhoDeg && rhoDeg < ACC_R_SIZE
)
{
++accN[iNp]; // Increment N accumulator
++accR[rhoDeg]; // Increment R accumulator
}
}
}
}
{
float q1 = data_ij.rb;
float q3 = data_ik.rb;
float q5 = data_ik.ra;
for (int ij2 = 0; ij2 < sz_ij2; ++ij2)
{
float q2 = data_ij.Sb[ij2];
float q1xq2 = q1*q2;
for (int ik2 = 0; ik2 < sz_ik2; ++ik2)
{
float q4 = data_ik.Sb[ik2];
float q3xq4 = q3*q4;
// See Eq. [13-18] in the paper
float a = (q1xq2 - q3xq4);
float b = (q3xq4 + 1)*(q1 + q2) - (q1xq2 + 1)*(q3 + q4);
float Kp = (-b + sqrt(b*b + 4 * a*a)) / (2 * a);
float zplus = ((q1 - Kp)*(q2 - Kp)) / ((1 + q1*Kp)*(1 + q2*Kp));
if (zplus >= 0.0f)
{
continue;
}
float Np = sqrt(-zplus);
float rho = atan(Kp);
int rhoDeg;
if (Np > 1.f)
{
Np = 1.f / Np;
rhoDeg = cvRound((rho * 180 / CV_PI) + 180) % 180; // [0,180)
}
else
{
rhoDeg = cvRound((rho * 180 / CV_PI) + 90) % 180; // [0,180)
}
int iNp = cvRound(Np * 100); // [0, 100]
if (0 <= iNp && iNp < ACC_N_SIZE &&
0 <= rhoDeg && rhoDeg < ACC_R_SIZE
)
{
++accN[iNp]; // Increment N accumulator
++accR[rhoDeg]; // Increment R accumulator
}
}
for (int ik1 = 0; ik1 < sz_ik1; ++ik1)
{
float q4 = data_ik.Sa[ik1];
float q5xq4 = q5*q4;
// See Eq. [13-18] in the paper
float a = (q1xq2 - q5xq4);
float b = (q5xq4 + 1)*(q1 + q2) - (q1xq2 + 1)*(q5 + q4);
float Kp = (-b + sqrt(b*b + 4 * a*a)) / (2 * a);
float zplus = ((q1 - Kp)*(q2 - Kp)) / ((1 + q1*Kp)*(1 + q2*Kp));
if (zplus >= 0.0f)
{
continue;
}
float Np = sqrt(-zplus);
float rho = atan(Kp);
int rhoDeg;
if (Np > 1.f)
{
Np = 1.f / Np;
rhoDeg = cvRound((rho * 180 / CV_PI) + 180) % 180; // [0,180)
}
else
{
rhoDeg = cvRound((rho * 180 / CV_PI) + 90) % 180; // [0,180)
}
int iNp = cvRound(Np * 100); // [0, 100]
if (0 <= iNp && iNp < ACC_N_SIZE &&
0 <= rhoDeg && rhoDeg < ACC_R_SIZE
)
{
++accN[iNp]; // Increment N accumulator
++accR[rhoDeg]; // Increment R accumulator
}
}
}
}
// Find peak in N and K accumulator
int iN = FindMaxN(accN);
int iK = FindMaxK(accR);
// Recover real values
float fK = float(iK);
float Np = float(iN) * 0.01f;
float rho = fK * float(CV_PI) / 180.f; //deg 2 rad
float Kp = tan(rho);
// Estimate A. See Eq. [19 - 22] in Sect [3.2.3] of the paper
for (ushort l = 0; l < sz_ei; ++l)
{
Point& pp = edge_i[l];
float sk = 1.f / sqrt(Kp*Kp + 1.f);
float x0 = ((pp.x - a0) * sk) + (((pp.y - b0)*Kp) * sk);
float y0 = -(((pp.x - a0) * Kp) * sk) + ((pp.y - b0) * sk);
float Ax = sqrt((x0*x0*Np*Np + y0*y0) / ((Np*Np)*(1.f + Kp*Kp)));
int A = cvRound(abs(Ax / cos(rho)));
if ((0 <= A) && (A < ACC_A_SIZE))
{
++accA[A];
}
}
for (ushort l = 0; l < sz_ej; ++l)
{
Point& pp = edge_j[l];
float sk = 1.f / sqrt(Kp*Kp + 1.f);
float x0 = ((pp.x - a0) * sk) + (((pp.y - b0)*Kp) * sk);
float y0 = -(((pp.x - a0) * Kp) * sk) + ((pp.y - b0) * sk);
float Ax = sqrt((x0*x0*Np*Np + y0*y0) / ((Np*Np)*(1.f + Kp*Kp)));
int A = cvRound(abs(Ax / cos(rho)));
if ((0 <= A) && (A < ACC_A_SIZE))
{
++accA[A];
}
}
for (ushort l = 0; l < sz_ek; ++l)
{
Point& pp = edge_k[l];
float sk = 1.f / sqrt(Kp*Kp + 1.f);
float x0 = ((pp.x - a0) * sk) + (((pp.y - b0)*Kp) * sk);
float y0 = -(((pp.x - a0) * Kp) * sk) + ((pp.y - b0) * sk);
float Ax = sqrt((x0*x0*Np*Np + y0*y0) / ((Np*Np)*(1.f + Kp*Kp)));
int A = cvRound(abs(Ax / cos(rho)));
if ((0 <= A) && (A < ACC_A_SIZE))
{
++accA[A];
}
}
// Find peak in A accumulator
int A = FindMaxA(accA);
float fA = float(A);
// Find B value. See Eq [23] in the paper
float fB = abs(fA * Np);
// Got all ellipse parameters!
Ellipse ell(a0, b0, fA, fB, fmod(rho + float(CV_PI)*2.f, float(CV_PI)));
Toc(3); //estimation
Tac(4); //validation
// Get the score. See Sect [3.3.1] in the paper
// Find the number of edge pixel lying on the ellipse
float _cos = cos(-ell._rad);
float _sin = sin(-ell._rad);
float invA2 = 1.f / (ell._a * ell._a);
float invB2 = 1.f / (ell._b * ell._b);
float invNofPoints = 1.f / float(sz_ei + sz_ej + sz_ek);
int counter_on_perimeter = 0;
for (ushort l = 0; l < sz_ei; ++l)
{
float tx = float(edge_i[l].x) - ell._xc;
float ty = float(edge_i[l].y) - ell._yc;
float rx = (tx*_cos - ty*_sin);
float ry = (tx*_sin + ty*_cos);
float h = (rx*rx)*invA2 + (ry*ry)*invB2;
if (abs(h - 1.f) < _fDistanceToEllipseContour)
{
++counter_on_perimeter;
}
}
for (ushort l = 0; l < sz_ej; ++l)
{
float tx = float(edge_j[l].x) - ell._xc;
float ty = float(edge_j[l].y) - ell._yc;
float rx = (tx*_cos - ty*_sin);
float ry = (tx*_sin + ty*_cos);
float h = (rx*rx)*invA2 + (ry*ry)*invB2;
if (abs(h - 1.f) < _fDistanceToEllipseContour)
{
++counter_on_perimeter;
}
}
for (ushort l = 0; l < sz_ek; ++l)
{
float tx = float(edge_k[l].x) - ell._xc;
float ty = float(edge_k[l].y) - ell._yc;
float rx = (tx*_cos - ty*_sin);
float ry = (tx*_sin + ty*_cos);
float h = (rx*rx)*invA2 + (ry*ry)*invB2;
if (abs(h - 1.f) < _fDistanceToEllipseContour)
{
++counter_on_perimeter;
}
}
//no points found on the ellipse
if (counter_on_perimeter <= 0)
{
Toc(4); //validation
return;
}
// Compute score
float score = float(counter_on_perimeter) * invNofPoints;
if (score < _fMinScore)
{
Toc(4); //validation
return;
}
// Compute reliability
// this metric is not described in the paper, mostly due to space limitations.
// The main idea is that for a given ellipse (TD) even if the score is high, the arcs
// can cover only a small amount of the contour of the estimated ellipse.
// A low reliability indicate that the arcs form an elliptic shape by chance, but do not underlie
// an actual ellipse. The value is normalized between 0 and 1.
// The default value is 0.4.
// It is somehow similar to the "Angular Circumreference Ratio" saliency criteria
// as in the paper:
// D. K. Prasad, M. K. Leung, S.-Y. Cho, Edge curvature and convexity
// based ellipse detection method, Pattern Recognition 45 (2012) 3204-3221.
float di, dj, dk;
{
Point2f p1(float(edge_i[0].x), float(edge_i[0].y));
Point2f p2(float(edge_i[sz_ei - 1].x), float(edge_i[sz_ei - 1].y));
p1.x -= ell._xc;
p1.y -= ell._yc;
p2.x -= ell._xc;
p2.y -= ell._yc;
Point2f r1((p1.x*_cos - p1.y*_sin), (p1.x*_sin + p1.y*_cos));
Point2f r2((p2.x*_cos - p2.y*_sin), (p2.x*_sin + p2.y*_cos));
di = abs(r2.x - r1.x) + abs(r2.y - r1.y);
}
{
Point2f p1(float(edge_j[0].x), float(edge_j[0].y));
Point2f p2(float(edge_j[sz_ej - 1].x), float(edge_j[sz_ej - 1].y));
p1.x -= ell._xc;
p1.y -= ell._yc;
p2.x -= ell._xc;
p2.y -= ell._yc;
Point2f r1((p1.x*_cos - p1.y*_sin), (p1.x*_sin + p1.y*_cos));
Point2f r2((p2.x*_cos - p2.y*_sin), (p2.x*_sin + p2.y*_cos));
dj = abs(r2.x - r1.x) + abs(r2.y - r1.y);
}
{
Point2f p1(float(edge_k[0].x), float(edge_k[0].y));
Point2f p2(float(edge_k[sz_ek - 1].x), float(edge_k[sz_ek - 1].y));
p1.x -= ell._xc;
p1.y -= ell._yc;
p2.x -= ell._xc;
p2.y -= ell._yc;
Point2f r1((p1.x*_cos - p1.y*_sin), (p1.x*_sin + p1.y*_cos));
Point2f r2((p2.x*_cos - p2.y*_sin), (p2.x*_sin + p2.y*_cos));
dk = abs(r2.x - r1.x) + abs(r2.y - r1.y);
}
// This allows to get rid of thick edges
float rel = min(1.f, ((di + dj + dk) / (3 * (ell._a + ell._b))));
if (rel < _fMinReliability)
{
Toc(4); //validation
return;
}
// Assign the new score!
ell._score = (score + rel) * 0.5f;
//ell._score = score;
// The tentative detection has been confirmed. Save it!
ellipses.push_back(ell);
Toc(4); // Validation
};
// Get the coordinates of the center, given the intersection of the estimated lines. See Fig. [8] in Sect [3.2.3] in the paper.
Point2f CEllipseDetectorYaed::GetCenterCoordinates(EllipseData& data_ij, EllipseData& data_ik)
{
float xx[7];
float yy[7];