Curve Fitting

Reference:
https://cloud.tencent.com/developer/article/1165821

基于Numpy包的polyfit函数实现,其支持的三个参数分别是x点集合、y点集合,以及多项式的幂次。得到多项式方程以后,就可以完整拟合曲线:

代码:

def circle_fitness_demo():
    # 创建图像, 绘制初始点
    image = np.zeros((400, 400, 3), dtype=np.uint8)
    x = np.array([30, 50, 100, 120])
    y = np.array([100, 150, 240, 200])
    for i in range(len(x)):
        cv.circle(image, (x[i], y[i]), 3, (255, 0, 0), -1, 8, 0)

    # 多项式曲线生成
    poly = np.poly1d(np.polyfit(x, y, 3))
    print(poly)

    # 绘制拟合曲线
    for t in range(30, 250, 1):
        y_ = np.int(poly(t))
        cv.circle(image, (t, y_), 1, (0, 0, 255), 1, 8, 0)
    cv.imshow("fit curve", image)
posted @ 2023-03-14 09:39  shendawei  阅读(46)  评论(0)    收藏  举报