Automatic focusing method, device, equipment and medium

By employing a coarse-to-fine graded focusing strategy and multi-directional differential fusion calculation, along with global sharpness assessment, the problem of low efficiency and poor accuracy in existing autofocus methods is solved, achieving highly efficient and accurate autofocus results.

CN121865099APending Publication Date: 2026-04-14BEIJING SPERMCAPTURER BIOTECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-01-30
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

Existing autofocus methods are inefficient, have poor accuracy, and do not fully assess complex textures, making it impossible to perform autofocus efficiently and accurately.

Method used

A tiered focusing strategy combining coarse and fine focusing is adopted. Coarse focusing quickly locates a large area, and then fine focusing is performed near the coarse focal length position with smaller steps. The global sharpness score is calculated by multi-directional differential fusion, and the number of differential interval pixels is dynamically adjusted to improve the evaluation accuracy.

Benefits of technology

It significantly improves focusing efficiency and accuracy, enabling efficient and accurate autofocus in complex texture scenes. In particular, its sharpness assessment stability is superior to traditional methods in low-light and high-noise environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121865099A_ABST
    Figure CN121865099A_ABST
Patent Text Reader

Abstract

The invention provides an automatic focusing method, device, equipment and medium, and the method comprises the steps: firstly executing a coarse focusing step to determine a coarse focal length position, and then executing a fine focusing process which comprises the steps of iteratively executing a fine focusing step until an iteration stop condition is satisfied, and finally determining the focal length position corresponding to the image with the highest global definition score in the whole fine focusing process as the final focal length position. Wherein the global definition score of each fine focusing step is calculated according to the comprehensive definition scores, in at least two preset directions, of the sub-blocks of the current image acquired at different focal length positions in the current fine focusing step; and the comprehensive definition scores of the sub-blocks can be obtained by calculating the differential definition scores of the sub-blocks in at least two preset directions. Therefore, the focusing efficiency can be improved, and texture edge information in different directions can be captured through the global definition score calculated through multi-direction differential fusion, so that the problem that complex texture evaluation is not comprehensive in the traditional focusing process is solved, and the focusing precision is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of focusing technology, and in particular to an automatic focusing method, apparatus, device and medium. Background Technology

[0002] Autofocus is one of the core functions of machine vision systems. It uses algorithms to assess image sharpness and drives actuators (such as motors) to adjust the lens focal length to obtain optimal image quality. However, existing autofocus methods suffer from problems such as low efficiency, poor accuracy, and incomplete evaluation of complex textures. Summary of the Invention

[0003] To address the aforementioned problems, this application provides an autofocus method, apparatus, device, and medium.

[0004] Firstly, an autofocus method is provided, comprising: performing a coarse focusing step to determine a coarse focal length position; performing a fine focusing process, wherein the fine focusing process includes iteratively performing the fine focusing step until an iteration stopping condition is met, each fine focusing step comprising: acquiring an image of the object as the current image at each of a plurality of different focal length positions spaced apart by a current preset step size within the current focal length movement range, wherein the current focal length movement range is determined based on the current focal length position; calculating the global sharpness score of each current image in the current fine focusing step; and determining the current image with the highest global sharpness score in the current fine focusing step. The corresponding focal length position is used as the updated current focal length position; the focal length position corresponding to the current image with the highest global sharpness score during the entire fine focusing process is determined as the final focal length position; wherein, during the fine focusing process, for the first fine focusing step, its corresponding current focal length position is the coarse focal length position; for each subsequent fine focusing step other than the first fine focusing step, its corresponding current focal length position is the current focal length position updated in the previous fine focusing step, and wherein, for each subsequent fine focusing step, its corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step.

[0005] Secondly, an autofocus device is provided, comprising: a coarse focusing module configured to perform a coarse focusing step to determine a coarse focal length position; and a fine focusing module configured to perform a fine focusing process, the fine focusing process comprising iteratively performing fine focusing steps until an iteration stop condition is met, each fine focusing step comprising: acquiring an image of an object as a current image at each of a plurality of different focal length positions spaced apart by a current preset step size within a current focal length movement range, the current focal length movement range being determined based on the current focal length position; calculating a global sharpness score for each current image in the current fine focusing step; and determining the image with the highest global sharpness score in the current fine focusing step. The focal length position corresponding to the current image is used as the updated current focal length position; the final position determination module is configured to determine the focal length position corresponding to the current image with the highest global sharpness score in the entire fine focusing process as the final focal length position; wherein, in the fine focusing process, for the first fine focusing step, its corresponding current focal length position is the coarse focal length position; for each subsequent fine focusing step other than the first fine focusing step, its corresponding current focal length position is the current focal length position updated in the previous fine focusing step, and wherein, for each subsequent fine focusing step, its corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step.

[0006] Thirdly, an electronic device is provided, including a processor, a memory, and a program stored in the memory and capable of running on the processor, wherein the program, when executed by the processor, implements the steps of any of the autofocus methods provided in the embodiments of this application.

[0007] Fourthly, a computer-readable storage medium is provided, on which instructions are stored, which, when executed by a processor, implement the steps of any of the autofocus methods provided in the embodiments of this application.

[0008] In summary, the autofocus method, apparatus, electronic device, and computer-readable storage medium provided in this application have at least the following beneficial effects: by adopting a graded focusing strategy that combines coarse and fine focus, the coarse focal length position is first determined to achieve rapid positioning over a wide range, significantly improving focusing efficiency; then, a fine search is performed near the coarse focal length position with a smaller current preset step size to obtain a more accurate current focal length position; subsequently, by continuously updating the center of the focal length movement range and reducing the movement step size for repeated fine focusing, the final focal length position is gradually approached until the fine focusing stopping condition is met, and finally the final focal length position is selected, effectively improving focusing accuracy and focusing effect, and enabling efficient and accurate autofocus. Attached Figure Description

[0009] To more clearly illustrate the specific embodiments of this application or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0010] Figure 1 A flowchart illustrating an embodiment of the autofocus method provided in this application is shown. Figure 2 This invention provides a schematic diagram of the structure of an autofocus device according to an embodiment of the present application. Figure 3 This application illustrates another autofocus device provided in another embodiment; Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0011] To make the above and other features and advantages of this application clearer, the application is further described below with reference to the accompanying drawings. It should be understood that the specific embodiments given herein are for the purpose of explanation to those skilled in the art, and are exemplary only, not restrictive.

[0012] In the following description, numerous specific details are set forth to provide a thorough understanding of this application. However, it will be apparent to those skilled in the art that the specific details are not required to practice this application. In other instances, well-known steps or operations have not been described in detail to avoid obscuring this application.

[0013] One embodiment of this application provides an autofocus method. Figure 1 This document shows a flowchart illustrating an embodiment of an autofocus method provided in this application. Figure 1 As shown, the autofocus method includes the following steps.

[0014] S11, perform the coarse focusing step to determine the coarse focal length position.

[0015] One embodiment of this application involves a coarse focusing step that is performed within a preset focal length range with an initial preset step size. The coarse focal length position is a focal length position within the preset focal length range that can acquire a high-resolution image.

[0016] In one embodiment of this application, the focal length position refers to the position where the lens reaches a certain focal length.

[0017] The preset focal length range involved in one embodiment of this application may refer to the position range between the minimum and maximum adjustable focal length of the lens.

[0018] S12, Perform a fine focusing process. The fine focusing process includes iteratively performing fine focusing steps until the iteration stop condition is met. Each fine focusing step includes: acquiring an image of the object as the current image using each of multiple different focal length positions spaced apart by a current preset step size within the current focal length movement range; calculating the global sharpness score of each current image in the current fine focusing step; and determining the focal length position corresponding to the current image with the highest global sharpness score in the current fine focusing step as the updated current focal length position.

[0019] An embodiment of this application involves iteration stopping conditions including at least one of the following: the number of iterations in the fine focusing step meets a preset number; the difference between the highest global sharpness score in the current fine focusing step and the highest global sharpness score in the previous fine focusing step is not greater than a preset threshold. The preset number of iterations is not less than 3, and the preset threshold can be set according to requirements.

[0020] In one embodiment of this application, the current focal length movement range can be determined based on the current focal length position. In one embodiment, the current focal length movement range is a positional interval formed by extending a preset distance upwards and downwards from the current focal length position as the center. The preset distance is M times the current preset step size, where M is not less than 3.

[0021] In one embodiment of this application, the current preset step size is smaller than the preset initial step size.

[0022] In one embodiment of this application, during the fine focusing process, for the first fine focusing step, the corresponding current focal length position is the coarse focal length position. Furthermore, for each subsequent fine focusing step other than the first fine focusing step, the corresponding current focal length position is the current focal length position updated in the previous fine focusing step.

[0023] One embodiment of this application relates to a preprocessed grayscale image acquired at a specified focal length position within the current focal length movement range of the lens. The preprocessing includes, but is not limited to, color conversion and noise reduction.

[0024] Specifically, a color image of the object is acquired at a specified focal length position within the current focal length movement range of the lens. This color image is then converted to a grayscale image, and Gaussian filtering is applied to the grayscale image for noise reduction to suppress noise interference with the difference calculation. Thus, after color conversion and noise reduction, the current image is obtained.

[0025] In one embodiment of this application, each fine focusing step can be performed as follows: within the current focal length movement range, multiple different focal length positions are spaced out according to the current preset step size; an image of the object is acquired at each focal length position; each image of the object is used as the current image; the global sharpness score of each current image is calculated; and the focal length position corresponding to the current image with the highest global sharpness score is determined as the updated current focal length position, and the updated current focal length position is used as the current focal length position in the next fine focusing step.

[0026] It should be noted that in the fine focusing step, the number of current images is the same as the number of positions at multiple different focal lengths. The distance between two adjacent focal length positions among these multiple different focal lengths is the current preset step size.

[0027] In one embodiment of this application, during the fine focusing process, for the first fine focusing step, the corresponding current focal length position is the coarse focal length position. For each subsequent fine focusing step other than the first fine focusing step, the corresponding current focal length position is the current focal length position updated in the previous fine focusing step, and wherein, for each subsequent fine focusing step, the corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step.

[0028] In one embodiment of this application, the first fine focusing step, acquiring multiple current images can be specifically performed by starting from the current focal length position, i.e., the coarse focal length position, moving M current initial steps in one direction (upward or downward), and acquiring an image of an object as the current image after each current initial step. After the movement in that direction ends, returning to the coarse focal length position, moving M current initial steps in another direction, and again acquiring an image of an object as the current image after each current initial step.

[0029] In one embodiment of this application, the current preset step size corresponding to the first fine focusing step is smaller than the initial preset step size. For example, the current preset step size corresponding to the first fine focusing step is 1 / L of the initial preset step size, where L is not less than 2.

[0030] In one embodiment of this application, for each subsequent fine focusing step, the corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step. For example, for each subsequent fine focusing step, the corresponding current preset step size is 1 / L of the current preset step size corresponding to the previous fine focusing step.

[0031] For example, when L=2, the current preset step size corresponding to the second fine focusing step is 1 / 4 of the initial preset step size. The current focal length position in the second fine focusing step is the current focal length position updated in the first fine focusing step.

[0032] In one embodiment of this application, the fine focusing step is executed iteratively until the fine focusing result after multiple iterations meets the iteration stopping condition, at which point the fine focusing step is terminated.

[0033] S13, determine the focal length position corresponding to the current image with the highest global sharpness score during the entire fine focusing process as the final focal length position.

[0034] In one embodiment of this application, the current image with the highest global sharpness score refers to the current image corresponding to the highest global sharpness score among all images.

[0035] One embodiment of this application involves a final focal length position that corresponds to the peak sharpness during the focal length adjustment process. The image obtained at the final focal length position has higher sharpness than the image obtained at other focal length positions.

[0036] In one embodiment of this application, the highest global sharpness score is selected from all global sharpness scores obtained during the fine focusing process, and the current image with the highest global sharpness score is found, and the focal length position corresponding to the current image is taken as the final focal length position.

[0037] In some embodiments of this application, in S11, performing a coarse focusing step to determine the coarse focal length position may include: acquiring an initial image of the object using each of a plurality of focal length positions spaced apart by an initial preset step size within a preset focal length range; calculating the initial global sharpness score of each initial image; and determining the focal length position corresponding to the initial image with the highest initial global sharpness score as the coarse focal length position.

[0038] In one embodiment of this application, the current preset step size corresponding to the first fine focusing step is smaller than the initial preset step size.

[0039] The preset focal length range involved in one embodiment of this application may refer to the position range between the minimum and maximum adjustable focal length of the lens.

[0040] In one embodiment of this application, within a preset focal length range, multiple different focal length positions are spaced out according to an initial preset step size. An image of the object is acquired at each focal length position, and each image of the object is used as an initial image. The global sharpness score of each image is calculated, and the focal length position corresponding to the initial image with the highest global sharpness score is determined as the coarse focal length position. Specifically, two adjacent focal length positions among the multiple different focal length positions are separated by an initial preset step size.

[0041] For example, acquire an initial image at the initial focal length position of the lens, move the focal length position of the lens down by an initial preset step, and acquire another initial image at the moved focal length position. Repeat the above movement and acquisition steps until the focal length position of the lens exceeds the lower maximum position of the preset focal length range, then stop acquiring images and stop moving down. Return the lens to the initial focal length position, move the focal length position of the lens up by an initial preset step, and acquire an initial image at the moved focal length position. Repeat the above movement and acquisition steps until the focal length position of the lens exceeds the upper maximum position of the preset focal length range.

[0042] In some of the above embodiments, the coarse focusing step can obtain the coarse focal length position, that is, the focal length position within the preset focal length range where the image is relatively clear, which can provide a reference benchmark for subsequent fine focusing.

[0043] The applicant discovered that existing focusing methods based on the Brenner Sharpness algorithm suffer from incomplete evaluation due to reliance on a single horizontal differential. Therefore, this applicant proposes a technique that calculates the global sharpness score through multi-directional differential fusion, thereby achieving comprehensive capture of image edge information and improving the accuracy of sharpness evaluation in complex texture scenes. Furthermore, the applicant found that the texture complexity varies greatly across different regions of an image; therefore, this applicant proposes a block-based approach to calculate the global sharpness score.

[0044] In some embodiments of this application, in S12, calculating the global sharpness score of each current image in the current fine focusing step includes: for each current image, dividing the current image into multiple non-overlapping sub-blocks, calculating the comprehensive sharpness score of each sub-block in at least two preset directions; and performing a weighted average of the comprehensive sharpness scores of all sub-blocks to obtain the global sharpness score of the current image.

[0045] An embodiment of this application relates to a sub-block's overall sharpness score in at least two preset directions, which is used to measure the overall sharpness of the sub-block in at least two preset directions.

[0046] In one embodiment of this application, the gray-level variance of a sub-block can be used as a weighting coefficient for a weighted average. The gray-level variance of a sub-block is the ratio of the sum of squares of the differences between the gray-level values ​​of each pixel in the sub-block and the average gray-level value of the current image to the number of pixels in the sub-block.

[0047] In one embodiment of this application, the global sharpness score of the current image can be calculated according to the following formula.

[0048] (1) Where K represents the total number of sub-blocks in the current image. This represents the grayscale variance value of the i-th sub-block. The overall sharpness score for the i-th sub-block is... This represents the global sharpness score of the current image.

[0049] In the above embodiments, by dividing into sub-blocks, calculating the overall sharpness in multiple preset directions, and weighted averaging, a balance is achieved between local and global features, utilization of multi-directional features, and noise suppression and detail preservation, which significantly improves the accuracy and robustness of image sharpness assessment.

[0050] In some embodiments of this application, calculating the overall sharpness score of each sub-block in at least two preset directions includes: calculating the differential sharpness score of the sub-block in at least two preset directions respectively; and using the weight coefficients corresponding to the at least two preset directions respectively, weighting and fusing the differential sharpness scores of the sub-block in at least two preset directions to obtain the overall sharpness score of the sub-block.

[0051] The differential sharpness score involved in one embodiment of this application can be used to measure the sharpness of a sub-block in a preset direction. In one embodiment of this application, calculating the differential sharpness score of a sub-block in at least two preset directions can refer to calculating the differential sharpness of the sub-block in each preset direction.

[0052] In one embodiment of this application, the weight coefficients corresponding to at least two preset directions are assigned based on the importance of the direction. The sum of the weight coefficients for all preset directions is 1.

[0053] In one embodiment of this application, the overall sharpness score of a sub-block can be calculated using the following formula.

[0054] (2) in, This represents the overall sharpness score of the sub-block. This represents the weight coefficient for the s-th preset direction. This represents the differential sharpness score of the sub-block in the s-th preset direction.

[0055] The preset direction involved in one embodiment of this application can be a horizontal direction, a vertical direction, or a diagonal direction.

[0056] In one embodiment of this application, at least two preset directions include a horizontal direction and a vertical direction.

[0057] In another embodiment of this application, at least two preset directions may further include a 45-degree diagonal direction and / or a 135-degree diagonal direction.

[0058] In one embodiment of this application, when there are at least two preset directions, including the horizontal and vertical directions, the overall sharpness score of the sub-block can be calculated by the following formula.

[0059] (3) in, This represents the weighting coefficient in the horizontal direction. This represents the differential sharpness score of the sub-block in the horizontal direction. This represents the weighting coefficient in the vertical direction. This represents the differential sharpness score of the sub-block in the vertical direction. Optionally, It is 0.5. It is 0.5.

[0060] In one embodiment of this application, when there are at least two preset directions, including the horizontal direction, the vertical direction and the 45-degree diagonal direction, the overall sharpness score of the sub-block can be calculated by the following formula.

[0061] (4) in, This represents the weighting coefficient in the horizontal direction. This represents the differential sharpness score of the sub-block in the horizontal direction. This represents the weighting coefficient in the vertical direction. This represents the differential sharpness score of the sub-block in the vertical direction. This represents the weighting coefficient along the 45-degree diagonal direction. This represents the differential sharpness score of the sub-block along a 45-degree diagonal direction. Optionally, It is 0.4. It is 0.4, and It is 0.2.

[0062] In some of the above embodiments, a comprehensive sharpness score is obtained by fusing differential sharpness scores from multiple directions. This enables the capture of horizontal, vertical, and diagonal edge information through multi-directional differential fusion, addressing the problem of incomplete evaluation of complex textures in traditional algorithms and improving robustness. Furthermore, by combining noise reduction processing with the comprehensive sharpness score calculated through differential weighted fusion, its sharpness evaluation stability in low-light and high-noise environments is significantly better than that of the traditional Brenner algorithm.

[0063] It should be noted that since the initial global sharpness score used throughout this paper is calculated using the same method as the global sharpness score of the current image, the calculation method for the initial global sharpness score will not be repeated here.

[0064] The applicant discovered that the texture complexity varies greatly across different regions of an image, and that the differential sharpness score calculated using a fixed pixel interval cannot accurately assess the image sharpness in a preset direction. Therefore, the applicant proposes to dynamically and adaptively adjust the number of differential interval pixels for sub-blocks based on their texture, thereby calculating a more accurate differential sharpness score. This makes the assessment of image sharpness more accurate, while simultaneously reducing the computational load in high-texture areas and improving the detail capture capability in low-texture areas.

[0065] In some embodiments of this application, calculating the differential sharpness score of a sub-block in at least two preset directions includes: determining the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions based on the grayscale variance value of the sub-block; and calculating the differential sharpness score of the sub-block in each preset direction based on the number of differential interval pixels corresponding to the sub-block.

[0066] In one embodiment of this application, the difference interval pixel number refers to the number of pixels that separate the pixels in a pixel pair used for difference calculation when calculating the differential sharpness score. In one embodiment of this application, the smaller the difference interval pixel number, the stronger the ability to capture subtle details when calculating the differential sharpness score. The larger the difference interval pixel number, the stronger the ability to suppress high-frequency noise and the smaller the computational redundancy when calculating the differential sharpness score.

[0067] In one embodiment of this application, the grayscale variance value of a sub-block can reflect the texture complexity of the sub-block. In one embodiment of this application, the grayscale variance value of a sub-block can be compared with a preset texture threshold to determine the number of differential interval pixels of the sub-block.

[0068] Specifically, the grayscale variance of the sub-block is compared to see if it is not less than the high texture threshold. If the grayscale variance of the sub-block is not less than the high texture threshold, the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions is determined as a first value.

[0069] When the grayscale variance of a sub-block is less than the high texture threshold, compare whether the grayscale variance of the sub-block is not less than the low texture threshold. When the grayscale variance of a sub-block is not less than the low texture threshold, determine the second value as the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions.

[0070] When the grayscale variance of a sub-block is less than the low texture threshold, the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions is determined to be a third value.

[0071] In other words, when the grayscale variance of a sub-block is not less than the high texture threshold, the sub-block can be identified as a high-texture sub-block, and the differential sharpness score of the sub-block in each preset direction can be calculated using a first value. When the grayscale variance of a sub-block is less than the high texture threshold and not less than the low texture threshold, the sub-block can be identified as a medium-texture sub-block, and the differential sharpness score of the sub-block in each preset direction can be calculated using a second value. When the grayscale variance of a sub-block is less than the low texture threshold, the sub-block can be identified as a low-texture sub-block, and the differential sharpness score of the sub-block in each preset direction can be calculated using a third value.

[0072] One embodiment of this application involves a high texture threshold that is greater than a low texture threshold. For example, the high texture threshold is 200, and the low texture threshold is 50.

[0073] One embodiment of this application involves a first value that is greater than a second value. The second value is greater than a third value. For example, the first value is 3, the second value is 2, and the third value is 1.

[0074] In some embodiments of this application, the differential sharpness score of the sub-block in at least two preset directions is calculated based on the number of differential interval pixels corresponding to the sub-block. This includes: selecting pixel pairs that satisfy the number of differential interval pixels along a preset direction within the sub-block, calculating the sum of squares of the grayscale differences of all pixel pairs, thereby obtaining the differential sharpness score of the sub-block in a preset direction.

[0075] In one embodiment of this application, the pixel pair refers to two pixels within a sub-block that are selected along a preset direction and are separated by a number of pixels.

[0076] In one embodiment of this application, when the preset direction is horizontal, the pixel pairs are (x, y) and (x+d, y). Here, x represents the horizontal coordinate of the pixel, y represents the vertical coordinate of the pixel, d represents the number of differential interval pixels, and x+d is less than the maximum horizontal coordinate of the sub-block.

[0077] In one embodiment of this application, the differential sharpness score of the sub-block in the horizontal direction It can be calculated using the following formula.

[0078] (5) in, This represents the grayscale value of a pixel (x, y).

[0079] In one embodiment of this application, when the preset direction is vertical, the pixel pairs are (x, y) and (x, y+d). Here, x represents the horizontal coordinate of the pixel, y represents the vertical coordinate of the pixel, d represents the number of differential interval pixels, and y+d is less than the maximum vertical coordinate of the sub-block.

[0080] In one embodiment of this application, the differential sharpness score of the sub-block in the vertical direction It can be calculated using the following formula.

[0081] (6) In one embodiment of this application, when the preset direction is a 45-degree diagonal direction, the pixel pairs are (x, y) and (x+d, y+d). Here, x represents the horizontal coordinate of the pixel, y represents the vertical coordinate of the pixel, d represents the number of differential interval pixels, x+d is less than the maximum horizontal coordinate of the sub-block, and y+d is less than the maximum vertical coordinate of the sub-block.

[0082] In one embodiment of this application, the differential sharpness score of the sub-block along the 45-degree diagonal direction. It can be calculated using the following formula.

[0083] (7) In one embodiment of this application, when the preset direction is a 135-degree diagonal direction, the pixel pairs are (x, y) and (xd, yd). Here, x represents the horizontal coordinate of the pixel, y represents the vertical coordinate of the pixel, d represents the number of differential interval pixels, xd is not less than the minimum horizontal coordinate of the sub-block, and yd is less than the minimum vertical coordinate of the sub-block.

[0084] In one embodiment of this application, the differential sharpness score of the sub-block along the 135-degree diagonal direction. It can be calculated using the following formula.

[0085] (8) In one embodiment of this application, for each of at least two preset directions, the differential sharpness of the sub-block in each preset direction is calculated according to the differential sharpness score calculation formula corresponding to each preset direction.

[0086] In some embodiments of this application, the autofocus method further includes: driving a motor to move the lens to the optimal focal length position.

[0087] Another aspect of this application provides an autofocus device. Figure 2 This application provides a schematic diagram of the structure of an autofocus device according to an embodiment of the present application. Figure 2 As shown, the autofocus device includes the following modules.

[0088] The coarse focusing module 21 is configured to perform a coarse focusing step to determine the coarse focal length position.

[0089] The fine focusing module 22 is configured to perform a fine focusing process, which includes iteratively executing fine focusing steps until an iteration stop condition is met. Each fine focusing step includes: acquiring an image of the object as the current image at each of a plurality of different focal length positions spaced apart by a current preset step size within the current focal length movement range, wherein the current focal length movement range is determined based on the current focal length position; calculating the global sharpness score of each current image in the current fine focusing step; and determining the focal length position corresponding to the current image with the highest global sharpness score in the current fine focusing step as the updated current focal length position.

[0090] The final position determination module 23 is configured to determine the focal length position corresponding to the current image with the highest global sharpness score during the entire fine focusing process as the final focal length position.

[0091] In the fine focusing process, for the first fine focusing step, the corresponding current focal length position is the coarse focal length position; for each subsequent fine focusing step other than the first fine focusing step, the corresponding current focal length position is the current focal length position updated in the previous fine focusing step, and for each subsequent fine focusing step, the corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step.

[0092] In the above embodiments, by adopting a graded focusing strategy that combines coarse and fine focus, the coarse focal length position is first determined to achieve rapid positioning over a wide range, significantly improving focusing efficiency. Then, a fine search is performed near the coarse focal length position with a smaller current preset step size to obtain a more accurate current focal length position. Subsequently, by continuously updating the center of the focal length movement range and reducing the movement step size, fine focusing is repeated to gradually approach the final focal length position until the fine focusing stopping condition is met, and finally the final focal length position is selected. This effectively ensures focusing accuracy, improves focusing effect, and enables efficient and accurate autofocus.

[0093] In some embodiments of this application, the coarse focusing module 21 is configured to acquire an initial image of the object at each of a plurality of focal length positions spaced apart by an initial preset step size within a preset focal length range; calculate the initial global sharpness score of each initial image; and determine the focal length position corresponding to the initial image with the highest initial global sharpness score as the coarse focal length position; wherein the current preset step size corresponding to the first fine focusing step is smaller than the initial preset step size.

[0094] In some embodiments of this application, the fine focus module 22 is configured to divide the current image into multiple non-overlapping sub-blocks for each current image, calculate the overall sharpness score of each sub-block in at least two preset directions, and perform a weighted average of the overall sharpness scores of all sub-blocks to obtain the global sharpness score of the current image.

[0095] In some embodiments of this application, the fine focus module 22 is configured to calculate the differential sharpness scores of the sub-blocks in at least two preset directions; and to perform weighted fusion of the differential sharpness scores of the sub-blocks in at least two preset directions using the weight coefficients corresponding to the at least two preset directions to obtain the comprehensive sharpness score of the sub-blocks.

[0096] In some embodiments of this application, the fine focus module 22 is configured to determine the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions based on the grayscale variance value of the sub-block; and to calculate the differential sharpness score of the sub-block in each preset direction based on the number of differential interval pixels corresponding to the sub-block.

[0097] In some embodiments of this application, the fine focus module 22 is configured to compare whether the grayscale variance value of a sub-block is not less than a high texture threshold; when the grayscale variance value of the sub-block is not less than the high texture threshold, determine the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions as a first value; when the grayscale variance value of the sub-block is less than the high texture threshold, compare whether the grayscale variance value of the sub-block is not less than a low texture threshold, wherein the low texture threshold is less than the high texture threshold; when the grayscale variance value of the sub-block is not less than the low texture threshold, determine the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions as a second value, wherein the second value is less than the first value; when the grayscale variance value of the sub-block is less than the low texture threshold, determine the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions as a third value, wherein the third value is less than the second value.

[0098] In some embodiments of this application, the autofocus device 20 further includes a focusing module configured to drive a motor to move the lens to the optimal focal length position.

[0099] Another aspect of this application provides an autofocus device. Figure 3 This application also illustrates another autofocus device 30 provided in another embodiment of the present application. The autofocus device 30 includes an image acquisition module 31, a preprocessing module 32, a sharpness calculation module 33, an adaptive interval module 34, and a focus control module 35.

[0100] The image acquisition module 31 and preprocessing module 32 are used to acquire the initial image and the current image. The sharpness calculation module 33 is used to calculate the differential sharpness score, the overall sharpness score, and the global sharpness score. The adaptive interval module 34 is used to determine the number of pixels for the differential interval. The focus control module 35 is used to determine the optimal focal length position and move the lens to the optimal focal length position.

[0101] Based on the above, Figure 3 The autofocus device shown in this application also provides another autofocus method, wherein the initial preset step size is the displacement distance corresponding to 100 steps of the motor, L in the step size update rule is 2, the initial image and the current image acquired by the image acquisition module 31 are both 1920×1080 in size, the sub-block size is 32×32, and there are a total of 1980 sub-blocks. This autofocus method includes the following steps.

[0102] S41, the image acquisition module 31 acquires the image of the object when the lens is at a specified focal length position, and the image is processed into a specified image by the preprocessing module 32.

[0103] In one embodiment of this application, the designated image may be an initial image or a current image.

[0104] S42, the adaptive interval module 34 divides the specified image into sub-blocks, calculates the grayscale variance of each sub-block, and determines the number of differential interval pixels for each sub-block.

[0105] S43, the sharpness calculation module 33 calculates the differential sharpness score of each sub-block in each preset direction, the comprehensive sharpness score of each sub-block, and the global sharpness score of the specified image based on the differential interval pixel number of each sub-block.

[0106] S44, the focus control module 35 controls the lens to move down H preset steps from the initial position, return to the initial position, and then move up H preset initial steps. Steps S41-S43 are repeated for each movement to obtain the global sharpness score of multiple initial images.

[0107] S45, taking the focal length position corresponding to the initial image with the highest global sharpness score as the center position, the focus control module 35 controls the lens to first move down from the center position by M current preset steps, return to the center position, and then move up by M preset initial steps. Each time the movement is repeated, steps S41-S43 are obtained to obtain multiple current images and multiple current images' global sharpness scores, where M is less than H.

[0108] S46. In each iteration step, the focal length position corresponding to the current image with the highest global sharpness score is taken as the center position of the next iteration. After a preset number of iterations, the highest score is selected from all global sharpness scores.

[0109] S47, the focus control module 35 controls the lens to move to the focal length position corresponding to the specified image with the highest score, and completes the focusing.

[0110] In the above embodiments, the autofocus method has the following beneficial effects: 1. More comprehensive evaluation: Multi-directional differential fusion captures horizontal, vertical, and diagonal edge information, solving the problem of incomplete evaluation of complex textures by traditional algorithms, and improving robustness by more than 30%; 2. Balance between effect and accuracy: Adaptive interval adjustment dynamically optimizes the differential interval according to the sub-block texture, reducing the computational load by 20%-40% in high-resolution scenes and improving the detail capture accuracy by 25% in low-texture scenes; 3. High robustness: Combining Gaussian filtering noise reduction and variance-weighted fusion, the sharpness evaluation stability is significantly better than the traditional Brenner algorithm in low-light and high-noise environments; 4. Wide adaptability: The computational load is controllable, and it can be adapted to both high-precision scenes on PCs and real-time scenes on embedded systems (such as ARM architecture), especially suitable for fields such as high-magnification industrial inspection and medical microscopy imaging.

[0111] In practical applications, the applicant applies the autofocus method provided in the embodiments of this application to high-magnification microscopy focusing scenarios. The focusing results of this autofocus method and traditional focusing methods (such as those based on the traditional Brenner algorithm) are compared in Table 1.

[0112] Table 1

[0113] The above comparison results show that, compared with traditional focusing methods, the autofocus method provided in this application improves the alignment accuracy and efficiency, and has a strong ability to capture details in low-texture scenes.

[0114] It should be understood that the specific features, operations, and details described herein with respect to the methods of this application can also be similarly applied to the apparatus and system of this application, or vice versa. Furthermore, each step of the methods of this application described above can be performed by a corresponding component or unit of the apparatus or system of this application.

[0115] It should be understood that the various modules / units of the device of this application can be implemented wholly or partially through software, hardware, firmware, or a combination thereof. Each module / unit can be embedded in the processor of the controller in hardware or firmware form or independent of the processor, or it can be stored in the memory of the controller in software form for the processor to call to execute the operation of each module / unit. Each module / unit can be implemented as an independent component or module, or two or more modules / units can be implemented as a single component or module.

[0116] In another aspect, this application provides an electronic device. Figure 4 This application provides a schematic diagram of the structure of an electronic device according to an embodiment of the present application. Figure 4As shown, the electronic device 50 includes a processor 51, a memory 52, and a program stored in the memory and capable of running on the processor. When the program is executed by the processor, it implements the steps of the autofocus method provided in any of the above embodiments.

[0117] In one embodiment, the electronic device 50 may include a processor, memory, network interface, communication interface, etc., connected via a system bus. The processor of the electronic device 50 can be used to provide necessary computing, processing, and / or control capabilities. The memory of the electronic device 50 may include non-volatile storage media and internal memory. The non-volatile storage media may store an operating system, computer programs, etc. The internal memory can provide an environment for the operation of the operating system and computer programs in the non-volatile storage media. The network interface and communication interface of the electronic device 50 can be used to connect and communicate with external devices via a network.

[0118] This application also provides a computer-readable storage medium storing instructions, wherein when executed by a processor, the instructions implement the steps of the autofocus method provided in any of the above embodiments.

[0119] Those skilled in the art will understand that the method steps of this application can be performed by a computer program instructing related hardware, such as a controller or processor. The computer program can be stored in a non-transitory computer-readable storage medium, and its execution causes the steps of this application to be performed. Depending on the context, any reference herein to memory, storage, or other media may include non-volatile or volatile memory. Examples of non-volatile memory include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), flash memory, magnetic tape, floppy disk, magneto-optical data storage device, optical data storage device, hard disk, solid-state drive, etc. Examples of volatile memory include random access memory (RAM), external cache memory, etc.

[0120] The technical features described above can be combined arbitrarily. Although not all possible combinations of these technical features are described, any combination of these technical features should be considered to be covered by this specification, provided that such combination does not contain contradictions.

[0121] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.

Claims

1. An autofocus method, characterized in that, The method includes: Perform a coarse focusing step to determine the coarse focal length position; The fine focusing process includes iteratively executing fine focusing steps until the iteration stop condition is met. Each fine focusing step includes: acquiring an image of the object as the current image at each of multiple different focal length positions spaced apart by a current preset step size within the current focal length movement range, wherein the current focal length movement range is determined based on the current focal length position; calculating the global sharpness score of each current image in the current fine focusing step; and determining the focal length position corresponding to the current image with the highest global sharpness score in the current fine focusing step as the updated current focal length position. The focal length position corresponding to the current image with the highest global sharpness score during the entire fine focusing process is determined as the final focal length position. In the fine focusing process, for the first fine focusing step, the corresponding current focal length position is the coarse focal length position; for each subsequent fine focusing step other than the first fine focusing step, the corresponding current focal length position is the current focal length position updated in the previous fine focusing step, and In this case, for each subsequent fine focusing step, the corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step.

2. The method according to claim 1, characterized in that, The coarse focusing step, which determines the coarse focal length position, includes: The initial image of the object is obtained by taking each focal position from multiple focal positions within a preset focal length range and spaced apart by an initial preset step size; the initial global sharpness score of each initial image is calculated; and the focal position corresponding to the initial image with the highest initial global sharpness score is determined as the coarse focal position. In this case, the current preset step size corresponding to the first fine focusing step is smaller than the initial preset step size.

3. The method according to claim 2, characterized in that, The current preset step size corresponding to the first fine focusing step is 1 / L of the initial preset step size, and L is not less than 2; For each subsequent fine focusing step, the corresponding current preset step size is 1 / L of the current preset step size corresponding to the previous fine focusing step.

4. The method according to claim 1, characterized in that, The iteration stopping condition includes at least one of the following: The number of iterations for the fine focusing step meets the preset number; The difference between the highest global sharpness score in the current fine focusing step and the highest global sharpness score in the previous fine focusing step is no greater than a preset threshold.

5. The method according to claim 1, characterized in that, The calculation of the global sharpness score of each current image in the current fine focusing step includes: For each current image, the current image is divided into multiple non-overlapping sub-blocks, and the comprehensive sharpness score of each sub-block is calculated in at least two preset directions; The overall sharpness score of the current image is obtained by taking a weighted average of the overall sharpness scores of all sub-blocks.

6. The method according to claim 5, characterized in that, The calculation of the overall sharpness score for each sub-block in at least two preset directions includes: Calculate the differential sharpness score of each sub-block in at least two preset directions; Using the weight coefficients corresponding to the at least two preset directions, the differential sharpness scores of the sub-block in the at least two preset directions are weighted and fused to obtain the comprehensive sharpness score of the sub-block.

7. The method according to claim 6, characterized in that, The calculation of the differential sharpness scores of the sub-blocks in at least two preset directions includes: Based on the grayscale variance value of the sub-block, determine the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions; Based on the number of differential interval pixels corresponding to the sub-block, the differential sharpness score of the sub-block in each preset direction is calculated.

8. The method according to claim 7, characterized in that, The step of determining the number of differential interval pixels required to calculate the differential sharpness scores of the sub-block in at least two preset directions based on the grayscale variance value of the sub-block includes: Compare whether the grayscale variance value of the sub-block is not less than the high texture threshold; When the grayscale variance of the sub-block is not less than the high texture threshold, the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions is determined to be a first value. When the grayscale variance of the sub-block is less than the high texture threshold, compare whether the grayscale variance of the sub-block is not less than the low texture threshold, where the low texture threshold is less than the high texture threshold. When the grayscale variance of the sub-block is not less than the low texture threshold, the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions is determined to be a second value, and the second value is less than the first value. When the grayscale variance of the sub-block is less than the low texture threshold, the number of differential interval pixels required to calculate the differential sharpness score of the sub-block in at least two preset directions is determined to be a third value, which is less than the second value.

9. The method according to any one of claims 5-8, characterized in that, The step of calculating the differential sharpness score of the sub-block in at least two preset directions based on the number of differential interval pixels corresponding to the sub-block includes: Within the sub-block, along a preset direction, select pixel pairs that satisfy the differential interval pixel number, calculate the sum of squares of the grayscale differences of all pixel pairs, and thus obtain the differential sharpness score of the sub-block in the preset direction.

10. An automatic focusing device, characterized in that, The device includes: The coarse focus module is configured to perform the coarse focus step to determine the coarse focal length position; A fine focus module is configured to perform a fine focus process, which includes iteratively executing fine focus steps until an iteration stop condition is met. Each fine focus step includes: acquiring an image of the object as the current image at each of a plurality of different focal length positions spaced apart by a current preset step size within the current focal length movement range, wherein the current focal length movement range is determined based on the current focal length position; calculating the global sharpness score of each current image in the current fine focus step; and determining the focal length position corresponding to the current image with the highest global sharpness score in the current fine focus step as the updated current focal length position. The final position determination module is configured to determine the focal length position of the current image with the highest global sharpness score during the entire fine focusing process as the final focal length position. In the fine focusing process, for the first fine focusing step, the corresponding current focal length position is the coarse focal length position; for each subsequent fine focusing step other than the first fine focusing step, the corresponding current focal length position is the current focal length position updated in the previous fine focusing step, and In this case, for each subsequent fine focusing step, the corresponding current preset step size is smaller than the current preset step size corresponding to the previous fine focusing step.

11. An electronic device, characterized in that, It includes a processor, a memory, and a program stored in the memory and executable on the processor, wherein the program, when executed by the processor, implements the steps of the autofocus method as described in any one of claims 1-9.

12. A computer-readable storage medium, characterized in that, Instructions are stored on the computer-readable storage medium, which, when executed by a processor, implement the steps of the autofocus method as described in any one of claims 1-9.