从视频中获取前一帧Opencv Haar Cascade

时间:2017-04-27 06:50:26

标签: c++ opencv cascade

我正在使用汽车级联来检测示例视频中的所有汽车。该程序目前正在检测到的每辆车周围绘制矩形。但是,矩形会逐帧改变大小。如果下一帧中的新矩形与前一个矩形重叠,我想通过保留原始矩形来添加一些稳定性。为了实现这一点,我保存了前一帧(并检测前一帧中的汽车)并将前一帧的矩形与当前帧进行比较。

dumpsys

但是,从前一帧检测到的汽车数量与之前的电流不同。如,

EMPTY 目前:3 previous:0< - 0因为它是空的 目前:3 previous:2< - previous是2,但应该是3,因为前一个当前是3 目前:3 上一篇:2

1 个答案:

答案 0 :(得分:1)

为了追踪和更新汽车' Rect,这就是我要做的(python就像代码):

def getIndexOfCorrespondingTrackedCar(car) :
    for i in range(0, len(tracked_cars)) :
        if distance(car.center, tracked_cars[i].center) < THRESHOLD : // you will have to define a threshold according to your case. It has to be smaller than the speed of cars (in px/frame) though.
            return(i) // we found the corresponding car in the tracked_cars list
    return(-1) // we found no corresponding car, it must be a new car

tracked_cars = [] // list of tracked Rects
cars_current_frame = [] // list of Rects on the current frame

while(camera.open()) :

    cars_current_frame = getCarsInFrame(frame) // here you want to use your detectMultiScale function

    for ccf in cars_current_frame :
        car_idx = getIndexOfCorrespondingTrackedCar(ccf) // get the index of the corresponding car in tracked_cars list
        if car_idx is -1 : // No correspondance has been found, this is a new car
            tracked_cars.append(ccf)
        else :
            tracked_cars[car_idx] = ccf // we update the position of the tracked car with its new position

    deleteOutdatedCars(tracked_cars) // delete all the tracked cars that haven't been updated for a certain time (it most probably means that the car went off of the frame)

在我的例子中,我只使用Rects列表,但在这里使用对象会更方便。我建议您使用以下成员变量创建Car类:

  • Rect 以跟踪汽车的位置
  • 上次更新的时间戳(这将需要删除&#34;过时的&#34;汽车)
  • 一个速度矢量来估算下一帧汽车的位置(可选)

我使用了类似的手动跟踪系统,效果很好。