Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock autofocus

Is there a way to prevent auto-focus from focusing, using any of the "standard" libraries, such as OpenCV, EmGU, DirectShow, etc?

I want auto-focus to find the optimal focus, then during video capture, prevent auto-focus from auto-focusing.

I know I can manually set the value, but that defeats the purpose of using auto-focus, and I have yet to find a way to obtain the optimal focus value as determined by the auto-focus.

like image 808
RickInWestPalmBeach Avatar asked Apr 30 '19 14:04

RickInWestPalmBeach


People also ask

How do I lock the camera focus on my iPhone?

How to lock the focus and exposure. Launch the Camera app from your Home screen or Lock screen. Tap the screen to choose the part of the image you want to focus on and expose. Tap and hold on your focal point until you see an AE/AF Lock banner appear at the top of the screen, to lock exposure and focus.

What is AF tracking lock?

What is AE/AF-L? Auto exposure/auto focus lock (AE/AF-L) is pretty straightforward. You select a point in your composition and the camera calculates the exposure and focus for that spot and then locks it in. You can move your phone around and recompose your shot, but the exposure and focus are locked and won't change.


1 Answers

For USB webcams that are UVC-compatible (as most are), there is a reasonable chance one can use the camera's autofocus and then lock it. To figure out whether the camera allows this via UVC, on Linux one can use v4l2-ctl, which is in package v4l-utils. v4l2-ctl -l lists all available controls, v4l2-ctl -c sets the value for a control, and v4l2-ctl -C gets the value.

For example, the following commands did the trick for a Microsoft LifeCam Cinema on an Ubuntu 16.04 box that had a simple Python OpenCV program running to display the current frame:

> v4l2-ctl -d 0 -c focus_auto=1
> v4l2-ctl -d 0 -C focus_absolute
focus_absolute: 12

After moving the object closer to the camera, the focus changed, and I got a different value for focus_absolute: (So UVC gives access to what what value the autofocus picked.)

> v4l2-ctl -d 0 -C focus_absolute
focus_absolute: 17

I then changed to manual focus and this locked the value the autofocus had picked:

> v4l2-ctl -d 0 -c focus_auto=0
> v4l2-ctl -d 0 -C focus_absolute
focus_absolute: 17

So for the LifeCam Cinema, the only thing the code would need to do is change the focus_auto control initially to auto (1) and then to manual once the focus is to be locked.

From Python, I typically run v4l2-ctl simply by using subprocess.check_output(). I recall seeing Windows libraries for UVC, but never played with them.

like image 182
Ulrich Stern Avatar answered Oct 02 '22 03:10

Ulrich Stern