python中的线平滑算法?

2024-05-08 04:34:13 发布

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

我正在进行在线综合的研究,它将被应用于从大比例尺地图到小比例尺地图的综合路网图的获取。我使用两个操作和两个算法。它是在python编程语言中使用shapefile库完成的,用于二维矢量数据。 操作:选择和消除。 对于选择我使用的条件类似,所有的道路,宽度超过7米的选择,它与道路的属性特征相连接。 与淘汰一样,所有道路宽度小于5米,淘汰。 到目前为止,这并不是什么大问题。

在应用了选择和消除操作之后,我们将得到形状文件、通过条件的道路。 我使用两种算法,直线简化和直线平滑。 为了简化线条,我使用了Douglas Peucker的线条简化算法。它是以矢量数据(坐标)为基础,在公差的基础上去掉一些点。我想用Python编程语言来做。在得到简化的线条之后,它需要一些编辑,比如线条平滑。 在这里,我使用高斯算法,但是它返回了一些错误,我不明白,因为我是编程环境中的新手

import numpy

 ### This is the Gaussian data smoothing function I wrote ###  

def smoothListGaussian(list1,degree):  

     window=degree*2-1  

     weight=numpy.array([1.0]*window)
     print weight

     weightGauss=[]  

     for i in range(window):  

         i=i-degree+1  

         frac=i/float(window)  

         gauss=1/(numpy.exp((4*(frac))**2))  

         weightGauss.append(gauss)  

     print weightGauss
     weight=numpy.array(weightGauss)*weight
     print weight
     print len(list1)-window


     smoothed=[0.0]*(len(list1)-window)
     print smoothed

     for i in range(len(smoothed)):  

         smoothed[i]=sum(numpy.array(list1[i:i+window])*weight)/sum(weight)  

     return smoothed


a=[[78.03881018900006, 30.315651467000066], [78.044901609000078, 30.31512798600005], [78.04927981700007, 30.312510579000048], [78.050041244000056, 30.301755415000059], [78.072646124000073, 30.281720353000082], [78.07902308000007, 30.273344651000059]]

smoothListGaussian(a,3)

有什么想法吗。 或者在python中有其他算法可以使用行中每个点的坐标来平滑矢量数据中的行

欢迎回答!


Tags: 数据numpy算法len矢量windowarray线条
2条回答

我猜你用了here的代码。您应该注意,代码是针对单个维度数据点的,而不是针对多维数据点的。

我不太了解高斯平滑算法,但在简要浏览了您的代码之后,我相信下面是您要做的(我不确定它是否能给您想要的结果)。用以下代码替换代码的最后一部分:

smoothed=[0.0,0.0]*(len(list1)-window)
print smoothed

for i in range(len(smoothed)):
    smoothing=[0.0,0.0]
    for e,w in zip(list1[i:i+window],weight):
        smoothing=smoothing+numpy.multiply(e,w)
    smoothed[i]=smoothing/sum(weight)

可以通过以下代码平滑路径:

from scipy.ndimage import gaussian_filter1d
import numpy as np
a=np.array([[78.03881018900006, 30.315651467000066],
 [78.044901609000078, 30.31512798600005], 
 [78.04927981700007, 30.312510579000048],
 [78.050041244000056, 30.301755415000059],
 [78.072646124000073, 30.281720353000082],
 [78.07902308000007, 30.273344651000059]])

x, y = a.T
t = np.linspace(0, 1, len(x))
t2 = np.linspace(0, 1, 100)

x2 = np.interp(t2, t, x)
y2 = np.interp(t2, t, y)
sigma = 10
x3 = gaussian_filter1d(x2, sigma)
y3 = gaussian_filter1d(y2, sigma)

x4 = np.interp(t, t2, x3)
y4 = np.interp(t, t2, y3)

plot(x, y, "o-", lw=2)
plot(x3, y3, "r", lw=2)
plot(x4, y4, "o", lw=2)

结果是:蓝色点是原始数据,红色曲线是包含多个点的平滑曲线,如果您想要与原始数据相同的点计数,您可以从红色曲线采样并得到绿色点。

您可以设置sigma来更改gaussian_filter1d()的平滑级别。

enter image description here

相关问题 更多 >