如何在 python 中的其他两条线之间插入一条线

How to interpolate a line between two other lines in python

注意:我之前问过这个问题,但它作为重复问题被关闭,但是,我和其他几个人认为它被不当关闭,我在我原来的编辑中解释了为什么 post。所以我想在这里再问一遍这个问题。

有谁知道可以在两行之间插入的 python 库。例如,给定下面的两条实线,我想在中间生成虚线。换句话说,我想得到中心线。输入只是两个 numpy 坐标数组,大小分别为 N x 2M x 2

此外,我想知道是否有人在某些优化的 python 库中为此编写了函数。虽然优化并不是必需的。

这是我可能拥有的两条线的示例,您可以假设它们彼此不重叠,并且 x/y 可以有多个 y/x 坐标。

array([[ 1233.87375018,  1230.07095987],
       [ 1237.63559365,  1253.90749041],
       [ 1240.87500801,  1264.43925132],
       [ 1245.30875975,  1274.63795396],
       [ 1256.1449357 ,  1294.48254424],
       [ 1264.33600095,  1304.47893299],
       [ 1273.38192911,  1313.71468591],
       [ 1283.12411536,  1322.35942538],
       [ 1293.2559388 ,  1330.55873344],
       [ 1309.4817002 ,  1342.53074698],
       [ 1325.7074616 ,  1354.50276051],
       [ 1341.93322301,  1366.47477405],
       [ 1358.15898441,  1378.44678759],
       [ 1394.38474581,  1390.41880113]])

array([[ 1152.27115094,  1281.52899302],
       [ 1155.53345506,  1295.30515742],
       [ 1163.56506781,  1318.41642169],
       [ 1168.03497425,  1330.03181319],
       [ 1173.26135672,  1341.30559949],
       [ 1184.07110925,  1356.54121651],
       [ 1194.88086178,  1371.77683353],
       [ 1202.58908737,  1381.41765447],
       [ 1210.72465255,  1390.65097106],
       [ 1227.81309742,  1403.2904646 ],
       [ 1244.90154229,  1415.92995815],
       [ 1261.98998716,  1428.56945169],
       [ 1275.89219696,  1438.21626352],
       [ 1289.79440676,  1447.86307535],
       [ 1303.69661656,  1457.50988719],
       [ 1323.80994319,  1470.41028655],
       [ 1343.92326983,  1488.31068591],
       [ 1354.31738934,  1499.33260989],
       [ 1374.48879779,  1516.93734053],
       [ 1394.66020624,  1534.54207116]])

可视化我们有:

所以我尝试使用 skimage.morphology 库中的 skeletonize 函数,首先将坐标栅格化为填充的多边形。但是,我在这样的末端得到分支:

"line between two lines" 的定义不是很明确。您可以通过在两条曲线之间进行三角剖分来获得一个不错但简单的解决方案(您可以通过从一个顶点到另一个顶点进行三角剖分,选择产生较少倾斜三角形的对角线)。

然后插值曲线连接边的中间。

首先,请原谅我的矫枉过正;我对你的问题很感兴趣。如果描述太长,请随意跳到底部,我定义了一个函数来完成我描述的所有事情。

如果您的数组长度相同,您的问题会相对简单。在这种情况下,您所要做的就是找到每个数组中对应的 x 值与每个数组中对应的 y 值之间的平均值。

所以我们可以做的是创建 相同长度的数组,这或多或少是对原始数组的良好估计。我们可以通过将多项式拟合到您拥有的数组来做到这一点。如评论和其他答案中所述,您的原始数组的中线没有具体定义,因此一个好的估计应该可以满足您的需求。

注意:在所有这些示例中,我已经将您发布的两个数组命名为 a1a2

第一步:创建估计旧行的新数组

查看您发布的数据:

这些并不是特别复杂的函数,看起来 3 次多项式很适合它们。我们可以使用 numpy:

创建那些
import numpy as np

# Find the range of x values in a1
min_a1_x, max_a1_x = min(a1[:,0]), max(a1[:,0])
# Create an evenly spaced array that ranges from the minimum to the maximum
# I used 100 elements, but you can use more or fewer. 
# This will be used as your new x coordinates
new_a1_x = np.linspace(min_a1_x, max_a1_x, 100)
# Fit a 3rd degree polynomial to your data
a1_coefs = np.polyfit(a1[:,0],a1[:,1], 3)
# Get your new y coordinates from the coefficients of the above polynomial
new_a1_y = np.polyval(a1_coefs, new_a1_x)

# Repeat for array 2:
min_a2_x, max_a2_x = min(a2[:,0]), max(a2[:,0])
new_a2_x = np.linspace(min_a2_x, max_a2_x, 100)
a2_coefs = np.polyfit(a2[:,0],a2[:,1], 3)
new_a2_y = np.polyval(a2_coefs, new_a2_x)

结果:

这还不错,太糟糕了!如果您有更复杂的函数,则必须拟合更高阶的多项式,或找到其他一些适合您的数据的函数。

现在,你得到了两组相同长度的数组(我选择了 100 的长度,你可以根据你希望中点线的平滑程度来增加或减少)。这些集合表示原始数组的 估计值 的 x 和 y 坐标。在上面的示例中,我将它们命名为 new_a1_xnew_a1_ynew_a2_xnew_a2_y.

第二步:计算新数组中每个 x 和每个 y 之间的平均值

然后,我们要找到每个估计数组的平均 x 和平均 y 值。只需使用 np.mean:

midx = [np.mean([new_a1_x[i], new_a2_x[i]]) for i in range(100)]
midy = [np.mean([new_a1_y[i], new_a2_y[i]]) for i in range(100)]

midxmidy 现在代表我们的 2 个估计数组之间的中点。现在,只需绘制您的原始(不是估计)数组,以及您的中点数组:

plt.plot(a1[:,0], a1[:,1],c='black')
plt.plot(a2[:,0], a2[:,1],c='black')
plt.plot(midx, midy, '--', c='black')
plt.show()

瞧瞧:

此方法仍然适用于更复杂、嘈杂的数据(但您必须仔细考虑函数):

作为函数:

我把上面的代码放在了一个函数里,方便大家使用。它 returns 一个你估计的中点数组,采用你的原始数组的格式。

参数:a1a2是你的2个输入数组,poly_deg是你要拟合的次数多项式,n_points是你要拟合的点数想要在你的中点数组中,并且 plot 是一个布尔值,无论你是否想要绘制它。

import matplotlib.pyplot as plt
import numpy as np

def interpolate(a1, a2, poly_deg=3, n_points=100, plot=True):

    min_a1_x, max_a1_x = min(a1[:,0]), max(a1[:,0])
    new_a1_x = np.linspace(min_a1_x, max_a1_x, n_points)
    a1_coefs = np.polyfit(a1[:,0],a1[:,1], poly_deg)
    new_a1_y = np.polyval(a1_coefs, new_a1_x)

    min_a2_x, max_a2_x = min(a2[:,0]), max(a2[:,0])
    new_a2_x = np.linspace(min_a2_x, max_a2_x, n_points)
    a2_coefs = np.polyfit(a2[:,0],a2[:,1], poly_deg)
    new_a2_y = np.polyval(a2_coefs, new_a2_x)

    midx = [np.mean([new_a1_x[i], new_a2_x[i]]) for i in range(n_points)]
    midy = [np.mean([new_a1_y[i], new_a2_y[i]]) for i in range(n_points)]

    if plot:
        plt.plot(a1[:,0], a1[:,1],c='black')
        plt.plot(a2[:,0], a2[:,1],c='black')
        plt.plot(midx, midy, '--', c='black')
        plt.show()

    return np.array([[x, y] for x, y in zip(midx, midy)])

[编辑]:

我在回想这个问题,但我忽略了一种更简单的方法,即 "densifying" 使用 np.interp 将两个数组的点数相同。此方法遵循与上述 line-fitting 方法相同的基本思想,但不是使用 polyfit / polyval 来近似线,它只是密集化:

min_a1_x, max_a1_x = min(a1[:,0]), max(a1[:,0])
min_a2_x, max_a2_x = min(a2[:,0]), max(a2[:,0])

new_a1_x = np.linspace(min_a1_x, max_a1_x, 100)
new_a2_x = np.linspace(min_a2_x, max_a2_x, 100)

new_a1_y = np.interp(new_a1_x, a1[:,0], a1[:,1])
new_a2_y = np.interp(new_a2_x, a2[:,0], a2[:,1])

midx = [np.mean([new_a1_x[i], new_a2_x[i]]) for i in range(100)]
midy = [np.mean([new_a1_y[i], new_a2_y[i]]) for i in range(100)]

plt.plot(a1[:,0], a1[:,1],c='black')
plt.plot(a2[:,0], a2[:,1],c='black')
plt.plot(midx, midy, '--', c='black')
plt.show()

我和河流一起工作,所以这是一个常见的问题。我的解决方案之一与您在问题中展示的完全一样——即骨架化 blob。你看到边界有问题,所以我所做的似乎很有效的是简单地镜像边界。要使这种方法起作用,blob 不能与图像的角相交。

您可以在名为“mask_to_centerline”的 RivGraph; this particular algorithm is in rivers/river_utils.py 中找到我的实现。

这是一个示例输出,显示了中心线的末端如何延伸到对象的所需边缘:

sacuL 的解决方案几乎对我有用,但我需要聚合的不仅仅是两条曲线。 这是我对 sacuL 解决方案的概括:

def interp(*axis_list):
    min_max_xs = [(min(axis[:,0]), max(axis[:,0])) for axis in axis_list]

    new_axis_xs = [np.linspace(min_x, max_x, 100) for min_x, max_x in min_max_xs]
    new_axis_ys = [np.interp(new_x_axis, axis[:,0], axis[:,1]) for axis, new_x_axis in zip(axis_list, new_axis_xs)]

    midx = [np.mean([new_axis_xs[axis_idx][i] for axis_idx in range(len(axis_list))]) for i in range(100)]
    midy = [np.mean([new_axis_ys[axis_idx][i] for axis_idx in range(len(axis_list))]) for i in range(100)]

    for axis in axis_list:
        plt.plot(axis[:,0], axis[:,1],c='black')
    plt.plot(midx, midy, '--', c='black')
    plt.show()

如果我们现在运行举个例子:

a1 = np.array([[x, x**2+5*(x%4)] for x in range(10)])
a2 = np.array([[x-0.5, x**2+6*(x%3)] for x in range(10)])
a3 = np.array([[x+0.2, x**2+7*(x%2)] for x in range(10)])
interp(a1, a2, a3)

我们得到了情节: