python中的行平滑算法?

pet*_*ter 2 python algorithm numpy line smoothing

我正在研究线路泛化,这将用于获得从大比例尺到小比例尺的广义道路网图.我正在使用两个操作和两个算法.它是使用shapefile库在python编程语言中完成的,它用于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)
Run Code Online (Sandbox Code Playgroud)

请,任何想法.或者,如果python中有任何其他算法,它使用行中每个点的坐标来平滑矢量数据中的行

任何答案赞赏!

HYR*_*YRY 14

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

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)
Run Code Online (Sandbox Code Playgroud)

以下是结果:蓝色圆点是原始数据,红色曲线是包含许多点的平滑曲线,如果您想要与原始数据相同的点数,则可以从红色曲线中采样并获得绿色点.

您可以设置sigma更改平滑级别gaussian_filter1d().

在此输入图像描述