【问题标题】:Interpolation of points along the spline using scipy.interpolate.splrep使用 scipy.interpolate.splrep 沿样条线插值点
【发布时间】:2021-03-09 14:50:55
【问题描述】:

我正在处理沿着图像中的车道插入点的任务。包含带注释点的示例图像(图像不是来自实际数据集,点之间的间距也不是实际的)包含在here

我正在尝试使用来自 scipy 的slprep,以下是我正在采取的步骤。

 import numpy as np
 from scipy.interpolate import splrep, splev 

 #### example coordinates of annotated pts
 x = ([138.614, 161.404, 184.507, 207.297, 230.4, 407.726]) 
 y = ([231.230, 209.741, 188.058, 166.531, 144.739, 249.985])

 #### interpolation function
 interpl_fun = splrep(x, y, k=3)  ### k=4
 query_y = np.linspace(y.min(), y.max(), 20)  #### y for which x coordinates should be interpolated
 #### obtaining interpolated x coordinates
 interpolated_x = splev(query_y, interp_fun)

我目前的观察是:

  1. 当我绘制 (interpolated_x 和 query_y) 的像素标记时,结果坐标确实 不在输入(x 和 y)坐标的标记之间。

我的问题是:

  1. 谁能告诉我我做错了什么?

  2. 如何定义来自 splrep 的节点参数,以便 插值样条通过最大输入点??

  3. 我看到的大多数相关线程都使用插值函数 计算 y = f(x) 其中 x 是已知的并且 y 将被插值并且 我正在使用插值函数来评估 x = f(y)。这是 有什么问题吗??

【问题讨论】:

    标签: python scipy interpolation


    【解决方案1】:

    希望我能理解您的问题。
    1 & 3:您正在使用 (y',x') 点对 (x,y) 数据进行插值。正如您在第三个子问题中建议的那样,这将不起作用。 2:如果是你要的,请看示例代码:

    # imports
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.interpolate import splrep, splev 
    
    #### example coordinates of annotated pts
    x = np.array([138.614, 161.404, 184.507, 207.297, 230.4, 407.726]) 
    y = np.array([231.230, 209.741, 188.058, 166.531, 144.739, 249.985])
    
    # we need to sort x and y together to obtain monotonically increasing y
    # linked sorting
    yinds = y.argsort()
    y = y[yinds]
    x = x[yinds]
    # now we have y(x)
    
    #### interpolation function
    # note that it is (y,x), not (x,y)
    interpl_fun = splrep(y, x, k=4)  ### k=4
    query_y = np.linspace(y.min(), y.max(), 20)  #### y for which x coordinates should be interpolated
    #### obtaining interpolated x coordinates
    # again, it is (y',x'), the same as in interpl_fun
    interpolated_x = splev(query_y, interpl_fun)
    # uncomment the next two lines if you want to see intermediate result
    # plt.figure(figsize=(10,10))
    # plt.plot(y,x,'rx',query_y,interpolated_x,'b-')
    
    # now we need to get to (x,y) back
    # one can either sort again or just make a copy earlier
    xinds = x.argsort()
    y = y[xinds]
    x = x[xinds]
    plt.figure(figsize=(10,10))
    plt.plot(x,y,'rx',interpolated_x,query_y,'b-')
    

    最后的情节是这样的

    其中红色十字明显是初始点,蓝色曲线是插值数据。

    链接排序取自Sort array's rows by another array in Python

    【讨论】:

    • 您好,感谢您的回答。对于用例,我提出了您的答案。但在我的实际用例中,沿样条线的标记像素之间的距离太高了。因此,样条插值会受到影响,然后插值点不会位于已经标记的点之间。对此有何建议??
    • 我将尝试使用来自实际数据集中的一个样本来更新这里的问题,该样本具有沿样条线标记的点的实际值。
    • 不客气。请更新您的答案,我会看看。
    • 嗨,提供的答案完全适合我。减少样条的度数使所有插值样条位于输入点之间。
    • 这些链接有助于了解有关样条曲线和插值的更多信息。 WikiVadym Pasko's Blog
    猜你喜欢
    • 2014-11-07
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多