Category Archives: OpenCV

cvCaptureFromCAM(CV_CAP_ANY) returns NULL

I had a weird problem when I was trying to use OpenGazer.

I used almost a whole day to make it successfully compiled, and when I ran it, it was terminated immediately by a segmentation fault.

After a few debugging I found it out that the reason was that cvCaptureFromCAM(CV_CAP_ANY) returned NULL.
I have to say it was extremely weird because I have been using Python+OpenCV on the same machine for a long while and it works completely perfect.

To demonstrate the problem, here are two pieces of code

//LANG: C++
#include <opencv/highgui.h>
#include <iostream>

int main()
    CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
    if(capture == NULL)

    return 0;
#LANG: Python
import cv2

cap = cv2.VideoCapture(0)
ret, frame =
cv2.imshow('frame', frame) # it shows a correct image from the webcam

The point is that the C++ one does not work, but the python one does.

I tried to install pwc but I couldn’t get it done. Then I installed libv4l2, and upgrade OpenCV from 2.4.2 to 2.4.8, then rebooted the system.

Then the problem was magically solved. I don’t know what exactly I did that solved it, but anyway I managed to run this amazing webcam eye tracker.

OpenGazer is an amazing webcam eye tracker that can detect which pixel on the screen you’re looking at. It can handle slight head moving since it tracks a few feature points on the face to detect the orientation, but It is still not that stable. At least it shows a probability that we can use the webcam to detect where the user is looking at.