2017-09-25 183 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