Blog Archives

OpenCV 2.2 + Webcam + Windows not working

Hey there…

it has been a while since my last post… but I’m here again, although I’ll not stay long =P. This will be a short post… another quick and dirty tip. I’ve been very busy and without the time to post something really substantial. However, I think this can be helpful, so…

These days I had a problem using my webcam with OpenCV 2.2 in Windows. Most of the time I use Linux and never had any problem like this, but last week I needed to use OpenCV 2.2 + Windows for a Computer Vision project and figured out that my webcam simply do not work in this configuration: when I try to create a capture object, a window opens requiring I select the device. However, it does not succeed, even if I select the device correctly.

Oddly, this does not occur with OpenCV 2.1 + Windows. Thus, I concluded it’s a bug in OpenCV 2.2, specifically in the highgui package. Apparently, this version of OpenCV is using a back-end for cameras different from DirectShow, despite OpenCV supports it and, as I know, this is the recommended way of accessing cameras in Windows.

A solution is to force OpenCV to use DirectShow, what can be done by defining the macros HAVE_VIDEOINPUT and HAVE_DSHOW during the building configuration. Just edit the flag CMAKE_C_COMPILER adding the proper options. For example, if you’re building OpenCV using Visual Studio, append “/DHAVE_DSHOW /DHAVE_VIDEOINPUT” to that flag.

Well, this worked for me… I hope it can be helpful to you =).

PS: As a matter of fact, this is a bug in the setup of the OpenCV building, not a bug in OpenCV itself.