如何在Python中的其他两行之间插入一行 [英] How to interpolate a line between two other lines in python

查看:782
本文介绍了如何在Python中的其他两行之间插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:之前我问过这个问题,但是它是作为一个副本关闭的,但是,我和其他几个人认为它是不合理的关闭,我解释了为什么在我的原始



<此外,我想知道是否有人在一些优化的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]])

阵列([[1152.27115094,1281.52899302],
[1155.53345506,1295.30515742]
[1163 0.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 库。但是,我在这样的结尾分支:



解决方案

两行之间的界限没有很好的定义。你可以通过在两条曲线之间进行三角测量来获得一个体面的,但简单的解决方案(你可以通过从顶点到顶点的进展来进行三角测量,选择产生较少三角形的对角线)

然后插入的曲线连接到边的中点。


Note: I asked this question before but it was closed as a duplicate, however, I, along with several others believe it was unduely closed, I explain why in an edit in my original post. So I would like to re-ask this question here again.

Does anyone know of a python library that can interpolate between two lines. For example, given the two solid lines below, I would like to produce the dashed line in the middle. In other words, I'd like to get the centreline. The input is a just two numpy arrays of coordinates with size N x 2 and M x 2 respectively.

Furthermore, I'd like to know if someone has written a function for this in some optimized python library. Although optimization isn't exactly a necessary.

Here is an example of two lines that I might have, you can assume they do not overlap with each other and an x/y can have multiple y/x coordinates.

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]])

Visualizing this we have:

So my attempt at this has been using the skeletonize function in the skimage.morphology library by first rasterizing the coordinates into a filled in polygon. However, I get branching at the ends like this:

解决方案

The "line between two lines" is not so well defined. You can obtain a decent though simple solution by triangulating between the two curves (you can triangulate by progressing from vertex to vertex, choosing the diagonal that produce the less skewed triangle).

Then the interpolated curve joins the middles of the sides.

这篇关于如何在Python中的其他两行之间插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆