Python中的线条平滑算法?

3
我正在研究线路概括,将应用于从大规模地图到小规模地图获得广义道路网络地图。我使用了两种操作和两种算法。这是在Python编程语言中使用shapefile库完成的,适用于2D矢量数据。
操作:选择和消除。 对于选择,我使用条件来选择所有道路,宽度大于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中有其他算法可以使用线条中每个点的坐标来平滑矢量数据中的线条。
感谢任何回答!

你得到了什么错误? - cyroxx
为了帮助您解决这个错误,只提供了相关的高斯平滑信息。选择和排除是无关的信息。请保持信息简洁,这将有助于他人帮助您。当您说出现错误时,请具体说明您遇到了什么错误。仅仅说它给了帮助是不够有用的。 - jitendra
2个回答

16
您可以通过以下代码使路径平滑:
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


1
我猜你使用了这里的代码。你应该注意到,这段代码是用于单维数据点而不是多维数据点的。
我不太了解高斯平滑算法,但在仅简要查看您的代码后,我相信以下是您试图做的(我不确定它是否给您想要的结果)。请使用以下代码替换您代码的最后一部分:
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)

但是当我替换代码时,它会出现一些错误:[1. 1. 1. 1. 1.] [0.077304740443299699, 0.52729242404304844, 1.0, 0.52729242404304844, 0.077304740443299699] [0.07730474 0.52729242 1. 0.52729242 0.07730474] 1 [0.0, 0.0]Traceback (most recent call last): File "C:/Users/User/Desktop/go_smo.py", line 44, in <module> smoothListGaussian(a,3) File "C:/Users/User/Desktop/go_smo.py", line 34, in smoothListGaussian smoothing[0.0,0.0] UnboundLocalError: 在赋值之前引用了本地变量'smoothing' - peter
@peter 当我运行时很好用。你确定你使用了正确的缩进和一切吗? - jitendra
是的,它正在工作,我犯了一个错误,对此感到抱歉,谢谢! - peter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接