1

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

Community
  • 1
  • 1
escapecharacter
  • 954
  • 2
  • 12
  • 29
  • What output do you expect? Have you verified whether `capFrame.data` is not `NULL`? Also, how many iterations does the loop go through? As written, your code only loops once if the image is filled. – Aurelius Oct 04 '13 at 17:31
  • I am checking `capFrame.empty()` which never becomes true. The loop continues until frameWaits = 1000 and then it breaks. – escapecharacter Oct 04 '13 at 18:50

2 Answers2

2

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

user1169629
  • 441
  • 3
  • 12
2

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;
    }
}
  • That code is (functionally) the same as the non-working code of the original poster. Did you use a different OpenCV build perhaps? – Chris Bennet Dec 22 '14 at 15:47