c++ - VideoCapture dosnt 从网络摄像头读取 openCV2.4.0 linux

标签 c++ linux opencv webcam

我正在尝试通过读取 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 支持。这可以解释为什么默认相机不起作用。

关于c++ - VideoCapture dosnt 从网络摄像头读取 openCV2.4.0 linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12685408/

相关文章:

mysql - 如何根据此 ID 恢复文件? MySQL数据恢复

linux - 内核开发中是否有类似down_interruptible()的互斥量函数?

opencv - libsvm 准确吗?

c++ - 对二维字符数组进行排序? C++

c++ - 为什么代码首先打印 0

linux - 为什么 Klocwork 提示 S_IRWXU 是一个整数?

java - 使用 OpenCV 进行光学盲文识别

c++ - 将 wostringstream 转换为 wchar_t* 乱码值

c++ - boost::multiprecision::mpz_int 的构造函数继承失败

opencv - 检测视频帧中的现实世界对象