【问题标题】:Getting previous frame from video Opencv Haar Cascade从视频 Opencv Haar Cascade 获取前一帧
【发布时间】:2017-04-27 16:46:52
【问题描述】:

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

 Mat frame;
Mat prevFrame;

while (capture.isOpened()) {
    capture.read(frame);
    vector<Rect> cars; // center of rectangles where each rectangle contains the detected object
    vector<Rect> prevCars; // to store previous tracked rectangles

    // Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
    car_cascade.detectMultiScale(frame, cars, 1.1, 2);

    if(!prevFrame.empty()) {
        car_cascade.detectMultiScale(prevFrame, prevCars, 1.1, 2);
    } else {
        cout << "EMPTY" << endl; // for testing
    }

    cout << "current : " << cars.size() << endl; // print out number of cars
    cout << "previous: " << prevCars.size() << endl; // print out number of cars


    // more code goes here which I haven't written here

    frame.copyTo(prevFrame); // set previous frame to current frame
    imshow("Video", frame);

    char key = waitKey(33);
    if (key == 'q')
    {
        break;
    }
}

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

空 当前:3 上一个:0

【问题讨论】:

  • 为什么汽车和 prevCars 都需要 vector????,一个就可以了.....
  • 我有两个,因为我将使用 for 循环遍历保存在汽车中的所有汽车,并将其与 prevCars 中的汽车进行比较,以检查两个矩形是否有边界

标签: c++ opencv cascade


【解决方案1】:

为了跟踪和更新汽车的矩形,这是我要做的(类似 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)

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

  • 一个矩形来跟踪汽车的位置
  • 上次更新的时间戳(删除“过时”汽车时需要该时间戳)
  • 一个速度向量来近似下一帧上汽车的位置(可选)

我在手部跟踪系统中使用了类似的方法,效果很好。

【讨论】:

  • 在看到这个之前,我实际上已经让它(有点)工作了。无论如何感谢您的简洁回答:)
猜你喜欢
  • 2014-02-16
  • 1970-01-01
  • 2012-03-04
  • 2016-02-13
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
相关资源
最近更新 更多