An auto-focusing method and device, electronic equipment and storage medium

By using a two-stage focusing method to select the effective area and perform fine-tuning, the problem of multi-target focusing difficulties in complex scenes of 8K ultra-high-definition cameras in existing technologies has been solved, achieving efficient and accurate autofocus effects that are suitable for various image formats and high frame rate scenes.

CN116828302BActive Publication Date: 2026-05-26SICHUAN NATIONAL INNOVATION VISION UHD VIDEO TECHNOLOGY CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SICHUAN NATIONAL INNOVATION VISION UHD VIDEO TECHNOLOGY CO LTD
Filing Date
2023-05-29
Publication Date
2026-05-26

AI Technical Summary

Technical Problem

Existing autofocus algorithms struggle to accurately locate multiple targets in real time in complex scenarios with 8K ultra-high-definition cameras, and their real-time performance faces challenges as image size and resolution increase, especially at high frame rates where they cannot meet the requirements for automatic real-time autofocus.

Method used

A two-stage focusing method is adopted: first, the effective focus area is screened and narrowed down with a large step size, and then fine-tuning is performed with a small step size. Combined with image segmentation and sharpness evaluation value, the sharpness curve is ensured to be unimodal.

Benefits of technology

It improves focusing accuracy and operating efficiency, is suitable for complex scenarios, and is compatible with formats such as 4K, 1080P, and 8K, meeting the real-time focusing requirements at high frame rates.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116828302B_ABST
    Figure CN116828302B_ABST
Patent Text Reader

Abstract

This invention discloses an autofocus method, apparatus, electronic device, and storage medium, belonging to the field of autofocus technology. The autofocus method includes: adjusting the focal length within a first focusing range according to a first preset step size, acquiring a first image captured by the camera at each focal length, and determining an effective focusing area and a second focusing range based on the first image; adjusting the focal length within a second focusing range according to a second preset step size, acquiring a second image captured by the camera at each focal length, and determining the optimal focusing position based on the sharpness evaluation value of the effective focusing area in the second image; wherein the first preset step size is greater than the second preset step size. This invention performs focusing in two stages: the first stage uses a large step size to filter the effective focusing area and narrow the focusing range, and the second stage uses a small step size for focusing and uses fewer but more effective areas for focusing, thus achieving higher operating efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of autofocus technology, and in particular relates to an autofocus method, device, electronic device and storage medium. Background Technology

[0002] As an important component of the 3A (Auto White Balance AWB, Auto Focus AF, Auto Exposure AE) algorithm, the autofocus algorithm combines stepper motors and other methods to control the optical systems of digital cameras and camcorders to complete the focusing operation, obtain clear, high-quality images, and solve the problems of high time consumption, low efficiency and strong subjectivity caused by early manual focusing. It has wide applications in satellite remote sensing, security monitoring, medical microscopy and other fields.

[0003] Existing autofocus strategies mainly include the following: hill-climbing method, global search method, Fibonacci search method, golden ratio search method, and function fitting method. The hill-climbing method is simple but susceptible to local extrema; the global search method is slow and difficult to meet real-time requirements; the Fibonacci search method and golden ratio search method oscillate at the focus position, resulting in low real-time performance; and the function fitting method has low fitting accuracy. Regarding the global search algorithm, researchers have proposed an improved method that divides the focusing process into two parts: the first step uses a larger step size to search for peaks, completing coarse focusing; the second step, based on the coarse focusing result, uses a smaller step size to further search for peaks in the sharpness curve, completing fine focusing. These methods work well in scenes with only one focused object, but they present two challenges for the complex scenes of 8K ultra-high-definition cameras: First, the uncertainty of the application scene means that the same scene may contain multiple targets at different distances, resulting in multiple peaks in the sharpness curve, making it difficult for the focusing strategy to effectively determine the sharpest image; Second, the algorithms with exceptionally good focusing performance face huge challenges in real-time performance as the size and resolution of the processed images increase. In 8K and above scenes, even additional dedicated hardware is needed to barely match the subjective effect of the human eye, but automatic real-time focusing at high frame rates of 30FPS and above is basically a pipe dream. Summary of the Invention

[0004] The purpose of this invention is to overcome the shortcomings of the prior art and provide an autofocus method, apparatus, electronic device and storage medium.

[0005] The objective of this invention is achieved through the following technical solution:

[0006] According to a first aspect of the present invention, an autofocus method includes:

[0007] Adjust the focal length within the first focal range according to the first preset step size, acquire the first image captured by the camera at each focal length, and determine the effective focus area and the second focal range based on the first image.

[0008] Adjust the focal length within the second focusing range according to the second preset step size, acquire the second image captured by the camera at each focal length, and determine the best focusing position based on the sharpness evaluation value of the effective focusing area in the second image.

[0009] The first preset step size is greater than the second preset step size.

[0010] Further, the focal length is adjusted within the first focusing range according to a first preset step size, and a first image is acquired by the camera at each focal length. Based on the first image, an effective focus area and a second focusing range are determined, including:

[0011] The stepper motor used to drive the camera lens moves within the first focusing range according to the first preset step size until the first preset condition is met and the stepper motor stops moving.

[0012] Acquire the first image captured by the camera at each focal length, and divide the first image into several sub-regions;

[0013] Set all sub-regions in the first image of the first frame as valid regions;

[0014] After each movement of the stepper motor, the sub-region in the current first image that corresponds to the effective region in the previous frame of the first image is set as the effective region, and then the effective region in the current first image that meets the second condition is set as the invalid region.

[0015] The effective focus area is formed by combining all the effective areas in the last frame of the first image.

[0016] The second focusing range is defined as (position-length / 2n, position), where position is the position of the stepper motor when it stops moving according to the first preset step length, length is the maximum movement value of the first focusing range, and length / n is the first preset step length.

[0017] Furthermore, the first preset condition is: the number of effective regions in the first image whose sharpness evaluation value is on a downward trend after the stepper motor is moved is greater than or equal to the number of effective regions in the first image whose sharpness evaluation value is on an upward trend.

[0018] Furthermore, the second preset condition is as follows: after moving the stepper motor, if the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is greater than the number of effective regions in the first image whose sharpness evaluation value is on an upward trend, then the effective regions in the first image whose sharpness evaluation value is on an upward trend satisfy the second preset condition; if the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is less than the number of effective regions in the first image whose sharpness evaluation value is on an upward trend, then the effective regions in the first image whose sharpness evaluation value is on a downward trend satisfy the second preset condition.

[0019] Furthermore, the autofocus method further includes:

[0020] The first and second images captured by the camera are converted to grayscale.

[0021] Furthermore, the focal length is adjusted within the second focusing range according to a second preset step size, and a second image is acquired by the camera at each focal length. The optimal focusing position is determined based on the sharpness evaluation value of the effective focusing area in the second image, including:

[0022] The stepper motor used to drive the camera lens is moved multiple times within the second focusing range according to the second preset step size to obtain the second image captured by the camera at each focal length.

[0023] The second image is divided into several sub-regions, and the effective focus area in the second image is determined;

[0024] Calculate the sharpness evaluation value of the effectively focused area in the second image;

[0025] The position of the stepper motor when the sharpness evaluation value of the effective focus area in the second image is maximized is determined as the optimal focus position.

[0026] According to a second aspect of the present invention, an autofocus device comprises:

[0027] The first focusing module is used to adjust the focal length within a first focusing range according to a first preset step size, acquire a first image captured by the camera at each focal length, and determine an effective focusing area and a second focusing range based on the first image.

[0028] The second focusing module is used to adjust the focal length within the second focusing range according to the second preset step size, acquire the second image captured by the camera at each focal length, and determine the best focusing position based on the sharpness evaluation value of the effective focusing area in the second image.

[0029] The first preset step size is greater than the second preset step size.

[0030] Furthermore, the first focusing module is specifically used for:

[0031] The stepper motor used to drive the camera lens moves within the first focusing range according to the first preset step size until the first preset condition is met and the stepper motor stops moving.

[0032] Acquire the first image captured by the camera at each focal length, and divide the first image into several sub-regions;

[0033] Set all sub-regions in the first image of the first frame as valid regions;

[0034] After each movement of the stepper motor, the sub-region in the current first image that corresponds to the effective region in the previous frame of the first image is set as the effective region, and then the effective region in the current first image that meets the second condition is set as the invalid region.

[0035] The effective focus area is formed by combining all the effective areas in the last frame of the first image.

[0036] The second focusing range is defined as (position-length / 2n, position), where position is the position of the stepper motor when it stops moving according to the first preset step length, length is the maximum movement value of the first focusing range, and length / n is the first preset step length.

[0037] According to a third aspect of the present invention, an electronic device includes a stepper motor, a lens, a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the autofocus method according to a first aspect of the present invention.

[0038] According to a fourth aspect of the present invention, a computer-readable storage medium stores a computer program thereon, which, when executed by a processor, implements the steps of the autofocus method according to a first aspect of the present invention.

[0039] The beneficial effects of this invention are:

[0040] (1) The present invention performs focusing in two stages. The first stage uses a large step size to filter the effective focus area and narrow the focus range. The second stage uses a small step size to focus and uses fewer and more effective areas to focus, thereby achieving higher operating efficiency.

[0041] (2) The present invention divides the image into different regions, so that the sharpness curve of each region has a single-peak trend, and then the region with consistent peaks is retained as the effective region, ensuring that the final sharpness curve has a single peak and improving the focusing accuracy.

[0042] (3) The method of the present invention is simple, effective and versatile, and is applicable to complex scenarios with multiple interfering targets. It can be compatible with formats such as 4K, 1080P and 8K. Attached Figure Description

[0043] Figure 1 This is a flowchart of one embodiment of the autofocus method in this invention;

[0044] Figure 2 This is a schematic diagram illustrating the region division of an image in this invention;

[0045] Figure 3 This is a flowchart of an embodiment of the present invention in which focusing is performed according to a first preset step size;

[0046] Figure 4 This is a test dataset for one case.

[0047] Figure 5 Test dataset two for one case;

[0048] Figure 6 Test dataset three for one case;

[0049] Figure 7 This is a schematic diagram showing the change curve of the sharpness evaluation value in a case study.

[0050] Figure 8 This is a block diagram illustrating one embodiment of the autofocus device in this invention. Detailed Implementation

[0051] The technical solution of the present invention will be clearly and completely described below with reference to the embodiments. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0052] See Figures 1 to 8 This invention provides an autofocus method, apparatus, electronic device, and storage medium:

[0053] The first aspect of this embodiment provides an autofocus method, including steps S100 to S200, such as... Figure 1 As shown. The following is a detailed explanation.

[0054] Step S100. Adjust the focal length within the first focusing range according to the first preset step size, acquire the first image captured by the camera at each focal length, and determine the effective focusing area and the second focusing range based on the first image.

[0055] The first focusing range is the range of focal lengths that can be adjusted when the lens is focusing. The first preset step size can be set according to actual needs. For example, the first focusing range is the movement range of the stepper motor (0 to length), which is used to drive the camera lens to move. Length is the maximum movement value of the first focusing range. The first preset step size step1 is length / n. The value of n can be set according to actual needs. For example, if the first preset step size step1 is set to length / 50, the value of n can be set to a smaller value to reduce the amount of data and improve processing efficiency.

[0056] In some embodiments, step S100 specifically includes: moving a stepper motor for driving a camera lens within a first focusing range according to a first preset step size until the first preset condition is met and then stopping the movement of the stepper motor; acquiring a first image captured by the camera at each focal length and dividing the first image into several sub-regions; setting all sub-regions in the first frame of the first image as valid regions; setting the sub-regions in the current first image obtained after each movement of the stepper motor that correspond to the valid regions in the previous frame of the first image as valid regions, and then setting the valid regions in the current first image that meet the second condition as invalid regions; forming a valid focusing region from all valid regions in the last frame of the first image; and determining the second focusing range as (position-length / 2n, position), where position is the position of the stepper motor when the movement of the stepper motor according to the first preset step size stops, length is the maximum movement value of the first focusing range, and length / n is the first preset step size.

[0057] Specifically, a corresponding first image is acquired when the stepper motor is at each position. This first image is then divided into several sub-regions, with each first image having the same number and size of sub-regions. For example, all sub-regions in the first first image are set as valid regions. For images other than the first, the valid regions in that first image are the remaining valid regions after removing those that satisfy the second condition from all valid regions in the previous first image. For example, if the second first image has 9 valid regions, then the 9 sub-regions in the third first image corresponding to the valid regions in the second first image are first set as valid regions. Then, it is determined whether any of these 9 valid regions in the third first image satisfy the second condition. For instance, if 3 valid regions satisfy the second condition, then these 3 valid regions are set as invalid regions.

[0058] The images captured by the camera are in RGB format. For example, the image dimensions are 7680(W) x 4320(H) x 3 (Channels), where W represents the width of the image, H represents the height of the image, and Channels represents the number of channels in the image. For example, ... Figure 2 As shown, the image is divided into 5x3 sub-regions. The value ranges of each sub-region (1-15) in the image are as follows: (0-W / 5, 0-H / 3), (W / 5-W*2 / 5, 0-H / 3), (W*2 / 5-W*3 / 5, 0-H / 3), (W*3 / 5-W*4 / 5, 0-H / 3), (W*4 / 5-W, 0-H / 3), (0-W / 5, H / 3-H*2 / 3), (W / 5-W*2 / 5, H / 3-H*2 / 3), (W*2 / 5~W*3 / 5,H / 3~H*2 / 3), (W*3 / 5~W*4 / 5,H / 3~H*2 / 3), (W*4 / 5~W,H / 3~H*2 / 3),, (0~W / 5,H*2 / 3~H), ( W / 5~W*2 / 5,H*2 / 3~H), (W*2 / 5~W*3 / 5,H*2 / 3~H), (W*3 / 5~W*4 / 5,H*2 / 3~H), (W*4 / 5~W,H*2 / 3~H).

[0059] The first preset condition is: after moving the stepper motor, the number of valid regions in the first image whose sharpness evaluation value is on a downward trend is greater than or equal to the number of valid regions in the first image whose sharpness evaluation value is on an upward trend. For example, after moving the stepper motor, if the number of valid regions whose sharpness evaluation value is on a downward trend is 6 and the number of valid regions whose sharpness evaluation value is on an upward trend is 5, then the first preset condition is met, and the stepper motor is stopped moving according to the first preset step size.

[0060] The second preset condition is as follows: After moving the stepper motor, if the number of valid regions in the first image whose sharpness evaluation value is on a downward trend is greater than the number of valid regions in the first image whose sharpness evaluation value is on an upward trend, then the valid regions in the first image whose sharpness evaluation value is on an upward trend satisfy the second preset condition; if the number of valid regions in the first image whose sharpness evaluation value is on a downward trend is less than the number of valid regions in the first image whose sharpness evaluation value is on an upward trend, then the valid regions in the first image whose sharpness evaluation value is on a downward trend satisfy the second preset condition. For example, after moving the stepper motor, if the number of valid regions whose sharpness evaluation value is on a downward trend is 6 and the number of valid regions whose sharpness evaluation value is on an upward trend is 5, then the valid regions whose sharpness evaluation value is on an upward trend satisfy the second preset condition, and in this case, the valid regions whose sharpness evaluation value is on an upward trend are set as invalid regions.

[0061] like Figure 3 As shown, within the first focusing range, the stepper motor moves once by a first preset step size step1. The sharpness evaluation value V of each effective region in the first image is calculated when the stepper motor is at its current position. i,jWhere i represents the number of steps the stepper motor moves, and j represents the region number. The difference ΔV is based on the sharpness evaluation value. i,j Determine the trend of sharpness evaluation values ​​for each effective area. Based on the difference in sharpness evaluation values ​​ΔV... i,j Determine the trend of the sharpness evaluation value of each effective area: if ΔV i,j If ΔV is greater than 0, the sharpness evaluation value of the effective area is on an upward trend; if ΔV i,j If the value is less than 0, the sharpness evaluation value of the effective area is on a downward trend. The number of effective areas on an upward trend and the number of effective areas on a downward trend are counted: if the number of effective areas on an upward trend is greater than the number of effective areas on a downward trend, the effective areas on a downward trend are set as invalid areas, and the stepper motor is moved once again by the first preset step size (step1), and subsequent steps are executed; if the number of effective areas on an upward trend is less than or equal to the number of effective areas on a downward trend, the effective areas on an upward trend are set as invalid areas, and focusing according to the first preset step size is stopped.

[0062] For example, the first focusing range is the movement range of the stepper motor, and the first preset step size step1 is set to length / 50. If the image is divided into 15 regions, then j∈(1~15).

[0063] In some embodiments, the Laplace function is used as the sharpness evaluation function in this embodiment. The calculation process is as follows:

[0064] ① The Laplace operator is a direction-independent linear space operator, and its template is as follows:

[0065]

[0066] ② Calculate the convolution of the Laplacian operator with the region image:

[0067] G(u,v)=L*I=-1*f(u-1,v-1)-1*f(u,v-1)-1*f(u+1,v-1)-1*f(u-1,v)+8*f(u,v)-1*f(u+1,v)-1*f(u-1,v+1)-1*f(u,v+1)-1*f(u+1,v+1)

[0068] Where I represents the effective region, G(u,v) represents the convolution of the image of the effective region with the Laplacian operator L, and f(u,v) represents the gray value of the image point (u,v) in the effective region.

[0069] ③ The image sharpness evaluation value of the effective region is obtained by the sum of squares of the convolution results:

[0070]

[0071] In this embodiment, based on the result of focusing according to the first preset step size, the peak must be within the movement range of the stepper motor in the previous or previous step.

[0072] Step S200. Adjust the focal length within the second focusing range according to the second preset step size, acquire the second image captured by the camera at each focal length, and determine the optimal focusing position based on the sharpness evaluation value of the effective focusing area in the second image; wherein, the first preset step size is greater than the second preset step size.

[0073] In some embodiments, step S200 includes: moving the stepper motor used to drive the camera lens multiple times within a second focusing range according to a second preset step size to acquire a second image captured by the camera at each focal length; dividing the second image into several sub-regions and determining the effective focusing region in the second image; calculating the sharpness evaluation value of the effective focusing region in the second image; and determining the position of the stepper motor when the sharpness evaluation value of the effective focusing region in the second image is the maximum as the optimal focusing position.

[0074] In this embodiment, the second preset step size can be set according to actual needs. The smaller the second preset step size, the more times the stepper motor moves, resulting in a more ideal final focus position, but the focusing efficiency will also decrease. The sub-region division specifications in the second image are the same as those in the first image.

[0075] The effective focus area in the second image consists of sub-regions corresponding to all effective areas in the last first image.

[0076] In some embodiments, the sharpness evaluation value of the effective focus area is the sum of the sharpness evaluation values ​​of all effective areas included in the effective focus area, and the calculation formula is:

[0077]

[0078] In the formula, f image R represents the sharpness evaluation value of the effective focus area, R represents all effective areas included in the effective focus area, and f(I) represents the sharpness evaluation value of the effective area.

[0079] In some embodiments, the autofocus method further includes: performing grayscale processing on the first image and the second image acquired by the camera before dividing the first image and the second image into several sub-regions.

[0080] For example, the format of the first and second images is changed from 7680(W) x 4320(H) x 3 (Channels) to 7680(W) x 4320(H) x 1 (Channels). In this embodiment, by performing grayscale processing on the first and second images, the number of channels in the first and second images is reduced, thereby improving the running efficiency of subsequent algorithms.

[0081] exist Figure 4 , Figure 5 and Figure 6 In the test dataset, the goal was to focus the camera on a wall, but there were dolls at different distances in the camera's field of view. As the camera moved from out of focus to focus, interference was inevitable. The change curve of the sharpness evaluation value is shown below. Figure 7 As shown.

[0082] according to Figure 7 The sharpness evaluation curves of the two focusing methods show that the focusing method in this embodiment has a single peak, a wide and steep width, and high sensitivity, thus enabling it to more accurately determine the location of the sharpest image. (Using the same computer configuration...) Figure 7 Two focusing methods were tested on 50 images (7680x4320 resolution) with different defocus levels. The total image processing time was recorded, as shown in Table 1.

[0083] Table 1

[0084] Test methods Running time (s) Dual-stage focusing solution 5.2 Invention Solution 4.3

[0085] The focusing method in this embodiment discards invalid areas during the search for the peak of the sharpness curve, thereby reducing the time consumption. This improvement is particularly significant when there are many images or the images are large (especially 8K images).

[0086] A second aspect of this embodiment provides an autofocus device, including an image acquisition module, a first focusing module, and a second focusing module, such as... Figure 8 As shown.

[0087] The first focusing module is used to adjust the focal length within a first focusing range according to a first preset step size, acquire a first image captured by the camera at each focal length, and determine an effective focusing area and a second focusing range based on the first image. For a detailed description of the first focusing module, please refer to the description of step S100.

[0088] The second focusing module is used to adjust the focal length within a second focusing range according to a second preset step size, acquire a second image captured by the camera at each focal length, and determine the optimal focusing position based on the sharpness evaluation value of the effective focusing area in the second image; wherein, the first preset step size is greater than the second preset step size. For a detailed description of the second focusing module, please refer to the description of step S200.

[0089] A third aspect of this embodiment provides an electronic device including a stepper motor, a lens, a processor, a memory, and a computer program stored in the memory and executable on the processor. When executed by the processor, the computer program implements the steps of the autofocus method described in the first aspect of the present invention.

[0090] A fourth aspect of this embodiment provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the autofocus method described in the first aspect of the present invention.

[0091] The above description is merely a preferred embodiment of the present invention. It should be understood that the present invention is not limited to the forms disclosed herein and should not be construed as excluding other embodiments. It can be used in various other combinations, modifications, and environments, and can be altered within the scope of the concept described herein through the above teachings or related technologies or knowledge. Modifications and variations made by those skilled in the art that do not depart from the spirit and scope of the present invention should be within the protection scope of the appended claims.

Claims

1. An autofocus method, characterized in that, include: Adjust the focal length within the first focal range according to the first preset step size, acquire the first image captured by the camera at each focal length, and determine the effective focus area and the second focal range based on the first image. Adjust the focal length within the second focusing range according to the second preset step size, acquire the second image captured by the camera at each focal length, and determine the best focusing position based on the sharpness evaluation value of the effective focusing area in the second image. Wherein, the first preset step size is greater than the second preset step size; The focal length is adjusted within a first focusing range according to a first preset step size, and a first image is acquired by the camera at each focal length. Based on the first image, an effective focus area and a second focusing range are determined, including: The stepper motor used to drive the camera lens moves within the first focusing range according to the first preset step size until the first preset condition is met and the stepper motor stops moving. Acquire the first image captured by the camera at each focal length, and divide the first image into several sub-regions; Set all sub-regions in the first image of the first frame as valid regions; The sub-region in the current first image obtained after each movement of the stepper motor that corresponds to the effective region in the previous frame of the first image is set as the effective region, and then the effective region in the current first image that meets the second preset condition is set as the invalid region. The effective focus area in the second image is formed by combining all the effective areas in the last frame of the first image. The second focusing range is determined as follows: Where position is the position of the stepper motor when it stops moving according to the first preset step length, length is the maximum movement value of the first focusing range, and length / n is the first preset step length; The first preset condition is: after moving the stepper motor, the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is greater than or equal to the number of effective regions in the first image whose sharpness evaluation value is on an upward trend. The second preset condition is as follows: after moving the stepper motor, if the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is greater than the number of effective regions in the first image whose sharpness evaluation value is on an upward trend, then the effective regions in the first image whose sharpness evaluation value is on an upward trend satisfy the second preset condition; if the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is less than the number of effective regions in the first image whose sharpness evaluation value is on an upward trend, then the effective regions in the first image whose sharpness evaluation value is on a downward trend satisfy the second preset condition.

2. The autofocus method according to claim 1, characterized in that, The autofocus method further includes: The first and second images captured by the camera are converted to grayscale.

3. The autofocus method according to claim 1, characterized in that, Adjust the focal length within the second focusing range according to the second preset step size, acquire the second image captured by the camera at each focal length, and determine the optimal focusing position based on the sharpness evaluation value of the effective focus area in the second image, including: The stepper motor used to drive the camera lens is moved multiple times within the second focusing range according to the second preset step size to obtain the second image captured by the camera at each focal length. Calculate the sharpness evaluation value of the effectively focused area in the second image; The position of the stepper motor when the sharpness evaluation value of the effective focus area in the second image is maximized is determined as the optimal focus position.

4. An automatic focusing device, characterized in that, include: The first focusing module is used to adjust the focal length within a first focusing range according to a first preset step size, acquire a first image captured by the camera at each focal length, and determine an effective focusing area and a second focusing range based on the first image. The second focusing module is used to adjust the focal length within the second focusing range according to the second preset step size, acquire the second image captured by the camera at each focal length, and determine the best focusing position based on the sharpness evaluation value of the effective focusing area in the second image. Wherein, the first preset step size is greater than the second preset step size; The first focusing module is specifically used for: The stepper motor used to drive the camera lens moves within the first focusing range according to the first preset step size until the first preset condition is met and the stepper motor stops moving. Acquire the first image captured by the camera at each focal length, and divide the first image into several sub-regions; Set all sub-regions in the first image of the first frame as valid regions; The sub-region in the current first image obtained after each movement of the stepper motor that corresponds to the effective region in the previous frame of the first image is set as the effective region, and then the effective region in the current first image that meets the second preset condition is set as the invalid region. The effective focus area is formed by combining all the effective areas in the last frame of the first image. The second focusing range is determined as follows: Where position is the position of the stepper motor when it stops moving according to the first preset step length, length is the maximum movement value of the first focusing range, and length / n is the first preset step length; The first preset condition is: after moving the stepper motor, the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is greater than or equal to the number of effective regions in the first image whose sharpness evaluation value is on an upward trend. The second preset condition is as follows: after moving the stepper motor, if the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is greater than the number of effective regions in the first image whose sharpness evaluation value is on an upward trend, then the effective regions in the first image whose sharpness evaluation value is on an upward trend satisfy the second preset condition; if the number of effective regions in the first image whose sharpness evaluation value is on a downward trend is less than the number of effective regions in the first image whose sharpness evaluation value is on an upward trend, then the effective regions in the first image whose sharpness evaluation value is on a downward trend satisfy the second preset condition.

5. An electronic device, characterized in that, The electronic device includes a stepper motor, a lens, a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the autofocus method as described in any one of claims 1 to 3.

6. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, which, when executed by a processor, implements the steps of the autofocus method as described in any one of claims 1 to 3.