我正在尝试通过阅读openCV中的网络摄像头流来制作一些计算机视频。我已经尝试过使用内部摄像头和外部USB摄像头,它们都可以很好地与camorama,streamer等程序配合使用。
我的代码的一部分:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "pic_manipulation.hpp"
#include "colorDetector.h"
#include <time.h>
using namespace std;
int main ( int argc, char **argv ){
string file="../test.avi";
cv::VideoCapture capture(0);//les video
if(!capture.isOpened()){
cout <<"could not read file"<<endl;
return -1;
}
double rate=capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame;
cv::namedWindow("Extract frame");
int delay=1000/rate;
while(!stop){
if(!capture.read(frame))break;
cv::imshow("Extract frame", frame);
sleep(delay/1000);
if(cv::waitKey(delay)>=0){
cout<<"stop"<<endl;
stop=true;
capture.release();
}
cout<< "one loop finished"<<endl;
}
return 0;
}
当我编译并运行程序时,我没有收到任何错误或警告,它只是返回到if(!capture.isOpened())(或者如果我跳过isOpened,它将返回到下一个if(...))。
它可以毫无问题地读取视频文件。有人知道我的opencv安装是否存在某些错误,还是导致问题的原因是linux网络摄像头设置?
我正在使用Linux Mint并使用cmake / g++构建项目
最佳答案
再看一下您的代码:
cv::VideoCapture capture(0); // open the default camera
if(!capture.isOpened()) // check if it succeeded
{
//...
}
索引
isOpened()
的0
失败的事实告诉您无法打开默认相机。由于您有其他与计算机连接的相机,因此您也可以尝试传递1
,2
,3
,...check the docs并理解每种方法的作用并返回总是很好的。
Here is a list of supported webcams,OpenCV不支持您的某些相机。那可以解释为什么默认相机不起作用。