OpenCV计数车辆

OpenCV counting vehicles

本文关键字:OpenCV      更新时间:2023-10-16

我已经完成了在OpenCV C++中检测车辆的工作,但我需要计算它们,而不是计算"frame"中的车辆。我需要数一下所有通过的车。这是我的检测代码,我该怎么办?

arac_cascade.detectMultiScale(
    frame_gray, arac, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(20, 20));
for (int i = 0; i < arac.size(); i++)
{
    Point pt1(arac[i].x + arac[i].width, arac[i].y + arac[i].height);
    Point pt2(arac[i].x, arac[i].y);
    rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
    Mat aracROI = frame_gray(arac[i]);
}

检测后,arac.size()是当前帧中检测到的车辆总数。如果你需要计算所有帧的数量,你可以简单地将它们相加,比如:

int num_total = 0;
for (-every-frame-){
    // detection code...
    num_total += arac.size();
}