使用OpenCV访问网络摄像头 - C ++

时间:2014-06-29 00:20:56

标签: c++ opencv

#include <opencv2/core/core.hpp>
#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<iostream>

using namespace cv;

void load(CascadeClassifier &a)
{
    try{
        a.load("F:/Projects/workspace/dispimage/src/lbpcascade_frontalface.xml");
    }
    catch(cv::Exception &e) {
        std::cout<<"Cannot load the qwespecified file";
    }
    if ( a.empty() ){
        std::cout << "ERROR: Couldn't load Face Detector (";
        std::cout<<")!";
        exit(1);
    }
}

void open(VideoCapture &camera)
{
    camera.open(0);
        if(!camera.isOpened())
        {
            std::cout<<"Camera did not open";
            exit(1);
        }

        camera.set(CV_CAP_PROP_FRAME_WIDTH, 640);
        camera.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
}

int main() {

    CascadeClassifier a;
    VideoCapture camera;
    namedWindow("CamerOP",CV_WINDOW_AUTOSIZE);
    bool b =0;

    load(a);
    open(camera);

    while (true)
    {
      // Grab the next camera frame.
        Mat cframe;
         b=camera.read(cframe);
          if (!b)
          {
            std::cerr << "ERROR: Couldn't grab a camera frame." <<std::endl;
            exit(1);
          }

          imshow("CamerOP",cframe);
    }


    std::cout<<"Haillo";

    return 1;
}

我正在尝试使用opencv访问我的网络摄像头。我创建的图像窗口打开并调整自己调整为640 * 480,然后它只是没有响应内容区域全灰色。我无法解决这个问题,因为我已经看到了几个关于这个问题的方法,而且每个地方或多或少做了同样的事情。

下面是截图 - Screen

1 个答案:

答案 0 :(得分:3)

在循环中添加cv::waitKey(30)