上一篇博客介绍了最小二乘法拟合圆的方法。这种方法对误差符合正态分布的数据点很有效。但是在机器视觉应用中经常会碰到一些干扰点。这些干扰点多数时候是偏向某一个方向的。这时要是用最小二乘法拟合,拟合出的圆会偏很多。因此,有必要研究更有效的拟合算法。
这里介绍一个我常用的拟合算法,根据数据点到圆的距离绝对值的和来确定圆的参数,也就是下面这个式子:
f=∑∣∣(xi−xc)2+(yi−yc)2−−−−−−−−−−−−−−−−−−√−R∣∣f=∑|(xi−xc)2+(yi−yc)2−R|
使得ff取得最小值的xcxc、ycyc和RR就是最佳拟合参数。
用这个式子没有解析解,只能靠数值算法。这里我采用了 GSL (GNU Scientific Library )里的多维函数求极值的功能。关于如何安装、使用 GSL 等问题我有其他的博客介绍了,这里就不多说了。下面给代码。
建立了一个类,叫做 “CircleFitSolver”。把所有的计算都封装到这个类中。
#ifndef CIRCLEFITSOLVER_H#define CIRCLEFITSOLVER_H#include #include #include #include using namespace std;typedef complex POINT;bool circleLeastFit(const std::vector &points, double ¢er_x, double ¢er_y, double &radius);class CircleFitSolver
{public:
CircleFitSolver();
~CircleFitSolver();
void setMaxIter(int iter) {m_max_iter = iter;}
/**
* @brief circleFitL1 拟合圆,拟合判据为数据点到拟合圆的距离绝对值之和最小。
* @param points 输入参数,存储各个数据点。
* @param center_x radius > 0 时作为迭代算法的初始值。计算完成后返回拟合圆的圆心 X 坐标
* @param center_y radius > 0 时作为迭代算法的初始值。计算完成后返回拟合圆的圆心 Y 坐标
* @param radius radius < 0 时,用最小二乘拟合的结果作为迭代算法的初始值。计算完成后返回拟合圆的半径。
* @return true 表示拟合成功,否则拟合失败。
*/ bool circleFitL1(const vector &points, double ¢er_x, double ¢er_y, double &radius);private:
gsl_multimin_function m_function;
gsl_multimin_fminimizer * m_fminimizer;
int m_max_iter; // 迭代算法的最大迭代次数 gsl_vector *m_start_point; // 迭代算法的初始值 gsl_vector *m_step_size; // 迭代算法的初始步长 void setStartPoint(double center_x, double center_y, double radius);
static double L1_distance(const gsl_vector * v, void * params);
};#endif// CIRCLEFITSOLVER_H
具体的实现代码如下:
#include "circlefitsolver.h"#include using namespace std;/**
* 最小二乘法拟合圆
* 拟合出的圆以圆心坐标和半径的形式表示
* 此代码改编自 newsmth.net 的 jingxing 在 Graphics 版贴出的代码。
* 版权归 jingxing, 我只是搬运工外加一些简单的修改工作。
*/bool circleLeastFit(const std::vector &points, double ¢er_x, double ¢er_y, double &radius)
{
center_x = 0.0f;
center_y = 0.0f;
radius = 0.0f;
if (points.size() < 3)
{
return false;
}
double sum_x = 0.0f, sum_y = 0.0f;
double sum_x2 = 0.0f, sum_y2 = 0.0f;
double sum_x3 = 0.0f, sum_y3 = 0.0f;
double sum_xy = 0.0f, sum_x1y2 = 0.0f, sum_x2y1 = 0.0f;
int N = points.size();
for (int i = 0; i < N; i++)
{
double x = points[i].real();
double y = points[i].imag();
double x2 = x * x;
double y2 = y * y;
sum_x += x;
sum_y += y;
sum_x2 += x2;
sum_y2 += y2;
sum_x3 += x2 * x;
sum_y3 += y2 * y;
sum_xy += x * y;
sum_x1y2 += x * y2;
sum_x2y1 += x2 * y;
}
double C, D, E, G, H;
double a, b, c;
C = N * sum_x2 - sum_x * sum_x;
D = N * sum_xy - sum_x * sum_y;
E = N * sum_x3 + N * sum_x1y2 - (sum_x2 + sum_y2) * sum_x;
G = N * sum_y2 - sum_y * sum_y;
H = N * sum_x2y1 + N * sum_y3 - (sum_x2 + sum_y2) * sum_y;
a = (H * D - E * G) / (C * G - D * D);
b = (H * C - E * D) / (D * D - G * C);
c = -(a * sum_x + b * sum_y + sum_x2 + sum_y2) / N;
center_x = a / (-2);
center_y = b / (-2);
radius = sqrt(a * a + b * b - 4 * c) / 2;
return true;
}double CircleFitSolver::L1_distance(const gsl_vector * v, void * params)
{
vector *vect = (vector *)params;
int N = vect->size();
double a, b, r;
a = gsl_vector_get(v, 0);
b = gsl_vector_get(v, 1);
r = gsl_vector_get(v, 2);
double sum = 0;
for(int i = 0; i < N; i++)
{
const POINT p = vect->at(i);
double xi = p.real() - a;
double yi = p.imag() - b;
double dist = sqrt(xi * xi + yi * yi) - r;
sum += fabs(dist);
}
return sum;
}inline void CircleFitSolver::setStartPoint(double center_x, double center_y, double radius)
{
gsl_vector_set (m_start_point, 0, center_x);
gsl_vector_set (m_start_point, 1, center_y);
gsl_vector_set (m_start_point, 2, radius);
}bool CircleFitSolver::circleFitL1(const vector &points, double ¢er_x, double ¢er_y, double &radius)
{
m_function.params = (void *)&points;
if( radius < 0 )
{
// 用最小二乘拟合的结果作为初始值 if( !circleLeastFit(points, center_x, center_y, radius) )
{
return false;
}
}
setStartPoint(center_x, center_y, radius);
/* 经验值,初始步长设置为半径的十分之一 */ gsl_vector_set (m_step_size, 0, radius / 10.0);
gsl_vector_set (m_step_size, 1, radius / 10.0);
gsl_vector_set (m_step_size, 2, radius / 10.0);
gsl_multimin_fminimizer_set(m_fminimizer, &m_function, m_start_point, m_step_size);
int iter = 0;
int status;
do {
iter++;
status = gsl_multimin_fminimizer_iterate(m_fminimizer);
if (status == GSL_ENOPROG ) // 表示无法找到更好的解了 {
break;
}
double size = gsl_multimin_fminimizer_size (m_fminimizer);
status = gsl_multimin_test_size (size, 1e-2);
}
while (status == GSL_CONTINUE && iter < m_max_iter);
gsl_vector * out = gsl_multimin_fminimizer_x(m_fminimizer);
center_x = gsl_vector_get(out, 0);
center_y = gsl_vector_get(out, 1);
radius = gsl_vector_get(out, 2);
return true;
}
CircleFitSolver::CircleFitSolver()
{
m_max_iter = 100; // 默认最大迭代 100 步 m_function.n = 3;
m_function.f = L1_distance;
m_start_point = gsl_vector_alloc (m_function.n);
m_step_size = gsl_vector_alloc (m_function.n);
m_fminimizer = gsl_multimin_fminimizer_alloc(gsl_multimin_fminimizer_nmsimplex, 3);
}
CircleFitSolver::~CircleFitSolver()
{
gsl_vector_free(m_start_point);
gsl_vector_free(m_step_size);
gsl_multimin_fminimizer_free(m_fminimizer);
}
这个代码基本上就是中规中矩的计算,没太多可说的。唯一一个小技巧就是用最小二乘法的结果作为迭代的初始值。这样很快就能收敛。
下面给个图,说说它的效果吧。