gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:49 25 4
gpt4 key购买 nike

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

 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;
}
}

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

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

最佳答案

为了跟踪和更新汽车的 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)

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

  • Rect 以跟踪汽车的位置
  • 上次更新的时间戳(删除“过时”汽车时需要)
  • 一个速度 vector 来近似汽车在下一帧的位置(可选)

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

关于c++ - 从视频 Opencv Haar Cascade 中获取前一帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43650537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com