This repository is currently being migrated. It's locked while the migration is in progress.
forked from h3ct0r/fast_ellipse_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EllipseDetectorYaed.h
259 lines (193 loc) · 7.85 KB
/
EllipseDetectorYaed.h
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
/*
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
*/
/*
This class implements a very fast ellipse detector, codename: YAED (Yet Another Ellipse Detector)
*/
#pragma once
// #include <cxcore.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <stdio.h>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <vector>
//#include "Ellipse.h"
#include "common.h"
#include <time.h>
using namespace std;
using namespace cv;
//#define DISCARD_CONSTRAINT_OBOX
//#define DISCARD_CONSTRAINT_CONVEXITY
//#define DISCARD_CONSTRAINT_POSITION
//#define DISCARD_CONSTRAINT_CENTER
// Data available after selection strategy.
// They are kept in an associative array to:
// 1) avoid recomputing data when starting from same arcs
// 2) be reused in firther proprecessing
// See Sect [] in the paper
struct EllipseData
{
bool isValid;
float ta;
float tb;
float ra;
float rb;
Point2f Ma;
Point2f Mb;
Point2f Cab;
vector<float> Sa;
vector<float> Sb;
};
class CEllipseDetectorYaed
{
// Parameters
// Preprocessing - Gaussian filter. See Sect [] in the paper
Size _szPreProcessingGaussKernelSize; // size of the Gaussian filter in preprocessing step
double _dPreProcessingGaussSigma; // sigma of the Gaussian filter in the preprocessing step
// Selection strategy - Step 1 - Discard noisy or straight arcs. See Sect [] in the paper
int _iMinEdgeLength; // minimum edge size
float _fMinOrientedRectSide; // minumum size of the oriented bounding box containing the arc
float _fMaxRectAxesRatio; // maximum aspect ratio of the oriented bounding box containing the arc
// Selection strategy - Step 2 - Remove according to mutual convexities. See Sect [] in the paper
float _fThPosition;
// Selection Strategy - Step 3 - Number of points considered for slope estimation when estimating the center. See Sect [] in the paper
unsigned _uNs; // Find at most Ns parallel chords.
// Selection strategy - Step 3 - Discard pairs of arcs if their estimated center is not close enough. See Sect [] in the paper
float _fMaxCenterDistance; // maximum distance in pixel between 2 center points
float _fMaxCenterDistance2; // _fMaxCenterDistance * _fMaxCenterDistance
// Validation - Points within a this threshold are considered to lie on the ellipse contour. See Sect [] in the paper
float _fDistanceToEllipseContour; // maximum distance between a point and the contour. See equation [] in the paper
// Validation - Assign a score. See Sect [] in the paper
float _fMinScore; // minimum score to confirm a detection
float _fMinReliability; // minimum auxiliary score to confirm a detection
bool _bCannyNormalizeForThreshold;
float _fCannyPercentOfPixelsNotEdges;
float _fCannyThresholdRatio;
// auxiliary variables
Size _szImg; // input image size
vector<double> _timesHelper;
vector<double> _times; // _times is a vector containing the execution time of each step.
// _times[0] : time for edge detection
// _times[1] : time for pre processing
// _times[2] : time for grouping
// _times[3] : time for estimation
// _times[4] : time for validation
// _times[5] : time for clustering
int ACC_N_SIZE; // size of accumulator N = B/A
int ACC_R_SIZE; // size of accumulator R = rho = atan(K)
int ACC_A_SIZE; // size of accumulator A
int* accN; // pointer to accumulator N
int* accR; // pointer to accumulator R
int* accA; // pointer to accumulator A
public:
//Constructor and Destructor
CEllipseDetectorYaed(void);
~CEllipseDetectorYaed(void);
void DetectAfterPreProcessing(vector<Ellipse>& ellipses, Mat1b& E, const Mat1f& PHI=Mat1f());
//Detect the ellipses in the gray image
void Detect(Mat1b& gray, vector<Ellipse>& ellipses, Mat1b& edge, Mat1b& blur);
//Draw the first iTopN ellipses on output
void DrawDetectedEllipses(Mat3b& output, vector<Ellipse>& ellipses, int iTopN=0, int thickness=2);
//Set the parameters of the detector
void SetParameters ( Size szPreProcessingGaussKernelSize,
double dPreProcessingGaussSigma,
float fThPosition,
float fMaxCenterDistance,
int iMinEdgeLength,
float fMinOrientedRectSide,
float fDistanceToEllipseContour,
float fMinScore,
float fMinReliability,
int iNs,
bool bCannyNormalizeForThreshold,
float fCannyPercentOfPixelsNotEdges,
float fCannyThresholdRatio
);
// Return the execution time
double GetExecTime() { return _times[0] + _times[1] + _times[2] + _times[3] + _times[4] + _times[5]; }
vector<double> GetTimes() { return _times; }
private:
//keys for hash table
static const ushort PAIR_12 = 0x00;
static const ushort PAIR_23 = 0x01;
static const ushort PAIR_34 = 0x02;
static const ushort PAIR_14 = 0x03;
//generate keys from pair and indicse
uint inline GenerateKey(uchar pair, ushort u, ushort v);
void PreProcessing(Mat1b& I, Mat1b& DP, Mat1b& DN, Mat1b& E, Mat1b& B);
void RemoveShortEdges(Mat1b& edges, Mat1b& clean);
void ClusterEllipses(vector<Ellipse>& ellipses);
int FindMaxK(const vector<int>& v) const;
int FindMaxN(const vector<int>& v) const;
int FindMaxA(const vector<int>& v) const;
int FindMaxK(const int* v) const;
int FindMaxN(const int* v) const;
int FindMaxA(const int* v) const;
float GetMedianSlope(vector<Point2f>& med, Point2f& M, vector<float>& slopes);
void GetFastCenter (vector<Point>& e1, vector<Point>& e2, EllipseData& data);
void DetectEdges13(Mat1b& DP, VVP& points_1, VVP& points_3);
void DetectEdges24(Mat1b& DN, VVP& points_2, VVP& points_4);
void FindEllipses ( Point2f& center,
VP& edge_i,
VP& edge_j,
VP& edge_k,
EllipseData& data_ij,
EllipseData& data_ik,
vector<Ellipse>& ellipses
);
Point2f GetCenterCoordinates(EllipseData& data_ij, EllipseData& data_ik);
Point2f _GetCenterCoordinates(EllipseData& data_ij, EllipseData& data_ik);
void Triplets124 ( VVP& pi,
VVP& pj,
VVP& pk,
unordered_map<uint, EllipseData>& data,
vector<Ellipse>& ellipses
);
void Triplets231 ( VVP& pi,
VVP& pj,
VVP& pk,
unordered_map<uint, EllipseData>& data,
vector<Ellipse>& ellipses
);
void Triplets342 ( VVP& pi,
VVP& pj,
VVP& pk,
unordered_map<uint, EllipseData>& data,
vector<Ellipse>& ellipses
);
void Triplets413 ( VVP& pi,
VVP& pj,
VVP& pk,
unordered_map<uint, EllipseData>& data,
vector<Ellipse>& ellipses
);
void Tic(unsigned idx) //start
{
_timesHelper[idx] = 0.0;
_times[idx] = (double)cv::getTickCount();
};
void Tac(unsigned idx) //restart
{
_timesHelper[idx] = _times[idx];
_times[idx] = (double)cv::getTickCount();
};
void Toc(unsigned idx) //stop
{
_times[idx] = ((double)cv::getTickCount() - _times[idx])*1000. / cv::getTickFrequency();
_times[idx] += _timesHelper[idx];
};
};