OpenCV VideoCapture fails to open camera on OSX

I am trying to capture video frames with OpenCV 3.0, but the camera device refuses to open. When I open device 0, VideoCapture::isOpened() returns false. I have an iSight camera and it never appears to turn on.

I am building from the command line and not from XCode.

I am using the example from opencv.org verbatim:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{

    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened()) { // check if we succeeded
        std::cout << "no capture device :(n";
        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;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

My CMakeLists.txt is:

cmake_minimum_required(VERSION 2.8)
project( ocvtest )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( cap cap.cpp )
target_link_libraries( cap ${OpenCV_LIBS} )

I don't know how to troubleshoot further, and it seems OpenCV offers no clues-- I am not aware of any error messages I can query, or even a way to query the camera status, or available devices. I am not even sure if OpenCV is built correctly on my machine to support camera capture, and I see no way to find out. OpenCV just silently ignores my request to open the camera, and as far as I can tell, I am expected to mind-read what OpenCV is grumpy about.

How do I troubleshoot this? What would cause a connected camera to fail to open?


I am not familar with OSX but there is a problem in OpenCV that you can overcome it with a tricky (and not good) way. Try to to make some kind of sleep after opening the camera:

  VideoCapture cap(0); // open the default camera
  SLEEP 1000 mSEC
    if(!cap.isOpened()) { // check if we succeeded
        std::cout << "no capture device :(n";
        return -1;
    }

PS Even if it works, this is not a good solution. Try to find the problem and solve it.

链接地址: http://www.djcxy.com/p/83786.html

上一篇: 如何使用OpenCV从网络摄像头读取视频?

下一篇: OpenCV VideoCapture无法在OSX上打开相机