OpenCV and connecting a webcam?

I'm struggling with connecting my webcam in OpenCV. My laptop's webcam in Windows 7 64-bit works properly. But if I connect another webcam, the code doesn't work at all:

capWebcam = cvCaptureFromCAM(0); 
if(capWebcam == NULL) { 
    printf("error: capture is NULL n"); 
    getchar(); 
    return(-1);
}

Since there isn't any webcam, it returns the message error. I disabled laptop webcam, and it worked, but I'm getting black screen.

I tried two webcams, namely Logitech HD Pro C920 and LifeCam Cinema from Microsoft. This is the line responsible to get the cam:

CvCapture* mycapWebcam = cvCaptureFromCAM(0);

I changed the 0 to another value (0-10) and this is information about this structure:

CvCapture* cvCaptureFromCAM(int index)

Initializes capturing a video from a camera.

Parameter: index – Index of the camera to be used. If there is only one camera or it does not matter what camera is used -1 may be passed.

The function cvCaptureFromCAM() allocates and initializes the CvCapture structure for reading a video stream from the camera. Currently two camera interfaces can be used on Windows: Video for Windows (VFW) and Matrox Imaging Library (MIL); and two on Linux: V4L and FireWire (IEEE1394).

To release the structure, use ReleaseCapture.

How can I fix this problem?


Try to use a different API - look at my answer to Stack Overflow question OpenCV on Mac is not opening USB web camera.

The first frame quite often is black (I don't know why). Try to wait some time and get another frame.


如果您使用其他相机,则应将1设置为索引:

capWebcam = cvCaptureFromCAM(1);
链接地址: http://www.djcxy.com/p/35408.html

上一篇: 使用findHomography时,Android opencv发生断言错误

下一篇: OpenCV和连接摄像头?