Ask Your Question
1

Video capture on MacBook

asked 2013-07-28 16:25:56 -0500

opencvdev gravatar image

I'm attempting to access the build-in camera on a MacBook Pro (running OS X 10.8.3, with OpenCV 2.4.6.1) with code I found elsewhere:

int main( int argc, const char** argv ) {

CvCapture* capture = NULL;

if ((capture = cvCaptureFromCAM(-1)) == NULL)
{
    std::cerr << "!!! ERROR: vCaptureFromCAM No camera found\n";
    return -1;
}

cvNamedWindow("webcam", CV_WINDOW_AUTOSIZE);
cvMoveWindow("webcam", 50, 50);

IplImage* src = NULL;
for (;;)
{
    if ((src = cvQueryFrame(capture)) == NULL)
    {
        std::cerr << "!!! ERROR: vQueryFrame\n";
        break;
    }

   cvShowImage("webcam", &src);
}

cvReleaseCapture(&capture);
return 0;

}

The camera appears to be found, the green light goes on, yet the frame returned by cvQueryFrame is always NULL. What might be wrong here?

Thanks!

edit retag flag offensive close merge delete

Comments

Try using the C++ interface and the Mat type of elements instead of the old depricated C style API. Look into this nice tutorial to get the basics of image reading and displaying.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-28 16:49:49 -0500 )edit
1

Also try the OpenCV 2.4.6 (not .1).

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-07-29 01:50:41 -0500 )edit

3 answers

Sort by ยป oldest newest most voted
5

answered 2013-09-21 20:14:41 -0500

stereomatching gravatar image

Better don't use C-style API anymore, it is more verbose, error-prone, no performance benefits and will be deprecated in the future(deprecate c api).

Here is the codes of c++ api, much easier and cleaner(we don't need goto again, because of the power of RAII, c++ programmers rarely need to use goto in their codes).

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

    int main()
    {
    cv::VideoCapture cap;
    cap.open(0);

    if( !cap.isOpened() )
    {
        std::cerr << "***Could not initialize capturing...***\n";
        std::cerr << "Current parameter's value: \n";
        return -1;
    }

    cv::Mat frame;
    while(1){
        cap >> frame;
        if(frame.empty()){
            std::cerr<<"frame is empty"<<std::endl;
            break;
        }

        cv::imshow("", frame);
        cv::waitKey(10);
    }

    return 1;
}

run on MacOSX 10.8.x

edit flag offensive delete link more

Comments

@stereomatching I've tried both your solution and the C api solution provided below. But it always return frame.empty for me while the C api works just fine do i have to avoid the first frame or something? (i saw some answer about first empty frame) p.s I'm running 10.9 Thanks in adv

JeffC gravatar imageJeffC ( 2013-11-14 04:07:41 -0500 )edit
2

answered 2013-09-20 22:34:13 -0500

marcelosalloum gravatar image

Try this, it worked perfectly for me:

#include <iostream>
#include <highgui.h>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;

#define CAMERA_OUTPUT_WINDOW_NAME "camera-output"

int main(int argc, char **argv)
{
    CvCapture *camCapture;
    int ret = 0;

    if (!(camCapture = cvCaptureFromCAM(CV_CAP_ANY))) {
        cout << "Failed to capture from camera" << endl;

        ret = 1;

        goto exitCameraOpenFailed;
    }

    cout << "Camera opened successfully" << endl;

    cvNamedWindow(CAMERA_OUTPUT_WINDOW_NAME, CV_WINDOW_AUTOSIZE);

    IplImage *cameraFrame;
    int grabFrameRet;

    while (true) {
        if ((cameraFrame = cvQueryFrame(camCapture))) {
            cvShowImage(CAMERA_OUTPUT_WINDOW_NAME, cameraFrame);
        }

        if (cvWaitKey(60) != -1) {
            cout << "Input" << endl;
            break;
        }
    }

    cout << "Done" << endl;

    cvReleaseCapture(&camCapture);
    cvDestroyWindow(CAMERA_OUTPUT_WINDOW_NAME);
exitCameraOpenFailed:
    return ret;
}
edit flag offensive delete link more
0

answered 2013-09-06 00:28:02 -0500

ronkadonk gravatar image

updated 2013-09-06 00:49:03 -0500

I had the same problem and I see that the following code works:

#include <iostream>
#include <highgui.h>
#include <imgproc/imgproc.hpp>

using namespace std;

#define CAMERA_OUTPUT_WINDOW_NAME "camera-output"

int main(int argc, char **argv)
{
    CvCapture *camCapture;
    int ret = 0;

    if (!(camCapture = cvCaptureFromCAM(CV_CAP_ANY))) {
        cout << "Failed to capture from camera" << endl;

        ret = 1;

        goto exitCameraOpenFailed;
    }

    cout << "Camera opened successfully" << endl;

    cvNamedWindow(CAMERA_OUTPUT_WINDOW_NAME, CV_WINDOW_AUTOSIZE);

    IplImage *cameraFrame;
    int grabFrameRet;

    while (true) {
        if ((cameraFrame = cvQueryFrame(camCapture))) {
            cvShowImage(CAMERA_OUTPUT_WINDOW_NAME, cameraFrame);
        }

        if (cvWaitKey(60) != -1) {
            cout << "Input" << endl;
            break;
        }
    }

    cout << "Done" << endl;

    cvReleaseCapture(&camCapture);
    cvDestroyWindow(CAMERA_OUTPUT_WINDOW_NAME);
exitCameraOpenFailed:
    return ret;
}

The cvQueryFrame appears to be non-blocking and if there's no frame to return, it just returns null.

edit flag offensive delete link more

Comments

But again using the old C-style API is discouraged :) It is easy to switch to the newer C++ version which is under active development :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-07 04:19:42 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-07-28 16:25:56 -0500

Seen: 13,344 times

Last updated: Mar 25 '14