【问题标题】:line smoothing algorithm in python?python中的线平滑算法?
【发布时间】:2013-02-17 03:49:12
【问题描述】:

我正在研究线路概化,将应用于从大比例尺地图到小比例尺地图的广义路网地图。我正在使用两种操作和两种算法。它是在 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 中有任何其他算法可以使用线条中每个点的坐标来平滑矢量数据中的线条

感谢任何答案!

【问题讨论】:

  • 你得到什么错误?
  • 为了帮助您解决这个错误,只有相关信息是高斯平滑。选择、淘汰是不相关的信息。请保持信息简洁。这将帮助其他人帮助你。而且,当您说您遇到错误时,请指定您遇到什么错误。说它提供帮助是不够的。

标签: python algorithm numpy line smoothing


【解决方案1】:

我猜你使用了来自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)

【讨论】:

  • 但是当我替换代码率时,给出一些错误:[1. 1. 1. 1. 1. 1. 0.04304440 0.07730470 0.0773042 1. 1 [0.0, 0.0] Traceback(最近一次调用最后):文件“C:/Users/User/Desktop/go_smo.py”,第 44 行,在 smoothListGaussian(a,3) 文件“C:/Users/ User/Desktop/go_smo.py", line 34, in smoothListGaussian smoothing[0.0,0.0] UnboundLocalError: local variable 'smoothing' referenced before assignment
  • @peter 在我跑步时工作正常。你确定,你使用的是正确的缩进和一切吗?
  • 是的,它正在工作,我犯了一个错误,很抱歉,谢谢!
【解决方案2】:

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

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()的平滑度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2012-10-10
    • 1970-01-01
    • 2018-03-19
    • 2018-07-05
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多