Python 使用scipy.interpolate.splrep沿样条曲线插值点

Python 使用scipy.interpolate.splrep沿样条曲线插值点,python,scipy,interpolation,Python,Scipy,Interpolation,我正在做的是在图像中沿车道插值点的工作。包含带注释点的示例图像(图像不是来自实际数据集,点之间的间距也不是实际的) 我正在尝试使用scipy,下面是我正在采取的步骤 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

我正在做的是在图像中沿车道插值点的工作。包含带注释点的示例图像(图像不是来自实际数据集,点之间的间距也不是实际的)

我正在尝试使用scipy,下面是我正在采取的步骤

 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)
我目前的意见是:

  • 当我绘制(插值的_x和查询的_y)的像素标记时,生成的坐标不起作用 不在输入(x和y)坐标标记之间
  • 我的问题是:

  • 有人能告诉我我做错了什么吗

  • 如何从中定义节点参数,以便 插值样条曲线通过最大输入点

  • 我看到的大多数相关线程都使用插值函数来 计算y=f(x),其中x是已知的,y是要插值的,并且 我用插值函数来计算x=f(y)。这是吗 有什么问题吗


  • 我希望我理解你的问题。
    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-')
    
    最后一个情节看起来像

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


    链接排序取自。

    我希望我理解您的问题。
    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-')
    
    最后一个情节看起来像

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


    链接排序取自。

    您好,谢谢您的回答。对于这个用例,我给出了您的答案。但是在我的实际用例中,沿着样条线标记的像素之间的距离太大了。因此,样条曲线插值会受到影响,然后插值点不位于已标记的点之间。对此有什么建议吗?我将尝试用实际数据集的一个样本来更新这里的问题,该样本带有沿样条线标记的点的实际值。欢迎您。请更新你的答案,我会看一看。嗨,提供的答案完全适合我。减少样条曲线的阶数使所有插值样条曲线位于输入点之间。这些链接有助于了解更多样条曲线和插值。嗨,谢谢你的回答。对于这个用例,我给出了您的答案。但是在我的实际用例中,沿着样条线标记的像素之间的距离太大了。因此,样条曲线插值会受到影响,然后插值点不位于已标记的点之间。对此有什么建议吗?我将尝试用实际数据集的一个样本来更新这里的问题,该样本带有沿样条线标记的点的实际值。欢迎您。请更新你的答案,我会看一看。嗨,提供的答案完全适合我。减少样条曲线的阶数使所有插值样条曲线位于输入点之间。这些链接有助于了解更多样条曲线和插值。和