You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何量化计算椭圆模型对散点的拟合效果得分

椭圆拟合效果量化评估方案

本站已有大量关于如何为散点集拟合椭圆的解答,但鲜有内容提及如何量化评估椭圆的拟合效果。以下为散点集椭圆拟合的实现代码及运行结果:

import numpy as np
from skimage.measure import EllipseModel
from matplotlib.patches import Ellipse
import matplotlib.pyplot as plt

x1 = [710.804, 710.117, 709.565, 709.036, 707.839, 707.424, 706.889, 705.913, 705.037, 704.58, 703.758,704.105, 704.552, 704.833, 705.204, 706.027, 706.932, 708.041, 708.849, 709.379, 709.797, 710.272,710.494, 710.871, 711.033, 711.018, 710.804]
y1 = [493.076, 491.902, 491.409, 490.947, 490.396, 491.456, 491.887, 492.917, 494.022, 494.882, 496.085,496.934, 497.723, 498.17, 498.656, 498.929, 499.248, 499.156, 498.768, 498.487, 497.853, 497.212,496.753, 495.957, 495.003, 493.997, 493.076]

points = list(zip(x1,y1))
a_points = np.array(points)
x = a_points[:, 0]
y = a_points[:, 1]
ell = EllipseModel()
ell.estimate(a_points)
residuals = ell.residuals(a_points)
xc, yc, a, b, theta = ell.params

print("center = ",  (xc, yc))
print("angle of rotation = ",  theta)
print("axes = ", (a,b))
print('residuals= ', residuals)

fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)
axs[0].scatter(x,y)
axs[1].scatter(x, y)
axs[1].scatter(xc, yc, color='red', s=100)
axs[1].set_xlim(x.min(), x.max())
axs[1].set_ylim(y.min(), y.max())
ell_patch = Ellipse((xc, yc), 2*a, 2*b, theta*180/np.pi, 
edgecolor='red', facecolor='none')
axs[1].add_patch(ell_patch)
plt.show()

运行输出:

center = (707.7248344321281, 495.2907644989805)
angle of rotation = 2.1051122302898926
axes = (4.783618808884847, 2.92938238506285)
residuals= [0.4031933 0.60237637 0.44816215 0.03196744 0.77430711 0.00493033 0.04060345 0.07892221 0.03482433 0.08585788 0.55088573 0.11629398 0.2794519 0.43588704 0.52172484 0.71671995 0.32973538 0.0389334
0.19938749 0.35342937 0.2419931 0.24903068 0.1998942 0.18446699
0.02005871 0.18610954 0.4031933 ]

拟合效果示意图

完成椭圆拟合后,需要量化计算模型对输入散点的拟合优劣程度。目前可通过EllipseModel内置方法获取拟合残差residuals,但该残差为椭圆隐式方程的代数残差,无明确物理意义,无法直接用于通用场景的拟合效果评分。皮尔逊相关系数仅适用于一维列表的对比,无法直接应用于二维点集的拟合评估;此前尝试的残差与质心距离比值、点积计算等方案均存在适配性问题,无法输出稳定可解释的评分结果。


可行量化评分方案

方案1:快速横向对比(无需额外计算)

如果仅需要在同一数据集、同一拟合逻辑下对比不同拟合结果的优劣,可直接基于内置返回的代数残差计算统计指标,指标值越小代表拟合效果越好:

  • 平均绝对残差:np.mean(np.abs(residuals))
  • 残差平方和(SSE):np.sum(residuals**2),也是最小二乘椭圆拟合的优化目标
  • 残差标准差:np.std(residuals),衡量所有点偏离程度的均匀性

注意:该方案的评分仅能做同数据集横向对比,代数残差的数值会随椭圆尺寸、坐标尺度变化,无法跨数据集比较。

方案2:通用几何距离评分(物理意义明确,可跨数据集对比)

要得到可解释、不受坐标尺度影响的通用评分,核心是计算每个散点到拟合椭圆的最短欧氏几何距离,再基于距离计算评分指标。
可直接复用以下代码计算几何距离:

def ellipse_geometric_distances(points, xc, yc, a, b, theta):
    # 坐标变换:平移至椭圆质心、旋转抵消椭圆倾斜,得到标准正椭圆坐标系
    cos_t, sin_t = np.cos(theta), np.sin(theta)
    x_rot = (points[:, 0] - xc) * cos_t + (points[:, 1] - yc) * sin_t
    y_rot = -(points[:, 0] - xc) * sin_t + (points[:, 1] - yc) * cos_t
    
    distances = np.zeros(len(points))
    # 牛顿迭代求每个点到标准椭圆的最短距离,迭代5次精度可达1e-6以上
    for idx, (px, py) in enumerate(zip(x_rot, y_rot)):
        t = np.arctan2(a * py, b * px) # 初始角度近似值
        for _ in range(5):
            cos_p, sin_p = np.cos(t), np.sin(t)
            ex, ey = a * cos_p, b * sin_p
            dx, dy = px - ex, py - ey
            d1 = (-a * sin_p, b * cos_p)
            d2 = (-a * cos_p, -b * sin_p)
            f = dx * d1[0] + dy * d1[1]
            f_d = dx * d2[0] + dy * d2[1] - (d1[0]**2 + d1[1]**2)
            t -= f / f_d
        cos_p, sin_p = np.cos(t), np.sin(t)
        ex, ey = a * cos_p, b * sin_p
        distances[idx] = np.hypot(px - ex, py - ey)
    return distances

# 传入拟合得到的参数即可计算
geo_dists = ellipse_geometric_distances(a_points, xc, yc, a, b, theta)

基于几何距离可计算两个通用评分:

  • 均方根误差(RMSE):np.sqrt(np.mean(geo_dists**2)),单位与原坐标一致,比如示例数据为像素坐标,RMSE=0.3即代表点平均偏离椭圆0.3像素,数值越小拟合越好
  • 类R²拟合优度:1 - np.sum(geo_dists**2) / np.sum((np.hypot(x-xc, y-yc))**2),取值越接近1代表拟合效果越好,值小于0说明拟合效果还不如直接用质心代表点集分布。

方案3:闭合轮廓场景专用评分

如果输入散点是沿椭圆轮廓顺序采样的闭合点(如示例中的首尾相连轮廓点),可使用更贴合轮廓场景的评分:

  • 径向偏差均匀度:将所有点投影到椭圆上得到对应投影点,计算每个点到质心距离与对应投影点到质心距离的比值,统计比值的标准差,数值越接近0代表轮廓和椭圆贴合度越高
  • 面积交并比(IoU):将散点围成的多边形与拟合椭圆做栅格化,计算两者交集面积与并集面积的比值,数值越接近1代表拟合效果越好。

内容的提问来源于stack exchange,提问作者Ninja Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 15:54:16