1

由于一些奇怪的原因,程序无法从网络摄像头读取帧。它虽然成功打开了网络摄像头。我已经搜索了这个问题,我发现了各种解决方案,但没有一个对我有用。这是我的代码

#include <iostream>
#include <cstdlib>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

int main()
{
    // access the default webcam 
    cv::VideoCapture cap(0); 

    // Double check the webcam before start reading. 
    if ( !cap.isOpened() ){ 
        std::cerr  << "Cannot open the webcam " << std::endl;
        exit (EXIT_FAILURE);
    }

    cv::Mat frame;
    cv::namedWindow("webcam",CV_WINDOW_AUTOSIZE);

    while ( true ){

       // acquire frame 
       cap >> frame;

       // Safety checking 
       if ( !frame.data ){ 
         std::cerr << "Cannot acquire frame from the webcam " << std::endl;
            break;
       }

       cv::imshow("webcam", frame); 

       if ( cv::waitKey(30) == 27){
        std::cout << "esc key is pressed" << std::endl;
        break; 
       }
    }

    return 0;
} 

这是终止程序之前的窗口。

在此处输入图像描述

我正在使用 Windows 7(戴尔笔记本电脑)。代码在发布模式下编译,链接到 .dll。OpenCV 版本是 2.4.10。在命令提示符下

cl /EHsc main.cpp /Fetest.exe /I D:\CPP_Libraries\opencv_2.4.10\build\include /link /LIBPATH:D:\CPP_Libraries\opencv_2.4.10\build\x86\vc12\lib opencv_core2410.lib opencv_highgui2410.lib 

我在 ubuntu 中运行了相同的代码(与 Windows 7 一起双启动),它打开了网络摄像头,但没有通过 HighGUI,我得到了这个错误

HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
init done 
opengl support available 

关于这个问题的任何建议。经过一番挖掘,一些人指出cmake,因此我需要重新安装opencv并cmake正确配置。如果这确实是 ubuntu 和 Windows 的问题,是否有任何解决方案而无需重新安装库?谢谢

4

1 回答 1

-1

遵循 VideoCapture heure 的 opencv 文档:VideoCapture。它为 C++ 中的视频捕获提供了一个简单的示例。

您的代码存在一些差异, on isMat frame在循环内。

于 2015-04-29T16:13:34.423 回答