由于一些奇怪的原因,程序无法从网络摄像头读取帧。它虽然成功打开了网络摄像头。我搜索了这个问题,发现了多种解决方案,但没有一个对我有用。这是我的代码
#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(戴尔笔记本电脑)。代码在 Release模式下编译,链接到 .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
关于此事的任何建议。经过一些挖掘,some ppl指出 cmake
,因此我需要重新安装 opencv 并正确配置 cmake
。如果这确实是 ubuntu 和 Windows 的问题,是否有解决此问题的方法而无需重新安装库?谢谢
最佳答案
请遵循 VideoCapture 的 opencv 文档:VideoCapture .它提供了一个使用 C++ 进行视频捕获的简单示例。
与您的代码有一些不同,Mat frame
在循环内。
关于c++ - 无法从网络摄像头读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939721/