1.圆方程定义
常规圆方程定义为
(
x
−
x
0
)
2
+
(
y
−
y
0
)
2
=
r
2
(x-x_0)^2+(y-y_0)^2 = r^2
(x−x0)2+(y−y0)2=r2
可以改写为
x
2
+
y
2
+
a
x
+
b
y
+
c
=
0.
x^2+y^2+ax+by+c=0.
x2+y2+ax+by+c=0.
其中
a
=
−
2
x
0
,
b
=
−
2
y
0
,
c
=
x
0
2
+
y
0
2
−
r
2
a=-2x_0,b=-2y_0,c={x_0}^2+{y_0}^2-r^2
a=−2x0,b=−2y0,c=x02+y02−r2文章来源:https://www.toymoban.com/news/detail-421271.html
2.最小能量函数定义
要使得圆方程最为准确,则要是所有的点尽可能满足方程,因此需使函数F
F
=
∑
(
x
i
2
+
y
i
2
+
a
x
i
+
b
y
i
+
c
)
2
=
0
,
i
∈
[
1
,
N
]
,
N
为
点
数
目
F=\sum( {x_i}^2+{y_i}^2+a{x_i}+b{y_i}+c)^2 =0 , i\in [1,N],N为点数目
F=∑(xi2+yi2+axi+byi+c)2=0,i∈[1,N],N为点数目
但实际上其不可能为0,因此根据最小二乘法求解其最小值
对函数F求导可得
∂
F
a
=
∑
2
(
x
i
2
+
y
i
2
+
a
x
i
+
b
y
i
+
c
)
∗
x
i
\frac{\partial{F}}{a} = \sum{2( {x_i}^2+{y_i}^2+a{x_i}+b{y_i}+c)*x_i}
a∂F=∑2(xi2+yi2+axi+byi+c)∗xi
∂
F
b
=
∑
2
(
x
i
2
+
y
i
2
+
a
x
i
+
b
y
i
+
c
)
∗
y
i
\frac{\partial{F}}{b} = \sum{2( {x_i}^2+{y_i}^2+a{x_i}+b{y_i}+c)*y_i}
b∂F=∑2(xi2+yi2+axi+byi+c)∗yi
∂
F
c
=
∑
2
(
x
i
2
+
y
i
2
+
a
x
i
+
b
y
i
+
c
)
\frac{\partial{F}}{c} = \sum{2( {x_i}^2+{y_i}^2+a{x_i}+b{y_i}+c)}
c∂F=∑2(xi2+yi2+axi+byi+c)
当这些导数为0是,函数最小
为方便求解,将其写成a,b,c的矩阵函数形式,则可得
接下来便是求解方程AX=B文章来源地址https://www.toymoban.com/news/detail-421271.html
3.matlab代码
- 实例
% 圆拟合实例
clc;
close all;
%% 创建测试数据
num =1000;
% 噪声
rng(1);
noise1=0.2*randn(num,1);
rng(2);
noise2=0.2*randn(num,1);
% 创建初始数据
theta=(2*rand(num,1)-1)*pi;
center = [10,10];
r=3;
x= center(1) + r*cos(theta)+noise1;
y= center(2) + r*sin(theta)+noise2;
%% 拟合圆
figure;
scatter(x,y);
hold on;
[a,b,c] = fit_circle(x,y);
center_cal=[-a/2,-b/2];
r_cal = sqrt(a*a/4+b*b/4-c);
theta_cal =0:0.01:2*pi;
x_cal= center_cal(1) + r_cal*cos(theta_cal);
y_cal= center_cal(2) + r_cal*sin(theta_cal);
plot(x_cal,y_cal);
- 圆拟合函数
function [a,b,c] = fit_circle(x,y)
% function fit_circle.m
% -------------------------------------------------------------------------
% 圆拟合
% 圆方程按照x^2+y^2+ax+by+c=0
% 最小化函数定义为 sum((x_i)^2+(y_i)^2+a(x_i)+b(y_i)+c),i为1到n的整数
% 计算导数,化为AX=B的形式,X为[a;b;c]
% Inputs:
% x = 点x坐标,写成(1000,1)竖排格式
% y = 点y坐标
% Output:
% a,b,c = 圆方程对应参数
% Last updated: November 14, 2022
% 构建A
A1_1 = sum(x.^2);
A1_2 = sum(x.*y);
A1_3 = sum(x);
A2_1 = A1_2;
A2_2 = sum(y.^2);
A2_3 = sum(y);
A3_1 = A1_3;
A3_2 = A2_3;
A3_3 = size(x,1);
A=[A1_1,A1_2,A1_3;
A2_1,A2_2,A2_3;
A3_1,A3_2,A3_3];
% 构建B
B1 = -sum(x.^3+(y.^2).*x);
B2 = -sum(y.^3+(x.^2).*y);
B3 = -sum(x.^2+y.^2);
B = [B1;B2;B3];
% 计算
X = A\B;
a=X(1);
b=X(2);
c=X(3);
end
到了这里,关于标准二维圆拟合(matlab代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!