简体   繁体   English

OpenCV慢速相机帧速率

[英]OpenCV Slow camera framerate

I just copied an program to detect faces through webcam but the video capture is really slow, and i dont know how to fix it! 我刚刚复制了一个程序,通过网络摄像头检测人脸,但视频捕获速度非常慢,我不知道如何解决它!

Here is the code: 这是代码:

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

I am using Visual studio 2012 and this is the main.cpp file. 我正在使用Visual Studio 2012,这是main.cpp文件。 Im using OpenCV 2.4.9! 我正在使用OpenCV 2.4.9!

OpenCV comes with prebuild libraries. OpenCV附带预构建库。 When you use them in an application you actually want to deploy make sure you use the release libraries. 在实际要部署的应用程序中使用它们时,请确保使用发行库。 In debug mode you have lots of additional checks in the form of asserts as well as debug symbols allowing you to step into the libraries with a debugger that are removed in release mode. 在调试模式下,您可以以asserts和调试符号的形式进行大量额外检查,从而允许您使用在发布模式下删除的调试器进入库。

Another more specific advise regarding the code you posted: Avoid calls to cv::waitKey() as every single call, who would have guessed, makes your main thread wait the specified amount of time in milliseconds. 关于您发布的代码的另一个更具体的建议:避免调用cv::waitKey()因为每次调用,谁会猜到,使主线程等待指定的时间量(以毫秒为单位)。 Do not drop calls to it entirely as eg cv::imshow() will only work appropriately with these in place. 不要完全放弃对它的调用,例如cv::imshow()只能适当地使用它们。


Edit: 编辑:

Reduce your while loop to: 将while循环减少为:

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

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

When you know how much time you need for capturing the image and just displaying it, then you can compare how much of an impact whatever algorithm you are running has on your performance. 当您知道捕获图像并显示它所需的时间时,您可以比较正在运行的算法对您的性能产​​生的影响程度。

Here is solution...everytime you read!!!..everytime you use vcap>>cap or cap.read(vcap)... you must set FPS ..25 is max..if i try to set 26 or higher it runs slow. 这里是解决方案...每次你阅读!!! ..每次你使用vcap >> cap或cap.read(vcap)...你必须设置FPS ..25是max..if我尝试设置26或更高它运行缓慢。 ...this runs 30 fps for me If you not set FPS...it runs slow too ...这对我运行30 fps如果你没有设置FPS ......它运行缓慢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM