如何绘制被跟踪者的轨迹

时间:2019-03-06 17:21:15

标签: python opencv

我正在尝试使用卡尔曼滤波器在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

我想画出如下图的轨迹

enter image description here

根据评论,我尝试了此操作,但还是没用。

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

0 个答案:

没有答案