简体   繁体   中英

Capturing from Macbook Pro iSight with Opencv

I'm trying to capture frames from a Macbook Pro's iSight using OpenCV 2.4.6, and built using the Apple LLVM 4.2 compiler on Xcode.

However, I don't receive any frames. Usually I set up a while loop to run until the frame is full, but the one below runs for ~30 seconds with no result. How can I debug this?

void testColourCapture() {

    cv::VideoCapture capture = cv::VideoCapture(0); //open default camera
    if(!capture.isOpened()) {
        fprintf( stderr, "ERROR: ColourInput capture is NULL \n" );
    }
    cv::Mat capFrame;

    int frameWaits = 0;
    while (capFrame.empty()) {
        capture.read(capFrame);
        //capture >> capFrame;
        cvWaitKey(30);
        frameWaits++;
        std::cout << "capture >> capFrame " << frameWaits << "\n";
        if (frameWaits > 1000) {
            break;
        }
    }
    imshow("capFrame", capFrame);

}

I have ensured it is not multi-threaded. Also, capture.isOpened is always returning true.

EDIT: It appears others have had this problem: OpenCV wont' capture from MacBook Pro iSight

EDIT: My procedure for installing opencv was:

$ sudo port selfupdate

$ sudo port install opencv

Then, I dragged libopencv_core.dylib, libopencv_highgui.dylib, libopencv_imgproc.dylib and libopencv_video.dylib into the Frameworks folder of my Xcode project, from /opt/local/lib

OpenCV 2.4.6 is broken and doesn't work with the iSight camera. So install 2.4.5 instead. I've written a step-for-step guide for this: http://accidentalprogramming.blogspot.ch/2013/10/opencv-installation-on-mac-os-x.html

I got it working with following code:

VideoCapture cap = VideoCapture(0); // open the video file for reading

if ( !cap.isOpened() )  // if not success, exit program
{
    cout << "Cannot open the video file" << endl;
    return -1;
}

//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

cout << "Frame per seconds : " << fps << endl;

namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

while(1)
{
    Mat frame;

    bool bSuccess = cap.read(frame); // read a new frame from video

    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read the frame from video file" << endl;
        break;
    }

    imshow("MyVideo", frame); //show the frame in "MyVideo" window

    if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
    {
        cout << "esc key is pressed by user" << endl;
        break;
    }
}

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