IDS peak AFL v2.1.0
Loading...
Searching...
No Matches
Auto focus

Automatic focus control for lens focus positioning and sharpness optimization. More...

Topics

 Skip frames
 Frame processing optimization by skipping frames to reduce computational load.
 Hysteresis
 Focus stability control to prevent hunting and oscillation.
 Limit
 Focus position range limits and boundaries.
 Weighted region of interest
 Multi-region focus evaluation with configurable weights.
 Region of interest presets
 Predefined region of interest configurations for focus control.
 Sharpness algorithm
 Sharpness measurement algorithms for focus evaluation.
 Mode
 Operation mode configuration for automatic control functionality.
 Peak search algorithm
 Focus search algorithms for optimal focus positioning.
 Callbacks
 Event notification callbacks for automatic control operations.

Detailed Description

Automatic focus control for lens focus positioning and sharpness optimization.

Auto focus

The autofocus controller will automatically adjusts the lens to focus on a subject.

Enabling and automatically disabling the control function

For ongoing adaptations the Continuous mode has to be used so that it responds to deviations from the target value. To disable the control functionality automatically once the target value has been reached, (approximately) use the Once mode.

Configuring the control functions

Skip frames

The Skip frames setting defines how many frames should be skipped between processing operations. This parameter strongly influences both control speed and system stability.

How Skip Frames Works

When skip frames is set to N, the controller will:

  • Process frame 1
  • Skip frames 2, 3, ..., N+1
  • Process frame N+2
  • Skip frames N+3, N+4, ..., 2N+2
  • And so on...

Example: Skip frames = 3 means process every 4th frame (process 1, skip 3, process 1, skip 3...)

Dynamic Adjustment

Skip frames can be adjusted dynamically based on conditions:

Note
For higher frame rates, a larger value should be used for the Skip frames parameter. This reduces the number of automatic adjustments and improves system stability.
Different controllers can have different skip frame settings optimized for their specific requirements.

Hysteresis

Defines the size of the interval range after which the algorithm terminates the search.

Limit

With the minimum and maximum limit the focus search range for the focus_algorithm can be limited. The focus_limit affects whether and where a maximum value is found.

Weighted region of interest

The Weighted Region of Interest (Weighted ROI) is a concept to prioritize certain areas of an image over others. In the context of autofocus systems, the Weighted ROI is used to assign more importance to specific regions of the image, such as where the main subject is likely located, to ensure optimal focus.

Note
  • Depending on the acquisition conditions, several "sharpness maximum values" may be possible in the ROI. This may result in an error in control, especially with Continuous mode.
  • Small ROIs can cause the focus to fluctuate constantly so that an optimal focus cannot be found.

Region of interest preset

In the context of autofocus, a Region of Interest (ROI) preset defines a specific, predefined area of the camera’s image where the autofocus algorithm should evaluate sharpness and adjust focus. Instead of manually specifying the ROI dimensions and location, ROI presets offer a convenient way to apply common autofocus patterns — reducing configuration time and promoting consistency across systems.

These presets are especially useful when the autofocus behavior needs to follow standard framing assumptions, such as focusing on the center of the image, which is typical in most photography and machine vision applications.

For example, the PEAK_AFL_CONTROLLER_ROI_PRESET_CENTER preset instructs the autofocus controller to evaluate and adjust focus based only on the central portion of the image. This is ideal when the primary subject is expected to appear near the center of the frame.

Sharpness algorithm

Sets the algorithm for the sharpness calculation.

  • Tenengrad: Sobel factors in x- and y-direction and summation of the euclidean distances.
  • Sobel: Sobel factors in x- and y-direction and summation of the absolute gradient magnitudes.
  • Mean Score: Summation of the difference between two mean values from two pixel values each in x- and y-direction.
  • Histogram Variance: Standard deviation of the gray values in the histogram of the mean value.

Peak search algorithm

Sets the algorithm for the maximum value search.

Note
If no maximum value can be found after 300 iterations, the search is aborted and the focus value is reset to the previous value.
  • Golden Ratio Search: Within the preset interval range, the focus function repeatedly divides into new intervals using the Golden Ratio. The interval range is decreased with each new calculation. This process continues until the interval range is less than or equal to the set hysteresis.
  • Hill Climbing Search: Within the preset interval range, the focus function scans the register values for a maximum with medium step width until two falling sharpness values occur at the last two steps. Based on these values and the step width, the interval range and step width are reduced and the scan is repeated within this interval. This process continues until the interval range is less than or equal to the set hysteresis.
  • Global Search: Within the preset interval range, the focus function completely scans with medium step width and the maximum that occurred is memorized. Based on the maximum, the interval range and the step width are reduced and the scan is repeated within this interval. This process continues until the interval range is less than or equal to the set hysteresis.
  • Full Scan: Within the preset interval range, the focus function scans with a constant step width for an optimal result (step width: hysteresis).

Callback

It is possible to be informed for specific events via callbacks. Callbacks provide real-time feedback about controller operations and enable responsive application behavior.

Available Callback Types

Finished Callback

Finished will be called when the controller has completed its operation and reached the target value.

When called:

  • Controller completes adjustment in Once mode
  • Target value is reached within tolerance
  • Operation is cancelled or encounters an error

Processing Callback

Processing will be called during image processing to provide real-time feedback.

When called:

  • Each time an image is processed
  • Provides intermediate calculation results
  • Available in all controller modes

Callback Threading Considerations

Important Threading Notes:

  • Callbacks may be invoked from worker threads
  • Avoid blocking operations in callbacks
  • Keep callback execution time minimal
Note
The Finished callback will only be called when the controller runs in Once mode.

Examples

Note
For the automatic host control to work, the frames must be transferred to the manager. To reduce and simplify the sample code, the examples do not check the return values.
peak_afl_AutoFeatureManager_Create(&hManager, nodemapHandle);
// create a focus controller within our manager
peak_afl_controller_handle hController = 0;
// run in continuous mode
// submit the images
while (acquisitionRunning)
{
peak_afl_AutoFeatureManager_Process(hManager, imageHandle);
}