line smoothing algorithm in python?

前端 未结 2 1549
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 17:14

I am doing research on line generalization, which will be applied to obtain generalized Road Network map from large scale map to small scale map. I am using two operation an

相关标签:
2条回答
  • 2020-12-29 17:23

    I guess you used the code from here. You should have paid attention that the code was for a single dimension data points not for multi-dimension data points.

    I am not much aware of Gaussian smoothing algorithm but after only briefly going through your code, I believe following is what you are trying to do (I am not sure if it gives you the result you desire). Replace last portion of your code with the following code:

    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)
    
    0 讨论(0)
  • 2020-12-29 17:39

    You can smooth the path by following code:

    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)
    

    Here is the resut: blue dots are the original data, red curve are the smoothed curve which contains many points, if you want the same point count as original data, you can sample from the red curve and get the green points.

    You can set sigma to change the smooth level of gaussian_filter1d().

    enter image description here

    0 讨论(0)
提交回复
热议问题