alljoyland 发表于 2008-7-25 16:06

圆拟合问题,源程序及讨论

圆拟合问题描述
作者:alljoyland
Email:wjlmail1@163.com
概述
事实上圆的拟合问题从小的方面来属于曲线拟合,从大一点的范围来说属于参数识别,或者是系统识别,在图形处理,图像识别,以及公差测量,人工智能方面应用比较多.
拟合方法大致的分为两类, 1.代数拟合 2.几何拟合问题
圆拟合问题描述(使用最小二乘的方法)( x - xc )^2 + ( y-yc)^2 = r ^2

(1)
其中,
xc,yc是圆心参数,
r是半径
展开可以得到
x^2 - 2*xc*x + xc^2 + y^2 -2*yc*y +yc^2 = r^2
(2)
变形可以得到
-2*xc*x - 2*yc*y + xc^2 + yc^2 -r^2 = -(x^2 +y^2)
(3)
其中
a = -2*xc
b = -2*yc
(4)
c = xc^2 + yc^2 -r^2
可以得到
a*x +b*y +c = -(x^2 +y^2)
(5)
由此可以构造最小二乘法% solve for parameters a, b, and c in the least-squares sense by
% using the backslash operator
abc=\[-(x.^2+y.^2)];
a = abc(1); b = abc(2); c = abc(3);
由(4)式,并且解出
xc yc 和 r
% calculate the location of the center and the radius
xc = -a/2;
yc = -b/2;
radius
=
sqrt((xc^2+yc^2)-c);


完整的程序如下,使用MATLAB:function = ls_circle_fit(x,y)

% least-squares circle fit


% solve for parameters a, b, and c in the least-squares sense by
% using the backslash operator
abc=\[-(x.^2+y.^2)];
a = abc(1); b = abc(2); c = abc(3);

% calculate the location of the center and the radius
xc = -a/2;
yc = -b/2;
radius
=
sqrt((xc^2+yc^2)-c);


% % if you want to plot the circle, and you can see also
% ls_circle_fit_ex.m
% plot the entire circle
% theta = 0:0.01:2*pi;
%
% % use parametric representation of the circle to obtain coordinates
% % of points on the circle
% Xfit = radius*cos(theta) + xc;
% Yfit = radius*sin(theta) + yc;
%
% plot(Xfit, Yfit);

使用的示例如下图所示:




图表 1
整圆
其源代码如下

r=3;
theta=0:0.1:2*pi;
x=r*sin(theta)+1;
y=r*cos(theta)+10;
xr=rand(1,length(x));
yr=rand(1,length(y));
x=x+xr;
y=y+yr;
figure
plot(x,y)
axis square
=ls_circle_fit(x',y')

theta = 0:0.01:2*pi;
%
% % use parametric representation of the circle to obtain coordinates
% % of points on the circle

Xfit = radius*cos(theta) + xc ;

Yfit = radius*sin(theta) + yc;

%
hold on

plot(Xfit, Yfit,'r');

axis square

plot(xc,yc,'*r')

hold off

title(' red is the circle by fit,and the blue is the rand gen')

如果是非完整圆,应注意使用范围以上源代码的第二句,分别改为
theta=0:0.1:pi;
theta=0:0.1:pi/2;



图表 2
半圆(数据只提供半个圆)



图表 3 1/4圆(数据只提供1/4圆)
结论从以上的比较可以看出,当圆的数据如果是不完整的时候,是失真的,
尤其当少于半个圆的时候(当然也与这些数据的波动大小有关,如果波动小一点,情况可能有所改善)
其他正交距离拟合可以参考
http://research.microsoft.com/~zhang/INRIA/Publis/Tutorial-Estim/node11.html#SECTION00062000000000000000
圆拟合的代码可以参考
http://google.com/codesearch
关键字
lang:matlab circle fit
Orthogonal distance fitting
正交距离拟合
圆拟合
椭圆拟合



[ 本帖最后由 alljoyland 于 2008-7-25 16:07 编辑 ]

happy 发表于 2008-7-26 14:43

尤其当少于半个圆的时候(当然也与这些数据的波动大小有关,如果波动小一点,情况可能有所改善)

能否解释一下这句话?

alljoyland 发表于 2008-7-30 15:14

原帖由 happy 于 2008-7-26 14:43 发表 http://www.chinavib.com/forum/images/common/back.gif


能否解释一下这句话?
:-) ,
就是有时候的测试数据不是整个圆圈,而是半个圆圈,或者3/4个圆圈,就会效果稍微差点,尤其更少的时候,就会有问题

zcs 发表于 2008-12-4 10:39

楼主能不能讲讲或介绍一下圆的几何拟合方面的问题呢?谢谢

alljoyland 发表于 2008-12-30 13:00

这个问题属于 Orthogonal distance fitting,

这个问题属于 Orthogonal distance fitting,
实际上在 正交距离拟合可以参考
http://research.microsoft.com/~zhang/INRIA/Publis/Tutorial-Estim/node11.html#SECTION00062000000000000000
这个超链接 有些 介绍,是英文的,
但是公式比较详细, 讲得也不错,

此外 涉及到 如最小二乘拟合 之类的知识,

圆拟合 和 椭圆拟合 以及直线拟合 因该是比较简单的
有很多的论文论及
页: [1]
查看完整版本: 圆拟合问题,源程序及讨论