【其他算法】曲线平滑

一、需求

  将一段曲线进行平滑,使得曲线光滑并保持原来的基本形状。

二、方法及原理

  对X/Y两个纬度分别进行平滑,再堆叠成新的点串。

  平滑用Savitzky-Golay滤波,是一种数字滤波器,可以在不改变信号趋势的情况下提高数字的精度。核心思想是,对一定长度窗口内的数据点进行K阶多项式拟合。可以理解为一种移动窗口的加权平均算法,但加权系数不是常数,而是用高阶多项式的最小二乘拟合得出。

三、代码实现

from scipy.signal import savgol_filter as savgol_filter
def smooth(pts):
    window_size, polyorder = 7, 3
    line_x = savgol_filter(pts[:, 0], window_size, polyorder, mode='nearest')
    line_y = savgol_filter(pts[:, 1], window_size, polyorder, mode='nearest')
    line = np.stack([line_x, line_y], axis=1)
    return line
posted @ 2022-12-08 16:28  我若成风者  阅读(574)  评论(0编辑  收藏  举报