使用 scipy.interpolate.splrep 沿样条插值点
Interpolation of points along the spline using scipy.interpolate.splrep
我正在处理沿图像中的车道插入点的任务。包含带注释点的示例图像(图像不是来自实际数据集,点之间的间距也不是实际的)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)
我目前的观察是:
- 当我绘制 (interpolated_x 和 query_y) 的像素标记时,结果坐标确实
不位于输入(x 和 y)坐标标记之间。
我的问题是:
谁能告诉我我做错了什么?
如何从 splrep 定义节点参数,以便
插值样条通过最大输入点数??
我看到的大部分相关线程都使用了插值函数
计算 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-')
最后的剧情好像
其中红色叉号显然是初始点,蓝色曲线是插值数据。
链接排序取自Sort array's rows by another array in Python。
我正在处理沿图像中的车道插入点的任务。包含带注释点的示例图像(图像不是来自实际数据集,点之间的间距也不是实际的)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)
我目前的观察是:
- 当我绘制 (interpolated_x 和 query_y) 的像素标记时,结果坐标确实 不位于输入(x 和 y)坐标标记之间。
我的问题是:
谁能告诉我我做错了什么?
如何从 splrep 定义节点参数,以便 插值样条通过最大输入点数??
我看到的大部分相关线程都使用了插值函数 计算 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-')
最后的剧情好像
其中红色叉号显然是初始点,蓝色曲线是插值数据。
链接排序取自Sort array's rows by another array in Python。