0
votes

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?

2
are you sure that the webcam is open normally ? i mean , you need to disable your laptop webcam , did you?Alya'a Gamal
thanks for your reply. Yes, I'm sure that the webcam works properly, what do you mean by saying disable laptop webcam and how I do so?CroCo
if you use windows , you will find the USB camera for laptop in "device manager" in "imaging devices" then right click and disable it, i faced this problem before and it worked with meAlya'a Gamal
Thank you again so much. The webcam works but I'm getting black screen. Do you know why?CroCo
I solved the problem. I did follow another tutorial to set up my OpenCV, and it worked.CroCo

2 Answers

0
votes

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.

0
votes

If you are using another camera you should set 1 as index:

capWebcam = cvCaptureFromCAM(1);