OpenCV on Mac is not opening USB web camera

2019-01-09 13:47发布

问题:

I have been unsuccessful using OpenCV's VideoCapture.open(int) to get video capture from a USB web cam in my MacBook Pro running Mac OS X v10.7 (Lion). Using open(0) successfully gets capture from the iSight camera. But I didn't have any luck trying to find the WebCam.

The WebCam is installed and works well with Skype, and the macam driver application.

Here is a portion of the code that I'm using:

VideoCapture cap; 
for (int i = 1; i < 1500; i++) {
    if (cap.open(i))
    {
        cout << "Found camera %d\n" << i;
        break;
    }
}
if(!cap.isOpened()) {  // Check if we succeeded
    return -1;
}

If I initialize i with 0 it immediately finds the iSight camera. If I initialize i with 1, then it finds iSight again when i = 500.

How can I fix this problem?

回答1:

Try to run your code without this line: break;. Probably you will find more than just one camera, and one of them will WebCam.
Note that parameter of cap.open is not only the number of camera - it also defines which API you want to use:

Camera dispatching method: index is the camera number.

  • If given an index from 0 to 99, it tries to find the first
  • API that can access a given camera index.
  • Add multiples of 100 to select an API (comment from cap.cpp)

Possibilities (taken from highgui_c.h):

CV_CAP_ANY      =0,     // autodetect  
CV_CAP_MIL      =100,   // MIL proprietary drivers  
CV_CAP_VFW      =200,   // platform native  
CV_CAP_V4L      =200,
CV_CAP_V4L2     =200,  
CV_CAP_FIREWARE =300,   // IEEE 1394 drivers  
CV_CAP_FIREWIRE =300,  
CV_CAP_IEEE1394 =300,  
CV_CAP_DC1394   =300,  
CV_CAP_CMU1394  =300,  
CV_CAP_STEREO   =400,   // TYZX proprietary drivers  
CV_CAP_TYZX     =400,  
CV_TYZX_LEFT    =400,  
CV_TYZX_RIGHT   =401,  
CV_TYZX_COLOR   =402,  
CV_TYZX_Z       =403,  
CV_CAP_QT       =500,   // QuickTime  
CV_CAP_UNICAP   =600,   // Unicap drivers  
CV_CAP_DSHOW    =700,   // DirectShow (via videoInput)  
CV_CAP_PVAPI    =800,   // PvAPI, Prosilica GigE SDK  
CV_CAP_OPENNI   =900,   // OpenNI (for Kinect)  
CV_CAP_OPENNI_ASUS =910,   // OpenNI (for Asus Xtion)  
CV_CAP_ANDROID  =1000,  // Android  
CV_CAP_XIAPI    =1100,   // XIMEA Camera API  
CV_CAP_AVFOUNDATION = 1200  // AVFoundation framework for iOS (OS X Lion will have the same API)

Probably CV_CAP_AVFOUNDATION = 1200 is what you are looking for - try to start you loop from 1200 and don't forget to remove break; and I think that you will find what you are looking for.