简体   繁体   中英

Cannot open camera with OpenCV in Linux

I am using OpenCV 2.4.3 on linux now. And in my /dev/ there is a device called video0 that is working fine with cheese.

Currently I am using the following code to try to access the camera. This code is written in qtcreator and compiled with qmake to include the necessary library. As for the OpenCV library, I downloaded the source code and compiled it according to the instruction on the website on my machine.

++main.cpp:

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

int main(int argc, char** argv) {
    int cameraNumber = 0;
    if (argc>1)
        cameraNumber = atoi(argv[1]);
    cv::VideoCapture camera;
    camera.open(cameraNumber);
    if(!camera.isOpened()) {
        std::cerr<<"Error opening camera"<<std::endl;
        exit(1);
    }
    camera.set(CV_CAP_PROP_FRAME_WIDTH,640);
    camera.set(CV_CAP_PROP_FRAME_HEIGHT,480);

    while(true) {
        cv::Mat cameraFrame;
        camera>>cameraFrame;
        if (cameraFrame.empty()){
            std::cerr<<"No frame read from camera"<<std::endl;
            exit(1);
        }
        imshow("Camera",cameraFrame);

        char keypress = cv::waitKey(20);
        if (keypress==27) {
            break;
        }
    }
    return 0;
}

++test.pro

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/release/ -lopencv_core
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/debug/ -lopencv_core
else:unix: LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/ -lopencv_core

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/release/ -lopencv_highgui
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/debug/ -lopencv_highgui
else:unix: LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/ -lopencv_highgui

INCLUDEPATH += $$PWD/../../../../opt/OpenCV-2.4.3/include
DEPENDPATH += $$PWD/../../../../opt/OpenCV-2.4.3/include

However, when running the program it will output Error opening camera and exit, which means the camera is not opened successfully. I have been googled a while and didn't find any possible solutions to solve this issue. Could some one give me some advice on how to solve this problem? Thanks!

Perhaps you are missing some addition libraries that are required for linux.

According to OpenCV install guide, Extra Prerequisites , when using OpenCV on linux, you need to install these libraries: ffmpeg, libgstreamer, libv4l, libxine, unicap, libdc1394 2.x.

On Ubuntu 12.04 , installing the following packages worked for me (*sudo apt-get install __ *):

  • python-opencv
  • libhighgui2.3
  • libhighgui-dev
  • ffmpeg
  • libgstreamer0.10-0
  • libv4l-0
  • libv4l-dev
  • libxine2
  • libunicap2
  • libdc1394-22

Good Luck!

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