iosavfoundationavcapturesessionavcapturedevice

Smooth focus ramp on AVCaptureDevice


I am using AVCaptureDevice setFocusModeLocked to implement a focus ramp from point A to point B. The way I do it is I define delta to be something like 0.03 and then repeatedly call the API to set lensPosition.

   device.setFocusModeLocked(lensPosition: pointA, completionHandler: {[weak self] (time) in
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now(), execute: { [weak self] in

                if pointA == pointB {
                    device.unlockForConfiguration()
                    return
                }

                var beginPoint = fmax(currentLensPosition + delta),Float(0))
                let endPoint = fmin(pointB, Float(1.0))


                self?.focusRampRecursive( beginPoint,
                                          pointB:endPoint,
                                          delta: delta,
                                          device: device)
            })
        })

The problem is ramp is not smooth. There are jumps visible when the ramp happens. How can I make it smooth?


Solution

  • there are a few ways.

    (1) reduce the time interval to call the function repeatedly. Only slow calling will create visual noticeable effect, if it is fast enough, human cant notice e.g your light is flashing at 50hz and you think it is constantly on

    (2)reduce the delta to smaller value. so that even if the time bettwen them is big, but change is very little each time. so eye also cant notice

    (3) use other nonlinear mapping function like exp or log function. Instead of calling linear added ramp. other exp or log might be better as A to B is typically not linearly mapped. at the visual not very sensitive region, move faster, at the visual sensitive region, move slower. Emm how should i describe this. The closest i can think of for illustration is stereo vision. at closer range, you actually have more discrete level to represent. and at far range, there is a lesser discrete level of representation. same apples for the focus.

    the ezist way for you to experience this, is to run v4l2ucp with chesse in ubuntu. drag the focus from one side to another. you will notice at close range it changes a lot, at the middle to far, the change is very minimal