首页 > 解决方案 > 如何绘制被跟踪人员的轨迹

问题描述

我正在尝试使用卡尔曼滤波器在 opencv 中绘制被跟踪人员的轨迹。

我正在尝试以下代码,但两个结尾行是无限的

def pipeline(img, tracker, det):
    global cnt
    z_box = det.get_localization(img)
    detections = tracker.update(np.array(z_box))
    prev= []
    cnt = cnt + 1

    for trk in detections:
            trk = trk.astype(np.int32)
            img = helpers.draw_box_label(img, trk, trk[4])  # Draw the bounding boxes on the
            if cnt < 20:
                prev.append(trk)
            else:
                cnt = 0
                prev = []

    for i in range(0,len(prev)-1):
        left1, top1, right1, bottom1 = prev[i][1], prev[i][0], prev[i][3], prev[i][2]
        left2, top2, right2, bottom2 = prev[i+1][1], prev[i+1][0], prev[i+1][3], prev[i+1][2]
        cv2.line(img, pt1=((int(left1 + right1 / 2)), int(top1 + bottom1 / 2)),pt2=(int((left2 + right2) / 2), int((top2 + bottom2) / 2)), color=(255,0,255))
    return img

我想绘制轨迹如下图

在此处输入图像描述

根据评论,我尝试了这个,但仍然没有工作。

 for trk in detections:
            trk = trk.astype(np.int32)
            img = helpers.draw_box_label(img, trk, trk[4])  # Draw the bounding boxes on the
            if cnt < 20:
                left2, top2, right2, bottom2 = trk[1], trk[0], trk[3], trk[2]

                prev.append( np.array([int(left2 + right2)/2, int(right2+bottom2)/2]))
            else:
                cnt = 0
                prev = []

    cv2.polylines(img,np.int32([prev]), True, color=(255,0,0))

标签: pythonopencv

解决方案


推荐阅读