Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Auto Focus in Video Input Library or OpenCV

I am using Video Input library to get frames from a webcam. I want to set FOCUS of this camera in C code.

Camera has AUTO FOCUS enabled. Isn't there a way to disable autofocus and set a specific focus value.

Regards, Saleh...

like image 699
Saleh Avatar asked Feb 08 '12 10:02

Saleh


People also ask

How do I turn off autofocus on my webcam?

Click on 'Webcam settings' link in blue letters. Click on the Camera Control tab. Uncheck the 'Auto' box next to the Focus setting.

How do I manually focus my webcam?

Check the outer casing of your cam for a manual focus ring. This plastic ring is usually set within the frame of the cam. Turning the ring adjusts the focus, just like on a microscope or a camera lens.

Can OpenCV capture video?

OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2. imwrite() which saves the image to a specified file location. But, for saving a recorded video, we create a Video Writer object.


3 Answers

If you use the OpenCV 3.1.0-dev version and Python 2.7.5, the following code snipped should help you ;)

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

With my Logitech HD Pro Webcam C920 is works fine. There are many other cool control functions inside cv2, like cv2.CAP_PROP_BRITHNESS or cv2.CAP_PROP_CONTRAST. Check out what the auto-complete is showing you ;)

like image 105
5v3n b0d3m3r Avatar answered Nov 08 '22 07:11

5v3n b0d3m3r


I have searched for this problems for couples of days and tried videoinput library and directshow. If you just want to set up the camera parameters for once (manually) inside opencv, the easiest way I found is:

VideoCapture cap(0);
cap.set(CV_CAP_PROP_SETTINGS, 1);

it will pop out a window for you to set the parameters. enough to disable autofocus.

The inconvenience of this method is that if you want to control the focal length by program, it cannot do that.

like image 36
bennygato Avatar answered Nov 08 '22 09:11

bennygato


Have you tried this: https://stackoverflow.com/a/1718009/7531 It requires you use directshow, but this should be possible.

Otherwise, have you looked at the OpenCV documentation for CameraCapture. the section concerning camera parameters shows how --in general- you control camera parameters, a quick look in videoinput.h should show you the parameters for autofocus. This is a complete example of setting autofocus this way.

IAMCameraControl is the windows interface for controlling parameters.

Note that depending on the webcam you use, and/or the specific firmware version it might not be possible to control focus / autofocus at all.

like image 31
jilles de wit Avatar answered Nov 08 '22 09:11

jilles de wit