5

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vixo7
  • 51
  • 1
  • 1
  • 2

1 Answers1

2

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.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
cyriel
  • 3,522
  • 17
  • 34
  • 1
    Thanks for your suggestions. I used the code below and it only found the iSight camera with API=0 (any) and API=500 (QuickTime). Any other ideas? `int findCamera() { for (int api =0; api <= 1200; api+=100) { for (int cam = 0; cam <= 99; cam++) { VideoCapture cap(api+cam); if (cap.open(api+cam)) { cout << "Found camera at api " << api << " camera " << cam << endl; } } } return 0; }` (sorry for the poor formatting) – vixo7 Jan 07 '13 at 03:28
  • 1. Use the latest version of OpenCV. 2. Try to build OpenCV with some other library(libraries) from the list - the best option is to check with of them is used by one program which is working fine with your camera and use this one. – cyriel Jan 07 '13 at 04:55
  • 1
    this helped me get my logitech c922 working, needed: `capture.open(CV_CAP_DSHOW);` – Nate B Aug 19 '18 at 15:44