How to programmatically disable the auto-focus of a webcam?
Asked Answered
S

3

12

I am trying to do computer vision using a webcam (the model is Hercules Dualpix). I know it is not the ideal camera to use, but I have no choice here.

The problem is the auto-focus makes it hard/impossible to calibrate the camera. Anyone knows a way to disable the auto-focus feature. Or, if someone has an idea to deal with it and calibrate the camera with the auto-focus.

Shelbyshelden answered 11/11, 2009 at 20:16 Comment(0)
W
11

The Hercules cameras are UVC compliant, so they should work with the DirectShow Interface IAMCameraControl. You can set the focus to a specific value, and use the flags to set that you do not want it to be automatic. You can use IAMCameraControl::Get to poll the current state, because not all cameras do support turning off the focus.

IAMCameraControl *pCameraControl; 
HRESULT hr; 
hr = pFilter->QueryInterface(IID_IAMCameraControl, (void **)&pCameraControl); 
if (hr == S_OK) {
  long defaultFocusValue;
  hr = pCameraControl->GetRange(CameraControl_Focus,
                                NULL, // min
                                NULL, // max
                                NULL, // minstep
                                &defaultFocusValue, // default
                                NULL); // capflags
  hr = pCameraControl->Set(CameraControl_Focus, // property
                           defaultFocusValue, // value
                           CameraControl_Flags_Manual); 
} 

Focus has a range which is defined by each camera separately, so you should query it as shown to find the default value and the min, max if you want. In this example the pFilter is a pointer to the input filter that you have from DirectShow. You can get it by enumerating the devices and finding the one you want.

Welldefined answered 11/11, 2009 at 20:56 Comment(3)
Thanks. Exactly what I wanted! First it did not work, but I upgraded the camera driver and firmware, and everything is working.Shelbyshelden
How to do this for several webcams?Beefsteak
its all windows, were is the option for Linux?Barnie
N
2

Try using uvcdynctrl where -l flag shall list you the features and -s can be used to set any feature http://manpages.ubuntu.com/manpages/natty/man1/uvcdynctrl.1.html

Notion answered 19/3, 2012 at 20:12 Comment(2)
My goal was to do it programatically.Shelbyshelden
You can use something like system("uvcdynctrl -s Backlight\ Compensation 0");Notion
S
0

With the a little help of OpenCV and Python you can do this very easy:

cap = cv2.VideoCapture(1) # my webcam cap.set(3, 1280) # set the resolution cap.set(4, 720) cap.set(cv2.CAP_PROP_AUTOFOCUS, 0) # turn the autofocus off

I use OpenCV 3.1.0-dev, Python 2.7.5 and the Logitech HD Pro Webcam C920

Scalf answered 14/7, 2016 at 18:25 Comment(1)
But this does not work to capture images with multiple exposure.Nutrition

© 2022 - 2024 — McMap. All rights reserved.