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
|
bool LineFit::LineFitSolve(const std::vector<cv::Point2d>& points, const std::vector<double>& weights, double&a, double& b)
{
_function.params = (void *)&points;
// 用最小二乘拟合的结果作为初始值
if (!LeastSquareLineFit(points, weights, a, b))
{
return false;
}
// 设置初始值
SetStartPoint(a, b);
//经验值,初始步长十分之一
gsl_vector_set(_stepSize, 0, a / 10.0);
gsl_vector_set(_stepSize, 1, b / 10.0);
gsl_multimin_fminimizer_set(_fminimizer, &_function, _startPoint, _stepSize);
int iter = 0;
int status;
do
{
iter++;
status = gsl_multimin_fminimizer_iterate(_fminimizer);
if (status == GSL_ENOPROG) // 表示无法找到更好的解了
{
break;
}
double size = gsl_multimin_fminimizer_size(_fminimizer);
status = gsl_multimin_test_size(size, 1e-2);
} while (status == GSL_CONTINUE && iter < _maxIter);
gsl_vector * out = gsl_multimin_fminimizer_x(_fminimizer);
a = gsl_vector_get(out, 0);
b = gsl_vector_get(out, 1);
}
double LineFit::L1Distance(const gsl_vector * v, void * params)
{
std::vector<cv::Point2d> *vect = static_cast<std::vector<cv::Point2d>*>(params);
int N = vect->size();
double a, b;
a = gsl_vector_get(v, 0);
b = gsl_vector_get(v, 1);
double sum = 0;
for (int i = 0; i < N; i++)
{
double yi = vect->at(i).y;
double fi = a * vect->at(i).x + b;
double dist = yi - fi;
sum += std::fabs(dist);
}
return sum / N;
}
void LineFit::SetStartPoint(const double& a, const double& b)
{
gsl_vector_set(_startPoint, 0, a);
gsl_vector_set(_startPoint, 1, b);
}
bool LeastSquareCirleFit(const std::vector<cv::Point2d>& points, const std::vector<double>& weights, cv::Point2d& circleCenter, double& radius)
{
//检查输入参数 | Check input parameters
if (points.size() < 3 || (points.size() != weights.size()))
{
return false;
}
//构造矩阵 | Construct mat
double XiSum = 0;
double Xi2Sum = 0;
double Xi3Sum = 0;
double YiSum = 0;
double Yi2Sum = 0;
double Yi3Sum = 0;
double XiYiSum = 0;
double Xi2YiSum = 0;
double XiYi2Sum = 0;
double WiSum = 0;
for (size_t i = 0; i < points.size(); i++)
{
XiSum += points.at(i).x * weights.at(i);
Xi2Sum += points.at(i).x * points.at(i).x * weights.at(i);
Xi3Sum += points.at(i).x * points.at(i).x * points.at(i).x * weights.at(i);
YiSum += points.at(i).y * weights.at(i);
Yi2Sum += points.at(i).y * points.at(i).y * weights.at(i);
Yi3Sum += points.at(i).y * points.at(i).y * points.at(i).y * weights.at(i);
XiYiSum += points.at(i).x * points.at(i).y * weights.at(i);
Xi2YiSum += points.at(i).x * points.at(i).x * points.at(i).y * weights.at(i);
XiYi2Sum += points.at(i).x * points.at(i).y * points.at(i).y * weights.at(i);
WiSum += weights.at(i);
}
const int N = 3;
cv::Mat A = cv::Mat::zeros(N, N, CV_64FC1);
cv::Mat B = cv::Mat::zeros(N, 1, CV_64FC1);
A.at<double>(0, 0) = Xi2Sum;
A.at<double>(0, 1) = XiYiSum;
A.at<double>(0, 2) = XiSum;
A.at<double>(1, 0) = XiYiSum;
A.at<double>(1, 1) = Yi2Sum;
A.at<double>(1, 2) = YiSum;
A.at<double>(2, 0) = XiSum;
A.at<double>(2, 1) = YiSum;
A.at<double>(2, 2) = WiSum;
B.at<double>(0, 0) = -(Xi3Sum + XiYi2Sum);
B.at<double>(1, 0) = -(Xi2YiSum + Yi3Sum);
B.at<double>(2, 0) = -(Xi2Sum + Yi2Sum);
//解矩阵 | Solve
//求解A*X = B | Solve the A*X = B
cv::Mat X;
cv::solve(A, B, X, cv::DECOMP_LU);
double a = X.at<double>(0, 0);
double b = X.at<double>(1, 0);
double c = X.at<double>(2, 0);
//计算圆心和半径 | Calculate center and radius.
circleCenter.x = -0.5 * a;
circleCenter.y = -0.5 * b;
radius = 0.5 * std::sqrt(a * a + b * b - 4 * c);
return true;
}
LineFit::LineFit()
{
_maxIter = 100; // 默认最大迭代 100 步
_function.n = 2; //变量个数
_function.f = L1Distance;
_startPoint = gsl_vector_alloc(_function.n);
_stepSize = gsl_vector_alloc(_function.n);
_fminimizer = gsl_multimin_fminimizer_alloc(gsl_multimin_fminimizer_nmsimplex, 2);
}
LineFit::~LineFit()
{
// 释放资源
gsl_vector_free(_startPoint);
gsl_vector_free(_stepSize);
gsl_multimin_fminimizer_free(_fminimizer);
}
bool LeastSquareLineFit(const std::vector<cv::Point2d>& points, const std::vector<double>& weights, double&a, double& b)
{
// AX = B
// 构造A矩阵 | Construct A mat
const int N = 2;
cv::Mat A = cv::Mat::zeros(N, N, CV_64FC1);
for (int row = 0; row < A.rows; row++)
{
for (int col = 0; col < A.cols; col++)
{
for (int k = 0; k < points.size(); k++)
{
A.at<double>(row, col) = A.at<double>(row, col) + pow(points[k].x, row + col) * weights[k];
}
}
}
//构造B矩阵 | Construct B mat
cv::Mat B = cv::Mat::zeros(N, 1, CV_64FC1);
for (int row = 0; row < B.rows; row++)
{
for (int k = 0; k < points.size(); k++)
{
B.at<double>(row, 0) = B.at<double>(row, 0) + pow(points[k].x, row)*points[k].y * weights[k];
}
}
// 求解A*X = B | Solve the A*X = B
cv::Mat X;
cv::solve(A, B, X, cv::DECOMP_LU);
// y = b + ax
b = X.at<double>(0, 0);
a = X.at<double>(1, 0);
return true;
}
|