-
Notifications
You must be signed in to change notification settings - Fork 52
/
ransac_ellipse2d.cpp
322 lines (264 loc) · 9.21 KB
/
ransac_ellipse2d.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
#ifndef RANSAC_ELLIPSE2D_IMPL_H_
#define RANSAC_ELLIPSE2D_IMPL_H_
#include "ransac_ellipse2D.h"
#include<cv.h>
#include<highgui.h>
namespace sac
{
bool ransacModelEllipse2D::isGoodSample(const std::vector<int> &samples) const
{
if (samples.size() != 5)
return false;
double aa[30] = { 0 };
for (size_t i = 0; i < 5; i++)
{
double x = input_[samples[i]].x;
double y = input_[samples[i]].y;
aa[6 * i] = x*x;
aa[6 * i + 1] = x*y;
aa[6 * i + 2] = y*y;
aa[6 * i + 3] = x;
aa[6 * i + 4] = y;
aa[6 * i + 5] = 1;
}
CvMat A = cvMat(5, 6, CV_64F, aa);
CvMat* D = cvCreateMat(5, 6, CV_64F);
CvMat* U = cvCreateMat(5, 5, CV_64F);
CvMat* V = cvCreateMat(6, 6, CV_64F);
cvSVD(&A, D, U, V, CV_SVD_U_T);
double V05 = cvmGet(V, 0, 5);
if (abs(V05) < 0.000001)
return false;
double epA = 1;
double epB = cvmGet(V, 1, 5) / V05;
double epC = cvmGet(V, 2, 5) / V05;
double epD = cvmGet(V, 3, 5) / V05;
double epE = cvmGet(V, 4, 5) / V05;
double epF = cvmGet(V, 5, 5) / V05;
cvReleaseMat(&D);
cvReleaseMat(&U);
cvReleaseMat(&V);
//ax^2 + bxy + cy^2 + dx + ey + f = 0;
// | 2a b | |2a b d|
// if | b 2c | >0 && (2a+2c) |b 2c f| < 0
// |d f 2g|
// a,b,c,d,e,f is a ellipse
double thres1 = 4 * epA * epC - epB * epB;
double ellParam[9] = { 2 * epA, epB, epD, epB, 2 * epC, epE, epD, epE, 2 * epF };
CvMat ellParamMat = cvMat(3, 3, CV_64F, ellParam);
double thres2 = cvDet(&ellParamMat)*(epA + epC);
if (thres1 <= 0 || thres2 >= 0)
return false;
return true;
}
bool ransacModelEllipse2D::computeModelCoefficients(const std::vector<int> &samples, ModelCoefficient &model_coefficient)
{
if (samples.size() != 5)
return false;
//https://www.zhihu.com/question/40362085
//http://yester-place.blogspot.jp/2008/08/opencv-cvsvd2.html
//http://www.mathchina.net/dvbbs/dv_rss.asp?s=xhtml&boardid=3&id=408&page=118
//http://m.blog.csdn.net/ningyaliuhebei/article/details/46327681
//http://blog.csdn.net/ningyaliuhebei/article/details/46327681
double aa[30] = { 0 };
for (size_t i = 0; i < 5; i++)
{
double x = input_[samples[i]].x;
double y = input_[samples[i]].y;
aa[6 * i] = x*x;
aa[6 * i + 1] = x*y;
aa[6 * i + 2] = y*y;
aa[6 * i + 3] = x;
aa[6 * i + 4] = y;
aa[6 * i + 5] = 1;
}
CvMat A = cvMat(5, 6, CV_64F, aa);
CvMat* D = cvCreateMat(5, 6, CV_64F);
CvMat* U = cvCreateMat(5, 5, CV_64F);
CvMat* V = cvCreateMat(6, 6, CV_64F);
cvSVD(&A, D, U, V, CV_SVD_U_T);
double V05 = cvmGet(V, 0, 5);
if (abs(V05) < 0.000001)
return false;
double epA = 1;
double epB = cvmGet(V, 1, 5) / V05;
double epC = cvmGet(V, 2, 5) / V05;
double epD = cvmGet(V, 3, 5) / V05;
double epE = cvmGet(V, 4, 5) / V05;
double epF = cvmGet(V, 5, 5) / V05;
cvReleaseMat(&D);
cvReleaseMat(&U);
cvReleaseMat(&V);
//ax^2 + bxy + cy^2 + dx + ey + f = 0;
// | 2a b | |2a b d|
// if | b 2c | >0 && (2a+2c) |b 2c f| < 0
// |d f 2g|
// a,b,c,d,e,f is a ellipse
double thres1 = 4 * epA * epC - epB * epB;
double ellParam[9] = { 2 * epA, epB, epD, epB, 2 * epC, epE, epD, epE, 2 * epF };
CvMat ellParamMat = cvMat(3, 3, CV_64F, ellParam);
double thres2 = cvDet(&ellParamMat)*(epA + epC);
if (thres1 <= 0 || thres2 >= 0)
return false;
double epX = (epB*epE - 2 * epC*epD) / (4 * epA*epC - epB*epB);
double epY = (epB*epD - 2 * epA*epE) / (4 * epA*epC - epB*epB);
double epAngle = 0;
if (abs(epB) <= 0.0001&&epA < epC)
epAngle = 0;
else if (abs(epB) <= 0.0001&&epA > epC)
epAngle = 90;
else if (epA < epC)
epAngle = 0.5*atan(epB / (epA - epC)) * 180 / 3.1415926;
else epAngle = 90 + 0.5*atan(epB / (epA - epC)) * 180 / 3.1415926;
double epTemp1 = epA*epX*epX + epC*epY*epY + epB*epX*epY - epF;
double epTemp2 = epA + epC;
double epTemp3 = sqrt((epA - epC)*(epA - epC) + epB*epB);
double epSAxis = sqrt(2 * epTemp1 / (epTemp2 + epTemp3));
double epLAxis = sqrt(2 * epTemp1 / (epTemp2 - epTemp3));
if (spLAxis > 0.001&&abs(epLAxis - spLAxis) / spLAxis > spRatio)
return false;
if (spSAxis > 0.001&&abs(epSAxis - spSAxis) / spSAxis > spRatio)
return false;
if (spAngle > 0.001&&abs(epAngle - spAngle) / spAngle > spRatio)
return false;
model_coefficient.modelParam[0] = epX;
model_coefficient.modelParam[1] = epY;
model_coefficient.modelParam[2] = epLAxis;
model_coefficient.modelParam[3] = epSAxis;
model_coefficient.modelParam[4] = epAngle;
return true;
}
int ransacModelEllipse2D::countWithinDistance(const ModelCoefficient model_coefficients, const double threshold)
{
double cx = model_coefficients.modelParam[0];
double cy = model_coefficients.modelParam[1];
double lA = model_coefficients.modelParam[2];
double sA = model_coefficients.modelParam[3];
double angle = model_coefficients.modelParam[4];
double cA = sqrt(lA*lA - sA*sA);
double f1x = cx - cA*cos(angle*3.141592653 / 180);
double f1y = cy - cA*sin(angle*3.141592653 / 180);
double f2x = cx + cA*cos(angle*3.141592653 / 180);
double f2y = cy + cA*sin(angle*3.141592653 / 180);
int count(0);
Point2D cP(cx, cy), cf1(f1x, f1y), cf2(f2x, f2y);
for (size_t i = 0; i < indices_.size(); i++)
{
Point2D iP = input_[indices_[i]];
double fd1 = cf1.calDistance(iP);
double fd2 = cf2.calDistance(iP);
if (abs(fd1 + fd2 - 2 * lA) < threshold)
count++;
}
return count;
}
int ransacModelEllipse2D::countWithinDistance(const ModelCoefficient model_coefficients, const double threshold, double& avgError)
{
avgError = 0;
double cx = model_coefficients.modelParam[0];
double cy = model_coefficients.modelParam[1];
double lA = model_coefficients.modelParam[2];
double sA = model_coefficients.modelParam[3];
double angle = model_coefficients.modelParam[4];
double cA = sqrt(lA*lA - sA*sA);
double f1x = cx - cA*cos(angle*3.141592653 / 180);
double f1y = cy - cA*sin(angle*3.141592653 / 180);
double f2x = cx + cA*cos(angle*3.141592653 / 180);
double f2y = cy + cA*sin(angle*3.141592653 / 180);
int count(0);
Point2D cP(cx, cy), cf1(f1x, f1y), cf2(f2x, f2y);
for (size_t i = 0; i < indices_.size(); i++)
{
Point2D iP = input_[indices_[i]];
double fd1 = cf1.calDistance(iP);
double fd2 = cf2.calDistance(iP);
double cerror = abs(fd1 + fd2 - 2 * lA);
if (cerror < threshold)
{
count++;
avgError += cerror;
}
}
if (count>0)
avgError /= count;
return count;
}
void ransacModelEllipse2D::selectWithinDistance(const ModelCoefficient model_coefficients, const double threshold, std::vector<int> &inliers)
{
double cx = model_coefficients.modelParam[0];
double cy = model_coefficients.modelParam[1];
double lA = model_coefficients.modelParam[2];
double sA = model_coefficients.modelParam[3];
double angle = model_coefficients.modelParam[4];
double cA = sqrt(lA*lA - sA*sA);
double f1x = cx - cA*cos(angle*3.141592653 / 180);
double f1y = cy - cA*sin(angle*3.141592653 / 180);
double f2x = cx + cA*cos(angle*3.141592653 / 180);
double f2y = cy + cA*sin(angle*3.141592653 / 180);
inliers.resize(indices_.size());
error_sqr_dists_.resize(indices_.size());
int count(0);
Point2D cP(cx, cy), cf1(f1x, f1y), cf2(f2x, f2y);
for (size_t i = 0; i < indices_.size(); i++)
{
Point2D iP = input_[indices_[i]];
double fd1 = cf1.calDistance(iP);
double fd2 = cf2.calDistance(iP);
if (abs(fd1 + fd2 - 2 * lA) < threshold)
{
inliers[count] = indices_[i];
error_sqr_dists_[count] = abs(fd1 + fd2 - 2 * lA);
count++;
}
}
inliers.resize(count);
inliers.resize(count);
}
bool ransacModelEllipse2D::computeModel()
{
//warn and exit if no threshold was set
assert(threshold_ != std::numeric_limits<double>::max());
iterations_ = 0;
int n_best_inliers_count = -INT_MAX;
double log_probability = log(1.0 - probability_);
double one_over_indices = 1 / static_cast<double>(getIndices().size());
int n_inliers_count(0);
int skipped_count = 0;
const int max_skip = max_iterations_ * 10;
ModelCoefficient model_coeff;
std::vector<int> selection;
while (iterations_ < max_iterations_ && skipped_count < max_skip)
{
getSamples(iterations_, selection);
assert(selection.size() != 0);
if (!computeModelCoefficients(selection, model_coeff))
{
++skipped_count;
++iterations_;
continue;
}
double cAvgError = 0;
n_inliers_count = countWithinDistance(model_coeff, threshold_, cAvgError);
if (n_inliers_count > n_best_inliers_count)
{
n_best_inliers_count = n_inliers_count;
model_ = selection;
model_coefficients_ = model_coeff;
m_dAvgError = cAvgError;
//compute the k parameter
//TODO
}
iterations_++;
if (iterations_ > max_iterations_)
break;
}
if (model_.size() == 0)
{
inliers_.clear();
return false;
}
selectWithinDistance(model_coefficients_, threshold_, inliers_);
return true;
}
}
#endif