使用 Opencv 从 Macbook Pro iSight 捕获

Capturing from Macbook Pro iSight with Opencv

本文关键字:iSight 捕获 Pro Macbook Opencv 使用      更新时间:2023-10-16

我正在尝试使用OpenCV 2.4.6从Macbook Pro的iSight捕获帧,并使用Xcode上的Apple LLVM 4.2编译器构建。

但是,我没有收到任何帧。通常我设置一个 while 循环来运行直到帧已满,但下面的循环运行 ~30 秒没有结果。我该如何调试?

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);
}

我已经确保它不是多线程的。此外,capture.isOpen 始终返回 true。

编辑:似乎其他人遇到了这个问题:OpenCV不会从MacBook Pro iSight捕获

编辑:我安装opencv的过程是:

$ 须藤端口自我更新

$ sudo port install opencv

然后,我将 libopencv_core.dylib、libopencv_highgui.dylib、libopencv_imgproc.dylib 和 libopencv_video.dylib 从/opt/local/lib 拖到 Xcode 项目的 Frameworks 文件夹中。

OpenCV 2.4.6 已损坏,无法与 iSight 相机配合使用。因此,请改为安装 2.4.5。我为此编写了一个分步指南:http://accidentalprogramming.blogspot.ch/2013/10/opencv-installation-on-mac-os-x.html

我使用以下代码得到了它:

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;
    }
}