OpenCV慢速相机帧速率

时间:2014-07-23 14:14:50

标签: c++ opencv visual-studio-2012

我刚刚复制了一个通过网络摄像头检测人脸的程序,但视频捕捉速度很慢,而且我不知道如何修复它!

以下是代码:

#include<stdio.h>
#include<math.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<vector>

using namespace cv;
using namespace std;

int main()
{
CascadeClassifier face_cascade;
if(!face_cascade.load("c:\\haar\\haarcascade_frontalface_alt2.xml")) {
    printf("Error loading cascade file for the face");
    return 1;
}
VideoCapture capture(0);
if(!capture.isOpened())
{
    printf("Error trying to start the Camera");
    return 1;
}
Mat cap_img,gray_img;
vector<Rect> faces;
while(1)
{
    capture >> cap_img;
    waitKey(10);
    cvtColor(cap_img, gray_img, CV_BGR2GRAY);
    cv::equalizeHist(gray_img,gray_img);
    face_cascade.detectMultiScale(gray_img, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(300,300));
    for(int i=0; i < faces.size();i++)
    {
        Point pt1(faces[i].x+faces[i].width, faces[i].y+faces[i].height);
        Point pt2(faces[i].x,faces[i].y);
        rectangle(cap_img, pt1, pt2, cvScalar(191,191,191), 2, 8, 0);
    }
    imshow("Result", cap_img);
    waitKey(3);
    char c = waitKey(3);
    if(c == 27)
        break;
}
return 0;
}

我使用的是Visual Studio 2012,这是main.cpp文件。我正在使用OpenCV 2.4.9!

3 个答案:

答案 0 :(得分:0)

OpenCV附带预建库。在实际要部署的应用程序中使用它们时,请确保使用发行库。在调试模式下,您可以使用asserts形式的大量附加检查以及调试符号,这些符号允许您使用在发布模式下删除的调试器进入库。

关于您发布的代码的另一个更具体的建议:避免调用cv::waitKey(),因为每次调用,谁会猜到,使主线程等待指定的时间量(以毫秒为单位)。不要完全放弃对它的呼叫,例如cv::imshow()只会在适当的情况下正常运作。


修改

将你的while循环减少到:

while ( true ) {
    capture >> cap_img;
    imshow("Result", cap_img);

    if(waitKey(1) == 27)
        break;
}

当您知道捕获图像并显示图像所需的时间时,您可以比较正在运行的算法对您的性能产​​生的影响程度。

答案 1 :(得分:0)

这是解决方案...每次你阅读!!! ..每次你使用vcap&gt;&gt; cap或cap.read(vcap)...你必须设置FPS ..25是max..if我尝试设置为26或更高它运行缓慢。 ...这对我来说是30 fps 如果你没有设置FPS ......它也会运行缓慢

if (cap.read(vcap)){
        imshow("Cam Feed", vcap);
        cap.set(CV_CAP_PROP_FPS, 25);
    }

答案 2 :(得分:0)

在其他答案之上,多处理解决了我的问题。我将我的程序分成 3 个进程。一种用于抓取图像,一种用于处理图像,一种用于显示图像。参考 this page 对我帮助很大。我也遵循了这里其他答案的提示。