c++ - 从 Opencv 捕获网络摄像头视频

标签 c++ opencv video-capture

我正在测试我发现的一些用于捕获网络摄像头视频流的代码,它与我以前为实现相同目的所做的完全不同,但它应该是合适的方式

这是我曾经做的的方式:

CvCapture* capture;
IplImage* frame = 0;

while (true)
{
    //Read the video stream
    capture = cvCaptureFromCAM(1);
    if (! capture) break;
    frame = cvQueryFrame(capture);

    //Create a window to display 
    cvNamedWindow("Te estas viendo", CV_WINDOW_NORMAL|CV_WINDOW_KEEPRATIO);

    cvShowImage("Te estas viendo", frame);

    int c = cvWaitKey(10);
    if ( (char)c == 27 ) break;
}

//Clean and release resources
cvReleaseImage(&frame);
cvDestroyAllWindows();

这是测试代码:

VideoCapture camera;
camera.open(cameraNumber);
if (!camera.isOpened()) {
    cerr << "ERROR: Could not access the camera or video!" <<endl;
    exit(1);
}

while (true) {
    // Grab the next camera frame.
    cv::Mat cameraFrame;
    camera >> cameraFrame;
    if (cameraFrame.empty()) {
    std::cerr << "ERROR: Couldn't grab a camera frame." <<
    std::endl;
    exit(1);
}

我没有收到第一个错误行,所以它应该是打开相机,但总是无法抓取相机帧。

最佳答案

您可能在 while 循环中遗漏了右括号。还要确保在 while 循环之外创建 namedWindow。在每次迭代中创建窗口是昂贵的。见下文:

VideoCapture camera;
camera.open(cameraNumber);
namedWindow("output");

if (!camera.isOpened()) {
    cerr << "ERROR: Could not access the camera or video!" <<endl;
    return -1;
}

while (true) {
    // Grab the next camera frame.
    cv::Mat cameraFrame;
    camera >> cameraFrame;
    if (cameraFrame.empty()) {
        std::cerr << "ERROR: Couldn't grab a camera frame." <<
            std::endl;
        return -1;
    }
    imshow("output", cameraFrame);
    waitKey(10);
}

关于c++ - 从 Opencv 捕获网络摄像头视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18643543/

相关文章:

c++ - 在 typedef 中添加元素同时考虑结构的整体大小?

c++ - 如何实现两个第三方类型之间的转换?

c++ - 使用 OpenCV 的 3D 重建 C++..基本矩阵太大

ffmpeg - 在具有少量帧的 H.264 原始流中添加当前时间作为时间戳

android - 如何在 Activity 中同时捕获**视频和照片**

python - VideoWriter 在 OpenCV Python 中仅输出一个 5.7kB 文件

c++ - 使用预处理器而不是可变参数模板构建的 Boost 变体

c++ - Qt Process Events 处理时间超过指定时间

opencv - 如何在 C++ 中使用 opencv 检测瞳孔和凝视?

python - OpenCV(cv2)Python findChessboardCorners 在看似简单的棋盘上失败