python中(x,y)点列表的移动平均值

2024-09-30 08:25:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个点数组,[(x,y),…],收集自用户鼠标线条绘制,我想用移动平均法去除其中的噪声

我该怎么做

enter image description here


Tags: 用户绘制数组鼠标噪声线条平均法
2条回答

https://docs.scipy.org/doc/scipy/reference/signal.html#filtering

或者只要使用convolve并在必要时更正边框即可

如果可以在项目中使用opencv,请参见 https://docs.opencv.org/4.5.2/d4/d86/group__imgproc__filter.html 和使用

a = np.array(list_of_points, 'f') # should be floating
kernel_size = 3 # for example
filtered = cv2.blur(a, (1, kernel_size), borderType=cv2.BORDER_REPLICATE)

下面是使用大小为s卷积的示例:

v = np.array([(0, 4), (1, 5), (2, 6), (-1, 9), (3, 7), (4, 8), (5, 9)])
s = 2

kernel = np.ones(s)
x = np.convolve(v[:,0], kernel, 'valid') / s
y = np.convolve(v[:,1], kernel, 'valid') / s
res = np.hstack((x[:, None], y[:, None]))

print(res)

输出:

[[0.5 4.5]
 [1.5 5.5]
 [0.5 7.5]
 [1.  8. ]
 [3.5 7.5]
 [4.5 8.5]]

越大的s,路径越平滑。但是s越大,路径越短

相关问题 更多 >

    热门问题