我在用tensorflow和python检测人和车辆我计算轨迹并用卡尔曼滤波进行预测,然后拟合一条预测轨迹的直线。
我的问题是如何找到两条轨迹的交集和碰撞时间?
我试过线与线相交,但拟合线并不总是两点线,而是多段线。
以下是我的尝试:
detections = tracker.update(np.array(z_box))
for trk in detections[0]:
trk = trk.astype(np.int32)
helpers.draw_box_label(img, trk, trk[4]) # Draw the bounding boxes on the
centerCoord = (((trk[1] +trk[3]) / 2), (trk[0] + trk[2]) / 2)
point_lists[trk[4]].append(centerCoord)
x = [i[0] for i in point_lists[trk[4]]]
y = [i[1] for i in point_lists[trk[4]]]
p = np.polyfit(x, y, deg=1)
y = p[1] + p[0] * np.array(x)
fitted = list(zip(x, y))
cv2.polylines(img, np.int32([fitted]), False, color=(255, 0, 0))
for other in detections[0]:
other = other.astype(np.int32)
if other[4] != trk[4]: # check for self ID
x2 = [i[0] for i in point_lists[other[4]]]
y2 = [i[1] for i in point_lists[other[4]]]
p2 = np.polyfit(x2, y2, deg=1)
y2 = p2[1] + p2[0] * np.array(x2)
other_fitted = list(zip(x2, y2))
if(line_intersection(fitted, other_fitted)):
print("intersection")
else:
print("not intersection")
最佳答案
这是一个更广泛的话题,所以我将只关注数学/物理部分,因为我感觉到CV/DIP部分已经被你们两个提问者(andre ahmed和chris burgees)处理了。
为了简单起见,我假设匀速直线运动,因此如何做到这一点:
在已知时间dt
因此,为每个有问题的对象获取图像上的二维中心(或角点或其他)位置。
将它们转换为三维
因此,使用已知的摄像机参数或已知的场景背景信息,可以将屏幕上的二维位置取消投影到相对于摄像机的三维位置。如果像处理二维情况一样处理,这将消除非线性插值。
有更多的选择,如何获得三维位置取决于你在你的处置。例如:
Transformation of 3D objects related to vanishing points and horizon line
获得物体的实际速度
速度矢量很简单:
vel = ( pos(t+dt) - pos(t) )/dt
因此,只需从两个后续帧中提取同一对象的位置,然后除以帧率周期(或使用的帧之间的间隔)。
测试每两个物体的碰撞
有趣的是,你可以解决一个不平等的系统,比如:
| ( pos0 + vel0 * t ) - (pos1 + vel1 * t ) | <= threshold
但我在这里用了一种更简单的方法
Collision detection between 2 “linearly” moving objects in WGS84
其思想是计算被测物体最接近的位置(如果彼此接近)。
所以我们可以这样推断每个物体的未来位置:
pos(t) = pos(t0) + vel*(t-t0)
其中
t
是实际时间,t
是一些开始时间(例如t0
)。假设我们有两个要测试的对象(
t0=0
),那么计算它们距离的前两次迭代,这样:pos0(0) = pos0;
pos1(0) = pos1;
dis0 = | pos1(0) - pos0(0) |
pos0(dt) = pos0 + vel0*dt;
pos1(dt) = pos1 + vel1*dt;
dis1 = | pos1(dt) - pos0(dt) |
其中
pos0,vel0,pos1,vel1
是足够小的时间(以避免跳过碰撞)现在dt
然后这些物体被割掉,因此没有碰撞,if (dis0<dis1)
这些物体不会移动或平行移动,只有if (dis0==dis1)
这些物体彼此靠近,所以我们可以估计:dis(t) = dis0 + (dis1-dis0)*t
碰撞预计
if (dis0>dis1)
,因此我们可以再次推断:0 = dis0 + (dis1-dis0)*t
(dis0-dis1)*t = dis0
t = dis0 / (dis0-dis1)
其中
dis(t)=0
是估计的碰撞时间。粗略地说,所有这一切处理所有的移动作为线性和外推很多,所以它不准确,但正如你可以这样做,为更多的后续帧,结果将更准确的时间接近碰撞…此外,为了确保您应该外推估计碰撞时每个对象的位置,以验证结果(如果没有碰撞,则外推只是数值计算,而没有碰撞的对象只是在一段时间内接近每个对象)如前所述,转换为3D(bullet#2)是不必要的,但它消除了非线性,因此简单的线性插值/外推可用于以后大大简化的事情。
关于python - python中的轨迹交点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55089783/