c++ - 无法使用 ubuntu opencv c++ 打开相机

标签 c++ opencv ubuntu

我有一个问题,我无法打开我电脑的摄像头 0

这是我使用的代码:

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened()) { // check if we succeeded
        cout << "cannot open camera "<< endl;
        return -1;
    }
    Mat edges;
    namedWindow("edges",1);

    for(;;){
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    return 0;
}

正在显示

can not open camera

因为 isOpened 返回 false

最佳答案

请确保正确检测到相机。你可以通过执行:

    $ls /dev/video*

我还发现其他程序可以为您提供更详细的输出,例如带有 V4L 的 ffmpeg 执行例如

     ffmpeg -f v4l2 -i /dev/video0 -vf scale=640:480 -r  20 -t 00:00:10 output.mp4

它会告诉你信号源是否繁忙或为什么无法打开相机,而 OpenCV 只会返回 false。

关于c++ - 无法使用 ubuntu opencv c++ 打开相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720178/

相关文章:

apache - 无法在 DigitalOcean 上安装 httpd

c++ - scons 如何将 sysroot 传递给 g++

python - 在第二台显示器中显示新窗口,opencv

opencv - CV_MAT_ELEM中的编译错误

python - python中的.dat文件

c++ - 使用 LIBUSB 的 USB 鼠标驱动程序

linux - HiveMQ systemctl 服务不监听端口

c++ - VirtualAlloc 的顺序似乎很重要 (c++)

c++ - 为什么我不能通过继承访问这个方法?

c++ - 设置比较器类型预期错误