Automatically in focus
The uEye LE USB 3.1 Gen 1 AF board level cameras now focus automatically
The new contrast-based autofocus extends the application range of uEye cameras with active focus, such as the uEye LE AF board-level cameras. The automatic is based on these camera models' "active" liquid lens control and works both triggered in single-shot mode and also in continuous focus control. The autofocus can be individually configured according to the application and ensures perfectly focused images in no time.
Background
Updating the uEye software to version 4.93 brings the uEye LE AF board level cameras a software-based focus automatic to control the previously manual liquid lens control. This opens up even more possibilities for using the cameras in applications with variable object distances. An adjustable region of interest in size and position and various image sharpness measurement algorithms with many configuration options ensure fast and reliable determination of the maximum image sharpness in the desired focus range in all situations.
The general use of the focus automatic of the "uEye LE USB 3.1 Gen 1 AF" is explained in detail in the uEye software suite 4.93 manual. All configuration options of the uEye cockpit focus dialogue can also be used in your own applications using the uEye API function is_Focus(). The manual contains a detailed description of this function with all interface parameters and source code samples.
The techtip provides you with additional help and tips how to pre-configure the autofocus optimally for your application.
Automatic focus
Autofocus is a function that automatically adjusts the camera lens to the correct object distance for a sharp image. The autofocus system uses the liquid lens control of the uEye LE AF board level camera to focus at different distances. In "single-shot" mode, the automatic is triggered by software and works only until the maximum sharpness is found in the measuring window. Then it deactivates itself. In "continuous" operation, the image sharpness is permanently measured and the lens is readjusted accordingly. The automatic controls the cameras liquid lens based on the image data analysis on the host PC in a closed loop.
With this so-called "closed-loop autofocus", settings of the liquid lens and the pixel path (contrast, binning, etc.) have direct impact on the image content and thus on the sharpness measurement result of the image. This evaluation in turn forms the basis for adjusting the liquid lens before the next image is acquired. As the optimum focus value cannot be calculated with this image-based method, the closed control loop including image acquisition, measurement and focal length adjustment must first be passed through several times. First the analysis of the value pairs determined from the focus value and the calculated image sharpness provide an indication for determining the best possible (maximum) image sharpness in the relevant focus field.
When is an image in focus?
There are different methods for measuring image sharpness, but they are based on only two basic principles. The first principle is edge sharpness. Large gray value jumps between adjacent pixels are searched for in the actual image content, allowing edges or contours to be highlighted. The contrast of these edges in turn allows drawing conclusions about the sharpness of the image. The clearer the edge image, the higher the degree of sharpness of the original image.
The second principle is based on the analysis of histogram values of the image. The standard deviation can be used to calculate a measure for the variation of the pixel values around the average gray value of the image. The larger the variance, the higher are the existing gray value edges and the contrast of the image. If the image becomes unfocused, the previously large gray value jumps (edges) assume a gradient in the form of a ramp. The variation and thus the contrast of the image decreases. To put it simply: The sharper the image, the higher the gray scale spread and thus the contrast in the image.
Focused - Unfocused
Stronger edges and greater standard deviation indicate higher contrast and focus level.
Measure image sharpness
In practice, however, object surfaces are rarely ideal for optical focus measurement. This often leads to unreliable, unstable measurement results. However, the uEye software's automatic focus function offers you different measurement methods for different image situations.
The algorithms "Mean Score" and "Tenengrad" of the focus automatic analyze the image data and use the principle of edge sharpness. To do this, they analyze pixel by pixel, including the relationship to direct neighboring pixels. "Mean Score" uses simpler pixel calculations and a smaller neighborhood than "Tenengrad", which makes it a bit faster but also more noise sensitive than the "Tenengrad" algorithm.
The "histogram deviation" uses the histogram values of the region of interest (ROI) to determine the image sharpness. This holistic approach gives this algorithm a filter character. Sharpness limits are displayed as clear curves in the measured values window. Noise has only small influence on the result. However, if there are several near focus maxima in the searched focus area, these cannot be clearly differentiated by the histogram deviation. Like a low pass, the algorithm "smoothes" the maxima to a covering curve. The analysis of smaller pixel neighborhoods at "Tenengrad" and "Mean Score" has a much clearer effect on gray value changes. Edges are thereby made more clearly visible. Both algorithms show clearly differentiable individual maxima, but are also more affected by interferences such as noise.
Due to different computing principles the three algorithms also differ in the calculation time of the sharpness value per image. Complex pixel transformations such as Sobel and Laplace filters, for example, generate a higher computing load than the simple standard deviation calculation from the mean gray value. The analysis of larger image areas (large region of interest) can quickly decrease the acquired camera frame rate. Depending on the scene and the requirements of your application, you can have influence on the speed and accuracy of the measurement by selecting an available algorithm. The calculation methods "Mean Score" and "histogram deviation" are more suitable for time-critical calculations on average PC hardware. The Tenengrad calculation efficiency, on the other hand, can quickly be at the expense of the achievable frame rate with larger measurement windows.
Setting the region of interest and focus range
The sharpness measurement is typically performed in a region of interest (here called "measurement window"). This helps to speed up calculation time.
The larger the measurement window, the more computing time and power the host CPU needs for the sharpness calculation per image. The set frame rate can decrease if the calculation time (per image) is too long! To achieve fast frame rates, choose a measurement window that is as small as possible.
Specify the position and size of the measurement window so that only the image content to be focused is marked, with one focus plane and thus one maximum focus. However, if there are several local sharpness maxima in the specified search range and you are using a peak search algorithm with termination condition, this algorithm terminates the search prematurely when the first clear maximum has been detected.
To specify the search for the "correct" focus level more precisely, you have the following options:
1. Restricting the focus range of the lens
Limit the focus measurements to only the focus range of the lens that your application requires. Depending on the distance between the lens and the image scene and the depth of field of the lens used, a small area is sufficient. This also reduces the maximum search time, as fewer sharpness calculations are required.
If you are not sure where to set the focus range, we recommend displaying the measured values window and a "full scan" of the entire focus range with a small hysteresis.
2. Change the size and position of the measurement window
Only a few clear features are sufficient for the algorithms to determine the sharpness (small structures, a clear edge, clear gray value scattering). The smaller the measurement window, the faster the focus is determined and the more clearly only one image area is in focus. Therefore, make the measurement window as small as possible and as large as necessary. The more reproducible the image position of the focus objects is in your application, the better you can preset the size and position of the measurement window.
Find maximum sharpness
The purpose of the focus automatic is to find as quickly as possible the focus setting with which the sharpness values in the measurement window of the captured images are greatest. In order to find these, the uEye software offers several basic peak search algorithms, which are suitable depending on the scene (image content) or your requirements for speed and quality. The most reliable but slowest way is to sequentially set each focus setting, take one picture at a time, and determine its sharpness. In order to speed up the search for the optimum sharpness value, only "random samples" are taken according to different strategies by skipping focus settings first (intervals). Only in further runs, when "peaks" are located, these are then scanned much finer. The individual algorithms reduce the search time through abort conditions, but this does not always lead to optimal results. Therefore the uEye software gives you the choice of the search strategy you want to use.
Since the focus calculation of the focus automatic is based on the image content and several measurements are necessary to find a maximum, the image content should not change during the search process!
Characteristics of the search algorithms
Golden Ratio Search:
- Searches and refines the maximum search by subdividing the search area by the "golden ratio".
- Very fast search even in large search areas.
- When using a large search range, it is possible that the maximum sharpness is skipped during the rough search! → Reduce search range in advance or use only with unimodal sharpness gradient (with only one sharpness maximum).
Hill Climbing Search:
- Starts the coarse search in the far range with a large search interval.
- Detects "a maximum" when the sharpness values no longer increase and aborts searching in the following focus area with the first decrease in sharpness value.
- Then refines the measurement results in the found maximum range in subsequent search runs with a smaller search intervals.
- Only aborts when the hysteresis is undercut.
- Faster than "Global Search"
- The first maximum found may not be the global maximum!
- When using a large search range, it is possible that the maximum sharpness is skipped during the rough search! → Reduce search range in advance or use only with unimodal sharpness gradient.
Global Search:
- Starts the coarse search in the far range with a large search interval.
- Coarse search in the complete search area.
- Then refines the measurement results in the found maximum range in subsequent search runs with a smaller search intervals.
- Only aborts when the hysteresis is undercut.
- Faster than "Full Scan".
- When using a large search range, it is possible that the maximum sharpness is skipped during the rough search! → Reduce search range in advance or use only with unimodal sharpness gradient.
Full Scan:
- Calculates the sharpness values in the entire search range in a single pass without a search strategy.
- The hysteresis defines the measuring interval.
- Well suitable for a "blind search", since it reliably finds the global maximum of sharpness with a small hysteresis.
- Time consuming with small hysteresis and large search range. In the worst case, 1024 focus values (max. count of focus settings of the uEye LE AF) are determined.
Some algorithms search too soon close to focus settings where the global maximum is not located if the depth of field in the area of interest is not large enough. In such a case, usually only a "Global Search" or even a "Full Scan" is successful.
Accuracy of maximum search
The hysteresis specifies the minimum step size, at which the search for the maximum sharpness is stopped. This also determines the achievable accuracy of the maximum search. Only in "Full Scan" the hysteresis specifies the constant step size that is used to scan the whole search area.
Application examples for a focus camera
In general, the uEye LE AF board level camera with liquid lens and automatic focus is suitable for any application involving variable object distances. The manual or automatic adjustment of the focus plane by the camera software also helps when no access to the lens is possible. Especially in mobile applications, such as on a robot arm, the small board level camera always focuses objects or codes to be read clearly after each robot movement.
In combination with image processing such as HALCON, the uEye automatic focus system can do a lot more. With images of any focus setting, HALCON can, for example, calculate a continuously sharp image that sharply depicts an object scene at all focus levels. A kind of "HDR" image related to the focus instead of the exposure (High Dynamic Focus).
By "scanning" an object scene on all focal planes, HALCON can even calculate depth information ("Depth from Focus") on the basis of the sharpness measurements in these images.
In this way, the volume of objects can also be determined without a 3D camera. It is also possible to check whether objects reside on the same floor. Ready-made focus image processing examples that can be used directly with the uEye LE USB 3.1 Gen 1 AF can be found in the scope of standard HALCON installation. Further code examples and a detailed description of the focus function with all its interface parameters can be found in the uEye Software Suite 4.93 manual.