声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2698|回复: 4

[编程技巧] 圆拟合问题,源程序及讨论

[复制链接]
发表于 2008-7-25 16:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
圆拟合问题描述
作者: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 y ones(length(x),1)]\[-(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 [radius,xc,yc] = 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 y ones(length(x),1)]\[-(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.GIF

图表 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
[radius xc yc]=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.GIF

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



圆拟合3.GIF

图表 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 编辑 ]

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2008-7-26 14:43 | 显示全部楼层
尤其当少于半个圆的时候(当然也与这些数据的波动大小有关,如果波动小一点,情况可能有所改善)


能否解释一下这句话?
 楼主| 发表于 2008-7-30 15:14 | 显示全部楼层

:-) ,
就是有时候的测试数据不是整个圆圈,而是半个圆圈,或者3/4个圆圈,就会效果稍微差点,尤其更少的时候,就会有问题
发表于 2008-12-4 10:39 | 显示全部楼层
楼主能不能讲讲或介绍一下圆的几何拟合方面的问题呢?谢谢
 楼主| 发表于 2008-12-30 13:00 | 显示全部楼层

这个问题属于 Orthogonal distance fitting,

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

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

圆拟合 和 椭圆拟合 以及直线拟合 因该是比较简单的
有很多的论文论及
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-22 21:35 , Processed in 0.067355 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表