2017-09-25 103 views
2

我有两个轨迹(即两个点列表),我试图找到这两个轨迹的交点。但是,如果我将这些轨迹表示为线条,我可能会错过真实世界的交点(只是错过)。查找交点基于区域的轨迹与线轨迹

我想要做的就是将线条表示为具有一定宽度的多边形,然后找到两个多边形相互交叉的位置。

我使用python空间库,但我想知道是否有人以前做过这个。这是一段不相交的线段,因为他们只是错过了彼此。以下是代表两个对象轨迹的示例数据代码。

enter image description here

enter image description here

object_trajectory=np.array([[-3370.00427248, 3701.46800775], 
    [-3363.69164715, 3702.21408203], 
    [-3356.31277271, 3703.06477984], 
    [-3347.25951787, 3704.10740164], 
    [-3336.739511 , 3705.3958357 ], 
    [-3326.29355823, 3706.78035903], 
    [-3313.4987339 , 3708.2076586 ], 
    [-3299.53433345, 3709.72507366], 
    [-3283.15486406, 3711.47077376], 
    [-3269.23487255, 3713.05635557]]) 
target_trajectory=np.array([[-3384.99966703, 3696.41922372], 
    [-3382.43687562, 3696.6739521 ], 
    [-3378.22995178, 3697.08802862], 
    [-3371.98983789, 3697.71490469], 
    [-3363.5900481 , 3698.62666805], 
    [-3354.28520354, 3699.67613798], 
    [-3342.18581931, 3701.04853915], 
    [-3328.51519511, 3702.57528111], 
    [-3312.09691577, 3704.41961271], 
    [-3297.85543763, 3706.00878621]]) 
plt.plot(object_trajectory[:,0],object_trajectory[:,1],'b',color='b') 
plt.plot(vehicle_trajectory[:,0],vehicle_trajectory[:,1],'b',color='r') 
+0

你是什么意思“代表一条线作为一个多边形”?多边形由线组成。你的意思是你想要找到点之间的点或点之间的距离是否在另一个轨迹的某个阈值内? –

+0

谢谢澄清@JeremyMcGibbon。基本上我想在每条线周围放一个信封。信封将被表示为多边形。然后我可以确定两个信封相互交叉的位置,然后对这些交点进行更多分析。考虑剧情图片。在这种情况下,两条线都不相交,但在实际情况中,它确实是因为它不是一条线并且具有宽度。这有帮助吗? – SriK

+0

这些要点有多好?仅仅在点的粘性上检查重叠是否足够,还是您还需要查看这些点之间的轨迹?你的轨迹有多少点,数量级? –

回答

0

原来,整齐的包装已经有很多方便的功能,让我对此非常感兴趣。

from shapely.geometry import Point, LineString, MultiPoint 
# I assume that self.line is of type LineString (i.e. a line trajectory) 
region_polygon = self.line.buffer(self.lane_width) 
# line.buffer essentially generates a nice interpolated bounding polygon around the trajectory. 
# Now we can identify all the other points in the other trajectory that intersects with the region_polygon that we just generated. You can also use .intersection if you want to simply generate two polygon trajectories and find the intersecting polygon as well. 
is_in_region = [region_polygon.intersects(point) for point in points] 
1

比方说,你必须numpy的阵列x1y1x2y2定义的两条线。

import numpy as np 

可以创建包含i个点之间在所述第一线和所述第二行中的第j点的距离的阵列distances[i, j]

distances = ((x1[:, None] - x2[None, :])**2 + (y1[:, None] - y2[None, :])**2)**0.5 

后,可以看到指数在那里distances小于你要定义路口某个阈值。如果你认为这些线条具有一定的厚度,那么阈值将是该厚度的一半。现在

threshold = 0.1 
intersections = np.argwhere(distances < threshold) 

intersections是由含有被认为是“交叉”点的所有对2阵列的N(在[i, 0]是从第一行中的索引,并且[i, 1]是从第二行中的索引)。如果你想获得一组从相交的每一行的所有索引,你可以使用像

first_intersection_indices = np.asarray(sorted(set(intersections[:, 0]))) 
second_intersection_indices = np.asarray(sorted(set(intersections[:, 1]))) 

从这里,你还可以确定有多少路口有采取只对任何的中心值每个列表中的连续值。

L1 = [] 
current_intersection = [] 
for i in range(first_intersection_indices.shape[0]): 
    if len(current_intersection) == 0: 
     current_intersection.append(first_intersection_indices[i]) 
    elif first_intersection_indices[i] == current_intersection[-1]: 
     current_intersection.append(first_intersection_indices[i]) 
    else: 
     L1.append(int(np.median(current_intersection))) 
     current_intersection = [first_intersection_indices[i]] 
print(len(L1)) 

您可以使用它们来打印每个交点的坐标。

for i in L1: 
    print(x1[i], y1[i]) 
+0

谢谢!寻找接近点的另一种方法是近似于与两个多边形的交点。一个问题 - 如果点的采样有点“不吉利”(因为不考虑点之间的插值),这种方法在实际上可能会出现交叉点时可能找不到交点。这是否准确? – SriK