【问题标题】:VideoCapture dosnt read from webcam openCV2.4.0 linuxVideoCapture dosnt 从网络摄像头 openCV2.4.0 linux 读取
【发布时间】:2012-09-23 00:26:29
【问题描述】:

我正在尝试通过读取 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++ 构建项目

【问题讨论】:

    标签: c++ linux opencv webcam


    【解决方案1】:

    再看看你的代码:

    cv::VideoCapture capture(0);      // open the default camera
    if(!capture.isOpened())           // check if it succeeded
    {
      //...
    }  
    

    isOpened() 索引 0 失败的事实告诉您它无法打开默认相机。由于您的计算机连接了其他摄像头,您也可以尝试传递123、...

    check the docs 了解这些方法的作用和返回值总是好的。

    Here is a list of supported webcams,您的某些相机可能不受 OpenCV 支持。这可以解释为什么默认相机不起作用。

    【讨论】:

    • 我可能忘了说我已经为 VideoCapture 构造函数(0,1,2,3...)尝试了不同的输入,但没有运气。内部和 USB 摄像头在 /dev/ 中显示为 video0 和 video1。莫非这两个摄像头都不支持? (我使用的是带有内置摄像头的 acer timeline3030T 和 Logitech something usb 网络摄像头)
    • 有可能。尝试将-1 作为参数传递。
    • -1 也没有帮助。有趣的是,我可以通过 python 绑定为 opencv 读取两个相机。所以我想我的 C++ 程序有问题
    • 如果您的 C++ 代码在 !capture.isOpened() 上返回,我认为您无能为力。
    【解决方案2】:

    我通过按照这个优秀的指南安装 openCV 解决了这个问题:A Comprehensive Guide to Installing and Configuring OpenCV 2.4.2 on Ubuntu

    我怀疑我没有按照上述指南中的说明在 64 位平台上为构建 ffmpeg、v4l 设置正确的配置。无论如何,它终于成功了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-11
      • 2013-07-11
      • 2015-07-08
      • 2014-03-27
      • 1970-01-01
      • 2012-12-05
      • 2020-02-06
      • 1970-01-01
      相关资源
      最近更新 更多