算法思想:

算法通过最小化约束条件4ac-b^2 = 1,最小化距离误差。利用最小二乘法进行求解,首先引入拉格朗日乘子算法获得等式组,然后求解等式组得到最优的拟合椭圆。

算法的优点:

  a、椭圆的特异性,在任何噪声或者遮挡的情况下都会给出一个有用的结果;

  b、不变性,对数据的Euclidean变换具有不变性,即数据进行一系列的Euclidean变换也不会导致拟合结果的不同;

  c、对噪声具有很高的鲁棒性;

  d、计算高效性。

算法原理:

代码实现(Matlab):

  1. %
  2. function a = fitellipse(X,Y)
  3.  
  4. % FITELLIPSE Least-squares fit of ellipse to 2D points.
  5. % A = FITELLIPSE(X,Y) returns the parameters of the best-fit
  6. % ellipse to 2D points (X,Y).
  7. % The returned vector A contains the center, radii, and orientation
  8. % of the ellipse, stored as (Cx, Cy, Rx, Ry, theta_radians)
  9. %
  10. % Authors: Andrew Fitzgibbon, Maurizio Pilu, Bob Fisher
  11. % Reference: "Direct Least Squares Fitting of Ellipses", IEEE T-PAMI,
  12. %
  13. % @Article{Fitzgibbon99,
  14. % author = "Fitzgibbon, A.~W.and Pilu, M. and Fisher, R.~B.",
  15. % title = "Direct least-squares fitting of ellipses",
  16. % journal = pami,
  17. % year = 1999,
  18. % volume = 21,
  19. % number = 5,
  20. % month = may,
  21. % pages = "476--480"
  22. % }
  23. %
  24. % This is a more bulletproof version than that in the paper, incorporating
  25. % scaling to reduce roundoff error, correction of behaviour when the input
  26. % data are on a perfect hyperbola, and returns the geometric parameters
  27. % of the ellipse, rather than the coefficients of the quadratic form.
  28. %
  29. % Example: Run fitellipse without any arguments to get a demo
  30. if nargin ==
  31. % Create an ellipse
  32. t = linspace(,);
  33.  
  34. Rx = ;
  35. Ry = ;
  36. Cx = ;
  37. Cy = ;
  38. Rotation = .; % Radians
  39.  
  40. NoiseLevel = .; % Will add Gaussian noise of this std.dev. to points
  41.  
  42. x = Rx * cos(t);
  43. y = Ry * sin(t);
  44. nx = x*cos(Rotation)-y*sin(Rotation) + Cx + randn(size(t))*NoiseLevel;
  45. ny = x*sin(Rotation)+y*cos(Rotation) + Cy + randn(size(t))*NoiseLevel;
  46.  
  47. % Clear figure
  48. clf
  49. % Draw it
  50. plot(nx,ny,'o');
  51. % Show the window
  52. figure(gcf)
  53. % Fit it
  54. params = fitellipse(nx,ny);
  55. % Note it may return (Rotation - pi/) and swapped radii, this is fine.
  56. Given = round([Cx Cy Rx Ry Rotation*])
  57. Returned = round(params.*[ ])
  58.  
  59. % Draw the returned ellipse
  60. t = linspace(,pi*);
  61. x = params() * cos(t);
  62. y = params() * sin(t);
  63. nx = x*cos(params())-y*sin(params()) + params();
  64. ny = x*sin(params())+y*cos(params()) + params();
  65. hold on
  66. plot(nx,ny,'r-')
  67.  
  68. return
  69. end
  70.  
  71. % normalize data
  72. mx = mean(X);
  73. my = mean(Y);
  74. sx = (max(X)-min(X))/;
  75. sy = (max(Y)-min(Y))/;
  76.  
  77. x = (X-mx)/sx;
  78. y = (Y-my)/sy;
  79.  
  80. % Force to column vectors
  81. x = x(:);
  82. y = y(:);
  83.  
  84. % Build design matrix
  85. D = [ x.*x x.*y y.*y x y ones(size(x)) ];
  86.  
  87. % Build scatter matrix
  88. S = D'*D;
  89.  
  90. % Build 6x6 constraint matrix
  91. C(,) = ; C(,) = -; C(,) = ; C(,) = -;
  92.  
  93. % Solve eigensystem
  94. if
  95. % Old way, numerically unstable if not implemented in matlab
  96. [gevec, geval] = eig(S,C);
  97.  
  98. % Find the negative eigenvalue
  99. I = find(real(diag(geval)) < 1e-8 & ~isinf(diag(geval)));
  100.  
  101. % Extract eigenvector corresponding to negative eigenvalue
  102. A = real(gevec(:,I));
  103. else
  104. % New way, numerically stabler in C [gevec, geval] = eig(S,C);
  105.  
  106. % Break into blocks
  107. tmpA = S(:,:);
  108. tmpB = S(:,:);
  109. tmpC = S(:,:);
  110. tmpD = C(:,:);
  111. tmpE = inv(tmpC)*tmpB';
  112. [evec_x, eval_x] = eig(inv(tmpD) * (tmpA - tmpB*tmpE));
  113.  
  114. % Find the positive (as det(tmpD) < ) eigenvalue
  115. I = find(real(diag(eval_x)) < 1e-8 & ~isinf(diag(eval_x)));
  116.  
  117. % Extract eigenvector corresponding to negative eigenvalue
  118. A = real(evec_x(:,I));
  119.  
  120. % Recover the bottom half...
  121. evec_y = -tmpE * A;
  122. A = [A; evec_y];
  123. end
  124.  
  125. % unnormalize
  126. par = [
  127. A()*sy*sy, ...
  128. A()*sx*sy, ...
  129. A()*sx*sx, ...
  130. -*A()*sy*sy*mx - A()*sx*sy*my + A()*sx*sy*sy, ...
  131. -A()*sx*sy*mx - *A()*sx*sx*my + A()*sx*sx*sy, ...
  132. A()*sy*sy*mx*mx + A()*sx*sy*mx*my + A()*sx*sx*my*my ...
  133. - A()*sx*sy*sy*mx - A()*sx*sx*sy*my ...
  134. + A()*sx*sx*sy*sy ...
  135. ]';
  136.  
  137. % Convert to geometric radii, and centers
  138.  
  139. thetarad = 0.5*atan2(par(),par() - par());
  140. cost = cos(thetarad);
  141. sint = sin(thetarad);
  142. sin_squared = sint.*sint;
  143. cos_squared = cost.*cost;
  144. cos_sin = sint .* cost;
  145.  
  146. Ao = par();
  147. Au = par() .* cost + par() .* sint;
  148. Av = - par() .* sint + par() .* cost;
  149. Auu = par() .* cos_squared + par() .* sin_squared + par() .* cos_sin;
  150. Avv = par() .* sin_squared + par() .* cos_squared - par() .* cos_sin;
  151.  
  152. % ROTATED = [Ao Au Av Auu Avv]
  153.  
  154. tuCentre = - Au./(.*Auu);
  155. tvCentre = - Av./(.*Avv);
  156. wCentre = Ao - Auu.*tuCentre.*tuCentre - Avv.*tvCentre.*tvCentre;
  157.  
  158. uCentre = tuCentre .* cost - tvCentre .* sint;
  159. vCentre = tuCentre .* sint + tvCentre .* cost;
  160.  
  161. Ru = -wCentre./Auu;
  162. Rv = -wCentre./Avv;
  163.  
  164. Ru = sqrt(abs(Ru)).*sign(Ru);
  165. Rv = sqrt(abs(Rv)).*sign(Rv);
  166.  
  167. a = [uCentre, vCentre, Ru, Rv, thetarad];

实验效果:

a、同等噪声条件下,不同长度的样本点,导致的拟合结果,如下所示:

b、相同长度的样本点下,不同噪声的样本点,导致的拟合结果,如下所示:

c、少样本点下,拟合结果如下:

源码下载:

      地址: FitEllipse

参考文献:

[1]. Andrew W. Fitzgibbon, Maurizio Pilu and Robert B. Fisher. Direct Least Squares Fitting of Ellipses. 1996.

[2]. http://research.microsoft.com/en-us/um/people/awf/ellipse/

基于直接最小二乘的椭圆拟合(Direct Least Squares Fitting of Ellipses)的更多相关文章

  1. 基于MATLAB的多项式数据拟合方法研究-毕业论文

    摘要:本论文先介绍了多项式数据拟合的相关背景,以及对整个课题做了一个完整的认识.接下来对拟合模型,多项式数学原理进行了详细的讲解,通过对文献的阅读以及自己的知识积累对原理有了一个系统的认识.介绍多项式 ...

  2. opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓

    一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...

  3. 【Matlab&Mathematica】对三维空间上的点进行椭圆拟合

    问题是这样:比如有一个地心惯性系的轨道,然后从轨道上取了几个点,问能不能根据这几个点把轨道还原了? 当然,如果知道轨道这几个点的速度的情况下,根据轨道六根数也是能计算轨道的,不过真近点角是随时间变动的 ...

  4. 基于EM的多直线拟合

    作者:桂. 时间:2017-03-22  06:13:50 链接:http://www.cnblogs.com/xingshansi/p/6597796.html 声明:欢迎被转载,不过记得注明出处哦 ...

  5. 基于EM的多直线拟合实现及思考

    作者:桂. 时间:2017-03-22  06:13:50 链接:http://www.cnblogs.com/xingshansi/p/6597796.html 声明:欢迎被转载,不过记得注明出处哦 ...

  6. Dlib Opencv cv2.fitEllipse用于人眼轮廓椭圆拟合

    dlib库的安装以及人脸特征点的识别分布分别在前两篇博文里面 Dlib Python 检测人脸特征点 Face Landmark Detection Mac OSX下安装dlib (Python) 这 ...

  7. 6、基于highcharts实现的线性拟合,计算部分在java中实现,画的是正态概率图

    1.坐标点类 package cn.test.domain; public class Point { double x; double y; public Point(){ } public Poi ...

  8. C# + Matlab 实现计件工时基于三层BP神经网络的拟合--真实项目

    工序工时由该工序的工艺参数决定,有了工时后乘以固定因子就是计件工资.一般参考本地小时工资以及同类小时工资并考虑作业的风险等因素给出固定因子 采用的VS2010 , Matlab2015a 64,  开 ...

  9. 【翻译】拟合与高斯分布 [Curve fitting and the Gaussian distribution]

    参考与前言 英文原版 Original English Version:https://fabiandablander.com/r/Curve-Fitting-Gaussian.html 如何通俗易懂 ...

随机推荐

  1. 七、springcloud之配置中心Config(二)之高可用集群

    方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...

  2. React 学习一 运行

    最近项目准备使用React作为前端,主要第一比较火,第二比较小.抽空先来学习一下. 首先下载资源文件:压缩后不到50KB,是挺小的哦. 其中:react.js 是 React 的核心库,react-d ...

  3. android设备休眠

    从上面的连接里面找到了一些资料: 如果一开始就对Android手机的硬件架构有一定的了解,设计出的应用程序通常不会成为待机电池杀手,而要设计出正确的通信机制与通信协议也并不困难.但如果不去了解而盲目设 ...

  4. 虚拟机 windows xp sp3 原版

    原版的镜像:http://www.7xdown.com/Download.asp?ID=3319&URL=http://d5.7xdown.com/soft2/&file=Window ...

  5. Javascript之浏览器兼容EventUtil

    var EventUtil = { //添加事件处理程序 addHandler: function (element, type, handler) { if (element.addEventLis ...

  6. es6遍历数组forof

  7. day1作业:编写登录窗口一个文件实现

    思路: 1.参考模型,这个作业我参考了linux的登录认证流程以及结合网上银行支付宝等锁定规则: 1)认证流程参考的是Linux的登录:当你输入完用户名密码后再验证用户名是否存在用户是否被锁定,然后在 ...

  8. mysql 删除重复项

    DELETE FROM j_rank_rise_record WHERE id NOT IN ( SELECT id FROM ( SELECT * FROM j_rank_rise_record g ...

  9. 全面兼容的Iframe 与父页面交互操作

     父页面 Father.htm 源码如下:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  10. thinkphp中I()方法的详解

    I('post.email','','email'); int boolean float validate_regexp validate_url validate_email validate_i ...