简体   繁体   中英

cannot open camera with ubuntu opencv c++

I have a problem I can not open camera 0 of my pc

here is the code that I use:

#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;
}

it is displaying

can not open camera

because isOpened returns false

Please make sure that the camera is correctly detected. You can do so by executing:

    $ls /dev/video*

I have also found that other programs give you a more detailed output eg ffmpeg with V4L Execute eg

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

and it will tell you if the source is busy or why the camera can't be opened, while OpenCV just returns false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM