c++ - 加速人员检测程序,使用 OpenCV 3.0 编写

标签 c++ performance opencv opencv3.0 object-detection

我使用 OpenCV 3.0.0 和 Visual Studio 2012。我发现这个程序可以检测人员并对他们进行计数并确定他们的运动方向。但它很慢。您知道如何加快速度吗?

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
    VideoCapture cap("C:/Users/John/Downloads/Video/Cam.avi");              
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 100);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 110);    
    if (!cap.isOpened())
    return -1;

    Mat img;
    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    namedWindow("video capture", CV_WINDOW_AUTOSIZE);
    while (true)
    {
        cap >> img;
        if (!img.data)
        continue;

        vector<Rect> found, found_filtered;
        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);

        size_t i, j;
        for (i=0; i<found.size(); i++)
        {
            Rect r = found[i];
            for (j=0; j<found.size(); j++)
            if (j!=i && (r & found[j])==r)
            break;
            if (j==found.size())
            found_filtered.push_back(r);
        }
        for (i=0; i<found_filtered.size(); i++)
        {
            Rect r = found_filtered[i];
            r.x += cvRound(r.width*0.1);
            r.width = cvRound(r.width*0.8);
            r.y += cvRound(r.height*0.06);
            r.height = cvRound(r.height*0.9);
            rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);
        }
        imshow("video capture", img);
        if (waitKey(33) >= 0)
        break;
    }
    return 0;
}

最佳答案

如果有 cuda gpu,请尝试使用相同代码的 gpu 版本

据此

http://opencv.org/platforms/cuda.html

http://opencv.org/wp-content/uploads/2012/06/perf.png

行人检测速度提高 8 倍

https://github.com/Itseez/opencv/blob/master/samples/gpu/hog.cpp

关于c++ - 加速人员检测程序,使用 OpenCV 3.0 编写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264008/

相关文章:

c++ - Opencv中Gaussians的GIMP差异

c++ - 具有多种类型的递归模板函数

c++ - 子类族的重写方法

c++ - 从类型 ‘int&' 的临时类型 'MyClass<int>::iterator*' 的非常量引用的无效初始化

c++ - 是否有任何 C++ 编译器删除内联时始终持有相同答案的 if 语句?

performance - GitLab CI 加速

c++ - 如何在 C++ 中实现猴子补丁?

sql - 查找所有带有希伯来名字的记录

iphone - 如何在 iphone 中使用 openCV 检测眨眼

python - IplImage 里面的 IplImage