A camera auto-focusing method, device, electronic equipment and storage medium
By employing a phased camera autofocus method that combines parallel and serial operations, and adaptively adjusting the image acquisition frame rate and lens depth of field, the problem of insufficient focusing speed and accuracy in existing technologies is solved, achieving efficient and precise autofocus.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ZHEJIANG JINGSHENG MECHANICAL & ELECTRICAL CO LTD
- Filing Date
- 2026-03-20
- Publication Date
- 2026-07-10
AI Technical Summary
Among existing camera autofocus methods, the traversal method is time-consuming to calculate and has a large focus position deviation, while the search method is inefficient and prone to getting trapped in local optima, making it difficult to balance accuracy and speed.
The method employs a phased approach of coarse focusing and fine focusing. In the coarse focusing phase, parallel operations are used to quickly lock the approximate focus position, while in the fine focusing phase, serial operations are used to precisely adjust the focus position and finally determine the fine focus position. The parameters are then adaptively adjusted using the camera's frame rate and the lens's depth of field.
It improves the accuracy and efficiency of camera autofocus, avoids focus position deviation, balances speed and accuracy, and has good adaptability and usability.
Smart Images

Figure CN122372836A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of camera automation control technology, and in particular to a camera autofocus method, apparatus, electronic device, and storage medium. Background Technology
[0002] Autofocus technology plays a crucial role in optical imaging systems. Based on differences in focusing principles, it is mainly divided into three categories: range-based, image detection-based, and image processing-based. Compared to the other two autofocus methods, image processing-based methods do not require additional hardware in the focusing system, resulting in lower costs and fewer limitations. The two most critical components of image processing-based autofocus are the sharpness function and the focus search strategy. The sharpness function evaluates the image's sharpness, while the focus search strategy represents the approach to the accurate focus position.
[0003] Existing focusing techniques can be broadly categorized into two types: one is the traversal method, which moves the camera at a set distance to traverse the entire range, calculating sharpness values in real time during the movement, and then selecting the position with the highest value as the focus point. However, this method is time-consuming due to steps such as camera triggering image output, communication to obtain motor position, and sharpness calculation, resulting in a persistent gap between the calculated focus position and the accurate focus position. The other type is the search method, which moves the camera step by step from a set position, taking images and calculating sharpness values. Based on these sharpness values, it determines subsequent movement strategies until the strategy determines that the focus point has been reached. This focusing method avoids the focusing gap problem of the traversal method, but its search efficiency is low, and it may use a locally optimal solution as the final focus position. Summary of the Invention
[0004] This application provides a camera autofocus method, apparatus, electronic device, and storage medium to improve the accuracy and efficiency of camera autofocus.
[0005] To address the aforementioned technical problems, this application provides a camera autofocus method, comprising: acquiring the camera's frame rate, lens depth of field, and initial focus range; based on the frame rate, lens depth of field, and initial focus range, driving the camera to move within the initial focus range and acquiring coarse focus images in real time; performing image frame extraction on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed; and then using the camera position corresponding to the image to be analyzed with the highest sharpness value as the final coarse focus position, thus completing the coarse focus stage; wherein, in the coarse focus stage, image frame extraction and calculation are performed... The sharpness value of the image to be analyzed is determined as a parallel operation. Based on the lens depth of field and the final coarse focus position, the fine focus range and number of steps are determined. The camera is driven to move in a step manner within the fine focus range, and a fine focus image is acquired after each step. After the camera has moved for the specified number of steps, the camera position corresponding to the fine focus image with the largest sharpness value is taken as the fine focus position, thus completing the fine focus stage. In the fine focus stage, driving the camera to move and calculating the sharpness value of the fine focus image are sequential operations. The camera is then driven to move to the fine focus position to complete autofocus.
[0006] The steps of driving the camera to move within the initial focus range and acquire coarse focus images in real time based on the image acquisition frame rate, the lens depth of field, and the initial focus range, and performing image frame extraction on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed, and then using the camera position corresponding to the image to be analyzed with the highest sharpness value as the final coarse focus position to complete the coarse focus stage, include: setting a first coarse focus speed based on the image acquisition frame rate, the lens depth of field, and the initial focus range; driving the camera to move within the initial focus range at the first coarse focus speed and acquiring first coarse focus images in real time; performing image frame extraction on all the first coarse focus images according to the set frame rate to obtain multiple first images to be analyzed, and simultaneously calculating each of the first... The system firstly determines the sharpness value of the first image to be analyzed; it then uses the camera position corresponding to the first image to be analyzed with the highest sharpness value as the initial coarse focus position; based on the image acquisition frame rate, the lens depth of field, and the initial coarse focus position, it determines a second coarse focus range and sets a second coarse focus speed; the speed value of the second coarse focus speed is half that of the first coarse focus speed, and the direction is opposite; it drives the camera to move within the second coarse focus range at the second coarse focus speed and acquires second coarse focus images in real time; it performs image frame extraction on all second coarse focus images according to the set frame rate to obtain multiple second images to be analyzed, and simultaneously calculates the sharpness value of each second image to be analyzed; finally, it uses the camera position corresponding to the second image to be analyzed with the highest sharpness value as the final coarse focus position.
[0007] The speed value of the first coarse focusing speed is set to 4df, where f is the image acquisition frame rate and d is the lens depth of field.
[0008] The second coarse focus range includes a second coarse focus start position and a second coarse focus end position. The distance from the second coarse focus end position to the initial coarse focus position is three times the distance from the second coarse focus start position to the initial coarse focus position. The second coarse focus start position and the second coarse focus end position are located on both sides of the initial coarse focus position.
[0009] The steps of determining the fine focus range and number of steps based on the lens depth of field and the final coarse focus position, driving the camera to move in a step-by-step manner within the fine focus range, and acquiring a fine focus image after each step, and then, after the camera has moved for the required number of steps, taking the camera position corresponding to the fine focus image with the highest sharpness value as the fine focus position to complete the fine focus stage, include: setting the fine focus range and focus step size based on the lens depth of field and the final coarse focus position, and determining the corresponding number of steps; the fine focus range is four times the lens depth of field, and the focus step size is half the lens depth of field; driving the camera to move in a step-by-step manner within the fine focus range, acquiring a fine focus image and calculating the sharpness value of the fine focus image after each focus step, and then driving the camera to move for the next focus step; after the camera has moved for the required number of steps, taking the camera position corresponding to the fine focus image with the highest sharpness value as the fine focus position.
[0010] The step of driving the camera to move in a stepwise manner within the fine focus range, acquiring a fine focus image and calculating the sharpness value of the fine focus image after each focus step, and then driving the camera to move to the next focus step, includes: driving the camera to move by the focus step during a certain step movement; acquiring the fine focus image and calculating the sharpness value of the fine focus image; determining whether the camera has completed the required number of steps; if not, driving the camera to perform the next step movement; if yes, performing the step of taking the camera position corresponding to the fine focus image with the largest sharpness value as the fine focus position.
[0011] The fine focus range includes a fine focus start position and a fine focus end position. The distance from the fine focus end position to the final coarse focus position is three times the distance from the fine focus start position to the final coarse focus position. The fine focus start position and the fine focus end position are located on both sides of the final coarse focus position.
[0012] The image sharpness value is calculated using a sharpness evaluation function, specifically the Brenner function, whose formula is as follows: ; Where D1(f) represents the Brenner function value, used to quantify the sharpness of the image, (x,y) represents the pixel in the image, and f(x,y) represents the gray value of the pixel (x,y) in the image.
[0013] To address the aforementioned technical problems, this application also provides a camera autofocus device, comprising: an acquisition module for acquiring the camera's frame rate, lens depth of field, and initial focus range; and a coarse focus module for driving the camera to move within the initial focus range and acquiring coarse focus images in real time based on the frame rate, lens depth of field, and initial focus range, and performing image frame extraction on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed, then using the camera position corresponding to the image to be analyzed with the highest sharpness value as the final coarse focus position, thus completing the coarse focus stage; wherein, in the coarse focus stage, image... The process of frame extraction and calculating the sharpness value of the image to be analyzed are performed in parallel. The fine focusing module determines the fine focusing range and number of steps based on the lens depth of field and the final coarse focusing position. It drives the camera to move in steps within the fine focusing range, acquiring a fine focusing image after each step. After the camera completes the specified number of steps, the camera position corresponding to the fine focusing image with the highest sharpness value is taken as the fine focusing position, completing the fine focusing stage. In the fine focusing stage, driving the camera to move and calculating the sharpness value of the fine focusing image are performed sequentially. The camera is then driven to the fine focusing position to complete autofocus.
[0014] To address the aforementioned technical problems, this application also provides an electronic device comprising: a memory and a processor coupled to each other, wherein the processor is configured to execute program instructions stored in the memory to implement the camera autofocus method described in any of the preceding claims.
[0015] To address the aforementioned technical problems, this application also provides a computer-readable storage medium storing program instructions that can be executed to implement the camera autofocus method as described in any of the preceding claims.
[0016] The beneficial effects of this application are as follows: Unlike existing technologies, the camera autofocus method of this application first acquires the camera's frame rate, lens depth of field, and initial focus range. Then, based on the frame rate, lens depth of field, and initial focus range, the camera is driven to move within the initial focus range and acquire coarse focus images in real time. Frame extraction is performed on all coarse focus images according to a set frame rate to obtain multiple images to be analyzed. The camera position corresponding to the image with the highest sharpness value is then used as the final coarse focus position, completing the coarse focus stage. In this coarse focus stage, image frame extraction and calculation are performed. The sharpness value of the image to be analyzed is a parallel operation process; then, based on the lens depth of field and the final coarse focus position, the fine focus range and number of steps are determined, and the camera is driven to move in a step manner within the fine focus range, acquiring a fine focus image after each step. After the camera has moved for the required number of steps, the camera position corresponding to the fine focus image with the highest sharpness value is taken as the fine focus position, completing the fine focus stage. In the fine focus stage, driving the camera movement and calculating the sharpness value of the fine focus image are serial operations; thus, the camera can be driven to move to the fine focus position to complete autofocus. This application improves the accuracy and efficiency of camera autofocus by setting up coarse focusing and fine focusing stages. In the coarse focusing stage, a traversal method is used. Since the camera moves within the initial focus range and acquires the coarse focusing image continuously, initial focusing can be completed quickly, limiting the fine focusing position to a smaller search range. Furthermore, image frame extraction and the calculation of the sharpness value of the image to be analyzed are processed in parallel, avoiding focus position deviations introduced by image processing. The method of moving the camera to traverse the entire range also effectively avoids the problem of focusing getting stuck in local optima, balancing focusing accuracy and speed. Simultaneously, in the fine focusing stage, the serial process of single-step camera movement, acquisition of the fine focusing image, and calculation of the sharpness value of the fine focusing image avoids focus position deviations introduced by camera movement, thus ensuring high accuracy in the focus position. In addition, this application takes into account parameters such as the camera's frame rate and lens depth of field during focusing. These hardware parameters can be used to select the focusing speed, range, or step size at each focusing stage, balancing focusing speed and accuracy. Furthermore, the hardware parameters can be adaptively adjusted for different application scenarios. Since the camera's frame rate and lens depth of field are conventional parameters, the setting method is simple and easy to understand, requiring no complicated understanding. This makes the camera autofocus method of this application highly adaptable and usable. Attached Figure Description
[0017] Figure 1 This is a schematic flowchart of an embodiment of the camera autofocus method provided in this application; Figure 2 yes Figure 1 A flowchart illustrating an embodiment of step S12; Figure 3This is a flowchart illustrating the coarse focusing stage of the camera autofocus method in one application scenario of this application. Figure 4 yes Figure 1 A flowchart illustrating an embodiment of step S13; Figure 5 yes Figure 4 A flowchart illustrating an embodiment of step S132; Figure 6 This is a flowchart illustrating the fine focusing stage of the camera autofocus method in one application scenario of this application. Figure 7 This is a schematic diagram of camera image acquisition in one application scenario of this application; Figure 8 This is a schematic diagram of the structure of an embodiment of the camera autofocus device provided in this application; Figure 9 This is a schematic diagram of the structure of an embodiment of the electronic device provided in this application; Figure 10 This is a schematic diagram of an embodiment of the computer-readable storage medium provided in this application. Detailed Implementation
[0018] The embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0019] In the following description, specific details such as particular system architectures, interfaces, and technologies are presented for illustrative purposes rather than for limiting purposes, in order to provide a thorough understanding of this application.
[0020] In this paper, the terms "system" and "network" are often used interchangeably. The term "and / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, or B alone. Additionally, the character " / " generally indicates that the preceding and following related objects have an "or" relationship. Furthermore, "many" in this paper means two or more.
[0021] Please see Figure 1 , Figure 1 This is a schematic flowchart of an embodiment of the camera autofocus method provided in this application. The camera autofocus method in this embodiment includes the following steps: Step S11: Obtain the camera's frame rate, lens depth of field, and initial focus range.
[0022] Understandably, accurate camera parameters are needed to ensure smooth focusing during the initial autofocus process. The frame rate refers to the number of images the camera can capture per second, directly determining the speed of image data acquisition during focusing. Lens depth of field refers to the range of objects the camera lens can clearly image; a shallower depth of field requires higher focusing accuracy, while a deeper depth of field provides greater tolerance for focusing errors. The initial focus range is a pre-set approximate focus interval based on the current shooting scene. This can be combined with scene recognition technology, such as identifying whether the shooting area is for microscopic observation or macroscopic measurement, and whether the subject is a close-up portrait or a distant landscape, to intelligently set the focus range.
[0023] Step S12: Based on the image acquisition frame rate, the lens depth of field, and the initial focus range, drive the camera to move within the initial focus range and acquire coarse focus images in real time. Then, perform image frame extraction on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed. Finally, take the camera position corresponding to the image with the highest sharpness value as the final coarse focus position, completing the coarse focus stage. In the coarse focus stage, image frame extraction and the calculation of the sharpness value of the image to be analyzed are performed in parallel.
[0024] The coarse focusing stage requires quickly locking the approximate focus direction within a short time to narrow down the focus area for the subsequent fine focusing stage. During coarse focusing, the camera moves within the initial focus range based on the acquired frame rate, lens depth of field, and initial focus range, acquiring coarse focus images in real time. To reduce computational load while maintaining focusing speed, this application employs image frame extraction. The frame rate for image extraction is a pre-set frequency based on the acquired frame rate and focusing speed requirements. For example, when the acquired frame rate is 60fps, the frame rate can be set to 10fps, meaning that one image is extracted from every six coarse focus images acquired as the image to be analyzed. This ensures sufficient image data for sharpness analysis while avoiding wasted computational resources due to processing too many images.
[0025] In the coarse focusing stage, image frame extraction and the calculation of sharpness values for the images to be analyzed are performed in parallel. Traditional serial processing requires extracting frames from all images first, then calculating the sharpness value for each, which prolongs the focusing time. Parallel operation, however, immediately starts calculating the sharpness value of an image after extraction, while simultaneously extracting frames from the next image. This parallel processing mode fully utilizes the camera's computing resources, significantly reducing the time spent in the coarse focusing stage. After the camera completes its movement and image acquisition within the initial focus range, the sharpness values of all images to be analyzed are compared, and the camera position corresponding to the image with the highest sharpness value is selected as the final coarse focusing position. Thus, the coarse focusing stage is completed, and the camera successfully locks a relatively accurate focus range, facilitating the implementation of the fine focusing stage.
[0026] Step S13: Based on the lens depth of field and the final coarse focus position, determine the fine focus range and the number of steps. Drive the camera to move within the fine focus range in a step-by-step manner, acquiring a fine focus image after each step. Then, after the camera has moved the required number of steps, take the camera position corresponding to the fine focus image with the highest sharpness value as the fine focus position, completing the fine focus stage. In the fine focus stage, driving the camera movement and calculating the sharpness value of the fine focus image are sequential operations.
[0027] The fine focusing stage requires more precise focus adjustments within the approximate range locked in the coarse focusing stage to achieve the highest focusing accuracy. In the fine focusing stage, the fine focusing range and number of steps need to be determined based on the lens depth of field and the final coarse focusing position. Lens depth of field is a key factor in determining the fine focusing range. When the lens depth of field is shallow, the fine focusing range needs to be correspondingly reduced to ensure accurate capture of the subject's sharp image position; when the lens depth of field is deep, the fine focusing range can be appropriately expanded, thereby improving focusing efficiency while maintaining accuracy. The number of steps is determined based on the fine focusing range and the camera's step size. The camera's step size refers to the distance the lens moves in one step, reflecting the lens's focusing precision. Assuming the fine focusing range is R and the lens's minimum step size is D, the number of steps N can be calculated using the formula N = R / D. For example, if the fine focusing range is 1 cm and the lens's minimum step size is 0.1 mm, then the number of steps is 100.
[0028] After determining the fine focus range and the number of steps, the camera moves within the fine focus range in steps. With each step, the camera acquires a fine focus image. Unlike the parallel operation of the coarse focus stage, the fine focus stage uses a serial operation. That is, after moving the camera one step and acquiring a fine focus image, the sharpness value of that image is calculated before proceeding to the next step of movement and acquisition. This serial processing method ensures focusing accuracy at each step because in the fine focus stage, even the slightest lens movement can significantly affect image sharpness. Only by accurately calculating the sharpness value of the current fine focus image can a reliable basis be provided for the next movement direction.
[0029] After the camera completes all the steps of movement and image acquisition, the sharpness values of all the fine-focused images are compared, and the camera position corresponding to the image with the highest sharpness value is selected as the fine-focus position. Thus, the fine-focusing stage is completed, and the camera achieves precise focus on the subject.
[0030] Step S14: Drive the camera to the fine focus position to complete autofocus.
[0031] After determining the fine focus position, the camera is driven to move the lens to that position, completing the entire autofocus process.
[0032] It is worth noting that in some dynamic shooting scenarios, the subject may move during the focusing process. To address this, some embodiments of this application may introduce a continuous focusing mechanism, that is, when the camera detects movement of the subject, it will re-trigger the focusing process, repeating the above steps S11 to S14, updating the focus position in real time, thereby ensuring that the subject is always in focus.
[0033] The above scheme improves the accuracy and efficiency of camera autofocus by setting up coarse focusing and fine focusing stages. In the coarse focusing stage, a traversal method is used. Since the camera moves within the initial focus range and acquires the coarse focusing image continuously, initial focusing can be completed quickly, limiting the fine focusing position to a smaller search range. Image frame extraction and the calculation of the sharpness value of the image to be analyzed are processed in parallel, avoiding focus position deviations introduced by image processing. Furthermore, moving the camera to traverse the entire range effectively avoids the problem of focusing getting stuck in local optima, balancing focusing accuracy and speed. Meanwhile, in the fine focusing stage, the serial process of single-step camera movement, acquiring the fine focusing image, and calculating the sharpness value of the fine focusing image avoids focus position deviations introduced by camera movement, thus ensuring high accuracy in the focus position. In addition, this application takes into account parameters such as the camera's frame rate and lens depth of field during focusing. These hardware parameters can be used to select the focusing speed, range, or step size at each focusing stage, balancing focusing speed and accuracy. Furthermore, the hardware parameters can be adaptively adjusted for different application scenarios. Since the camera's frame rate and lens depth of field are conventional parameters, the setting method is simple and easy to understand, requiring no complicated understanding. This makes the camera autofocus method of this application highly adaptable and usable.
[0034] Please combine Figure 2 , Figure 2 yes Figure 1 A flowchart illustrating one embodiment of step S12. In one embodiment, step S12 specifically includes: Step S121: Based on the image acquisition frame rate, the lens depth of field, and the initial focus range, set the first coarse focus speed.
[0035] Before the coarse focusing stage begins, the camera's frame rate f, lens depth of field d, and initial focus range R can be obtained. end1 And determine the initial coarse focus starting position P of the camera. start1 Based on these values, a corresponding first coarse focusing speed can be set. That is, by adjusting parameters such as the first coarse focusing speed, different hardware conditions can be adapted to balance focusing speed and accuracy. Taking the field of microscopic observation as an example, since the magnification of the objective lens is high, the depth of field is usually very small. Therefore, a moderate or low first coarse focusing speed can be set to ensure high-precision focusing. In the field of macroscopic measurement, since the depth of field of the lens is large, a higher first coarse focusing speed can be set to ensure focusing speed.
[0036] Understandably, the initial focus range R end1 With direction, when R end1 When the value of R is positive, the drive motor drives the camera to move forward. end1When the value is negative, the drive motor drives the camera to move in the negative direction, and the range of uniform motion is [P]. start1 P start1 +R end1 ].
[0037] In one embodiment, the speed value V of the first coarse focusing speed is set. focus1 The value is 4df, where f is the image capture frame rate and d is the lens depth of field.
[0038] Step S122: Drive the camera to move within the initial focus range at the first coarse focus speed and acquire the first coarse focus image in real time.
[0039] Step S123: Perform image frame extraction on all the first coarse focus images according to the set frame rate to obtain multiple first images to be analyzed, and simultaneously calculate the sharpness value of each of the first images to be analyzed.
[0040] Specifically, after initializing the camera, the drive motor moves at a constant speed, causing the camera to move within the initial focus range at a first coarse focusing speed. During the motor's constant-speed movement, the camera acquires the first coarse-focused image in real time and performs image frame extraction at a set frame rate, obtaining multiple first images to be analyzed and forming an image sequence. After each frame extraction, the real-time position of the motor is read, thus forming a motor position sequence. Sharpness values are calculated for all the first images to be analyzed in the image sequence, and this sharpness value calculation is a parallel operation that does not affect the frame extraction rate.
[0041] In one embodiment, the image sharpness value is calculated using a sharpness evaluation function, wherein the sharpness evaluation function is the Brenner function, and the formula for the Brenner function is: ; Where D1(f) represents the Brenner function value, used to quantify the sharpness of the image, (x,y) represents the pixel in the image, and f(x,y) represents the gray value of the pixel (x,y) in the image.
[0042] Specifically, the sharpness evaluation function used in this embodiment is the Brenner function. The Brenner function first performs convolution operations on each channel of the image sequence, which can effectively extract the gradient information of pixels in the image and highlight the edge and detail features of the image. After completing the convolution operation, the function calculates the variance of the output result. Variance, as a statistic measuring the degree of data dispersion, can intuitively reflect the variation range of image pixel values. The larger the output variance, the more significant the differences between pixels in the image, and the richer the edges and details, that is, the higher the image sharpness; conversely, the smaller the variance, the more blurry the image is and the less detail information is lost. In this way, a quantified sharpness value can be quickly and accurately assigned to each first image to be analyzed, thereby realizing the evaluation of multiple first images to be analyzed.
[0043] Step S124: Take the camera position corresponding to the first image to be analyzed with the largest sharpness value as the initial coarse focus position.
[0044] Specifically, after calculating the sharpness values of all images, a sharpness value sequence can be obtained. The length of this sequence is the same as the length of the motor position sequence. Therefore, the motor position corresponding to the maximum sharpness value can be taken as the initial coarse focus position P. focus1 This completes the camera's first coarse focusing.
[0045] Step S125: Based on the image acquisition frame rate, the lens depth of field, and the initial coarse focus position, determine the second coarse focus range and set the second coarse focus speed; the speed value of the second coarse focus speed is half of the first coarse focus speed, and the direction is opposite.
[0046] Step S126: Drive the camera to move within the second coarse focus range at the second coarse focus speed and acquire the second coarse focus image in real time.
[0047] Step S127: Perform image frame extraction on all the second coarse focus images according to the set frame rate to obtain multiple second images to be analyzed, and simultaneously calculate the sharpness value of each second image to be analyzed.
[0048] Step S128: The camera position corresponding to the second image to be analyzed with the largest sharpness value is taken as the final coarse focus position.
[0049] The coarse focusing stage in this embodiment includes a first coarse focusing and a second coarse focusing. During the second coarse focusing of the camera, since the camera's frame rate f, lens depth of field d, and initial coarse focusing position P are known... focus1 This allows us to determine the camera's second coarse focus range [P]. focus1 +R start2 P focus1 +R end2The second coarse focusing speed is set based on these values. That is, parameters such as the second coarse focusing speed can be adjusted to adapt to different hardware conditions, balancing focusing speed and accuracy. For example, in the field of microscopic observation, because the objective lens has a high magnification, the depth of field is usually very small, so a moderate or low second coarse focusing speed can be set to ensure high-precision focusing. In the field of macroscopic measurement, because the lens has a large depth of field, a higher second coarse focusing speed can be set to ensure focusing speed.
[0050] In one embodiment, the second coarse focus range includes a second coarse focus start position and a second coarse focus end position. The distance from the second coarse focus end position to the initial coarse focus position is three times the distance from the second coarse focus start position to the initial coarse focus position. The second coarse focus start position and the second coarse focus end position are located on both sides of the initial coarse focus position, respectively.
[0051] In one embodiment, the range of the second coarse focus is thus calculated, and the speed value of the second coarse focus speed is set to half of the first coarse focus speed, that is, when the speed value V of the first coarse focus speed... focus1 The speed value V of the second coarse focusing speed at 4df focus2 =2df, in the opposite direction. Therefore, after completing the camera's first coarse focusing, when performing the camera's second coarse focusing, according to the second coarse focusing range [P]... focus1 +R start2 P focus1 +R end2 The drive motor starts from the initial coarse focusing position P. focus1 The camera is moved at a constant speed in the opposite direction to the first coarse focus for a second coarse focus. The process for the second coarse focus is the same as the first, obtaining a sequence of sharpness values and a sequence of motor positions. The motor position corresponding to the maximum sharpness value is taken as the final coarse focus position P. focus2 .
[0052] Please combine Figure 3 , Figure 3This is a flowchart illustrating the coarse focusing stage of the camera autofocus method in one application scenario of this application. The coarse focusing stage includes a first coarse focusing and a second coarse focusing. Both the first and second coarse focusing follow this process: After the motor moves the camera to the focusing starting position, the camera begins real-time image acquisition. Simultaneously, the motor moves the camera at a set speed until it reaches its designated position. Parallel to the camera's constant-speed movement, the real-time captured images are extracted at a set frame rate and added to the image sequence. Simultaneously, the motor's current position is read and added to the position sequence. Then, it is determined whether the motor has reached its position. If not, image extraction continues; if so, it stops. Parallel to image extraction, an image is taken from the image sequence, its sharpness value is calculated and added to the sequence, and then it is determined whether the motor has reached its position. If not, the next image in the image sequence is taken and its sharpness value is calculated; if so, it is further determined whether the image sequence is empty. If not empty, continue to take the next image in the image sequence and calculate the sharpness value. If empty, it means that the sharpness value calculation of all extracted frames has been completed. Therefore, image processing can be stopped and real-time image acquisition by the camera can be stopped. Then, the coarse focus position is calculated based on the sharpness value sequence and the motor position sequence to complete the coarse focus process and further calculate the parameters for the next focusing step.
[0053] Please combine Figure 4 , Figure 4 yes Figure 1 A flowchart illustrating one embodiment of step S13. In one embodiment, step S13 specifically includes: Step S131: Based on the lens depth of field and the final coarse focus position, set the fine focus range and focus step size, and determine the corresponding number of steps; the fine focus range is four times the lens depth of field, and the focus step size is half the lens depth of field.
[0054] During the fine focusing stage, since the camera's lens depth of field d and the final coarse focusing position P are known... focus2 This allows us to determine the camera's fine focus range R. fine The system calculates the focusing step size and the corresponding number of steps based on these values. In other words, by adjusting parameters such as the focusing step size, it can adapt to different hardware conditions, balancing focusing speed and accuracy. For example, in the field of microscopic observation, because the objective lens has a high magnification, the depth of field is usually very small, so a smaller focusing step size can be set to ensure high-precision focusing. In the field of macroscopic measurement, because the lens has a large depth of field, a larger focusing step size can be set to ensure focusing speed.
[0055] Since the motor movement and camera image acquisition are sequential processes during the fine focusing stage, the focusing speed is slower than that of coarse focusing. In one embodiment, to ensure the overall focusing speed, a fine focusing range R can be set.fine The depth of field is four times that of the lens, d, or 4d. The focus step size for fine focusing is half of the depth of field, d, or focus step size D. step =1 / 2d.
[0056] In one embodiment, the fine focus range includes a fine focus start position and a fine focus end position, the distance from the fine focus end position to the final coarse focus position is 3 times the distance from the fine focus start position to the final coarse focus position, and the fine focus start position and the fine focus end position are located on both sides of the final coarse focus position.
[0057] This application considers parameters such as the camera's frame rate and lens depth of field during focusing. These hardware parameters allow for selection of focusing speed, range, or step size at each focusing stage, achieving a balance between focusing speed and accuracy. Specifically, in the coarse focusing stage, motor movement and image frame extraction are parallel processes, with an interval of 1 / f between image frame extractions. Assuming the second coarse focusing speed of the motor is V, the distance D that the motor has moved between two image frame extractions is... grab =V*1 / f; Additionally, there are uncontrollable factors such as communication time when acquiring the motor position, therefore it is necessary to introduce the resolution value and the position deviation D of the motor at the time of triggering image acquisition. other Therefore, there is always a certain deviation between the calculated sharpness value and the position of the motor when triggering image acquisition. offset And D offset =D grab +D other Given that the depth of field of the lens is d, what is the difference between the focus position obtained by coarse focusing and the ideal focus position P? theore The maximum deviation between them is D offsetmax D offsetmax =D grab +D other -d, and the focus position corresponding to the sharpness function value is always delayed by the accurate motor position. This ensures that the final coarse focus position P obtained from the second coarse focus is... focus2 The fine focus range R determined by the lens depth of field d fine It can accurately cover the correct fine focus position, taking the distance from the fine focus endpoint to the final coarse focus position P. focus2 The distance is from the fine focus starting position to the final coarse focus position P. focus2 Three times the distance, while the set fine focus range R fine =4d, then the fine focus range R fine For [P] focus2 +R start P focus2 +R end ], where |R start |=d,|R end|=3d, taking the final fine focus position as the origin, R start With initial focus range R end1 The positive and negative signs of R are opposite. end With R start Positive and negative values are opposite. Therefore, to ensure accurate focusing, the maximum deviation of the focus position should not exceed d, hence D offsetmax =D grab +D other -d≤d, take D other If it is approximately 0, then D grab =V*1 / f≤2d, therefore V≤2df; to balance focusing speed, we take V=2df, therefore the motor speed V during the second coarse focusing is... focus2 It is 2df. Additionally, the first coarse focusing speed during the first coarse focusing is taken as twice the second coarse focusing speed during the second coarse focusing, i.e., V focus1 =4df, then the initial coarse focus position P obtained by the first coarse focus is focus1 With the ideal focus position P theore The theoretical maximum deviation D offset =D grab -d, i.e., V focus1 *1 / f=4df*1 / fd=3d, similarly, take the distance from the second coarse focus endpoint to the initial coarse focus position P. focus1 The distance is from the second coarse focus starting position to the initial coarse focus position P. focus1 If the distance is 3 times the distance, then the second coarse focus range is [P]. focus1 +R start2 P focus1 +R end2 ], where |R start2 |=3d,|R end2 |=9d, taking the final fine focus position as the origin, R start2 With initial focus range R end1 R has the same sign. end2 With R start2 Positive and negative are opposites.
[0058] Step S132: Drive the camera to move in a step manner within the fine focus range. After each focus step, acquire a fine focus image and calculate the sharpness value of the fine focus image. Then drive the camera to move to the next focus step.
[0059] Furthermore, please combine Figure 5 , Figure 5 yes Figure 4 A flowchart illustrating one embodiment of step S132. In one embodiment, step S132 specifically includes: Step S1321: During a certain step movement of the camera, drive the camera to move by the focusing step length.
[0060] Step S1322: Acquire the finely focused image and calculate the sharpness value of the finely focused image.
[0061] Step S1323: Determine whether the camera has completed the required number of steps. If not, proceed to step S1324; if yes, proceed to step S1325.
[0062] Step S1324: Drive the camera to perform the next step movement process.
[0063] Step S1325: The camera position corresponding to the finely focused image with the largest sharpness value is taken as the finely focused position.
[0064] Step S133: After the camera has moved the specified number of steps, the camera position corresponding to the fine-focused image with the highest sharpness value is taken as the fine-focused position.
[0065] During the fine focusing stage, image acquisition is only performed after the motor's single-step movement is complete. Therefore, there is no discrepancy between the calculated sharpness value and the motor position. After obtaining the sharpness value sequence and the motor position sequence, the motor position corresponding to the maximum sharpness value is the accurate fine focusing position P. focus Therefore, the subsequent drive motor moves the camera to the fine focus position P. focus That is, autofocus is completed.
[0066] Please combine Figure 6 , Figure 6 This is a flowchart illustrating the fine-focusing stage of the camera autofocus method in one application scenario of this application. After the fine-focusing stage begins, the motor moves the camera to the focusing start position. The camera then acquires an image from the focusing start position, calculates and adds the image's sharpness value to the sequence, reads the motor's current position, and adds it to the position sequence. Next, the motor moves a set step size to the next position, and the camera acquires an image from that position, calculates and adds the image's sharpness value to the sequence, reads the motor's current position, and adds it to the position sequence. Then, it is determined whether the fine-focusing steps have been completed. If not, the motor continues to move a set step size to the next position, acquires an image, and calculates the image sharpness value. If yes, it means that image acquisition and sharpness value calculation have been completed for all positions within the fine-focusing range. Therefore, the fine-focusing position can be calculated based on the sharpness value sequence and the motor position sequence, completing the fine-focusing process. Afterward, the motor can be driven to move to that fine-focusing position to achieve automatic focusing of the camera.
[0067] To further illustrate the beneficial effects of this application, the following explanation is based on specific experiments.
[0068] The experiment compared the camera autofocus method provided in this application with two commonly used autofocus methods: the traversal method and the search method. Specifically, a high-magnification microscope objective with a known depth of field of 2µm and a matching industrial camera with a frame rate of 30fps was used. Before testing, the object to be tested was fixed, and the motor position was manually adjusted until the object surface was in focus. The motor position was recorded (5.274mm in this experiment). Each focusing method was tested three times. After setting the motor position to a uniform position (5.050mm in this experiment), the autofocus process began. To ensure the reliability of the test results, the range and motor speed of the traversal method were the same as the initial focusing range (0.5mm) and the first coarse focusing speed calculated by the method in this application, respectively. After focusing, the focusing position and process time were recorded. The images acquired during the test are shown below. Figure 7 As shown.
[0069] Table 1 Comparison of Three Autofocus Methods The test results are shown in Table 1: (1) In terms of focusing accuracy, the focusing difference was considered. Since the depth of field of the lens is 2um (i.e. ±1um), when the actual focusing position deviates from the focusing recorded value (i.e. 5.274mm) within 1um, the focusing difference is 0. It can be seen that the accuracy of the traversal method is the lowest. After the focusing process is completed, the system is still in a certain out-of-focus state and the image is blurry. The accuracy of the search method and the method of this application are similar, and the focusing error is within 1um. (2) In terms of focusing time, the traversal method has the shortest time, with an average time of 2.5s. The method of this application takes a little longer, with an average time of 3.7s. The search method takes the longest time.
[0070] The experimental results above demonstrate that the method presented in this application combines the focusing speed advantage of the traversal method with the focusing accuracy advantage of the search method, achieving an effective balance between speed and accuracy. Furthermore, the camera frame rate and lens depth of field settings required in this application are standard parameters that only need to be set once in specific scenarios, and the setting methods are straightforward and require no complex understanding.
[0071] Please see Figure 8 , Figure 8This is a schematic diagram of an embodiment of the camera autofocus device provided in this application. The camera autofocus device 80 of this embodiment includes an acquisition module 800, a coarse focus module 802, and a fine focus module 804 connected to each other. The acquisition module 800 is used to acquire the camera's frame rate, lens depth of field, and initial focus range. The coarse focus module 802 is used to drive the camera to move within the initial focus range and acquire coarse focus images in real time based on the frame rate, lens depth of field, and initial focus range. It also performs image frame extraction on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed. Then, the camera position corresponding to the image to be analyzed with the highest sharpness value is taken as the final coarse focus position, completing the coarse focus stage. In the coarse focus stage, image frame extraction and the calculation of the sharpness value of the image to be analyzed are performed in parallel. The process involves the following steps: The fine focus module 804 determines the fine focus range and number of steps based on the lens depth of field and the final coarse focus position. It drives the camera to move in steps within the fine focus range, acquiring a fine focus image after each step. After the camera completes the specified number of steps, the camera position corresponding to the fine focus image with the highest sharpness value is taken as the fine focus position, thus completing the fine focus stage. In this fine focus stage, driving the camera to move and calculating the sharpness value of the fine focus image are sequential operations. The camera is then driven to the fine focus position to complete autofocus.
[0072] In one embodiment, the coarse focusing module 802 performs the following steps based on the image acquisition frame rate, the lens depth of field, and the initial focus range: driving the camera to move within the initial focus range and acquiring coarse focusing images in real time; performing image frame extraction on all the coarse focusing images according to a set frame rate to obtain multiple images to be analyzed; and then using the camera position corresponding to the image to be analyzed with the highest sharpness value as the final coarse focusing position to complete the coarse focusing stage. The steps include: setting a first coarse focusing speed based on the image acquisition frame rate, the lens depth of field, and the initial focus range; driving the camera to move within the initial focus range at the first coarse focusing speed and acquiring first coarse focusing images in real time; performing image frame extraction on all the first coarse focusing images according to the set frame rate to obtain multiple first images to be analyzed, and simultaneously... Calculate the sharpness value of each of the first images to be analyzed; take the camera position corresponding to the first image to be analyzed with the largest sharpness value as the initial coarse focus position; determine the second coarse focus range based on the image acquisition frame rate, the lens depth of field, and the initial coarse focus position, and set the second coarse focus speed; the speed value of the second coarse focus speed is half of the first coarse focus speed, and the direction is opposite; drive the camera to move within the second coarse focus range at the second coarse focus speed and acquire second coarse focus images in real time; perform image frame extraction on all the second coarse focus images according to the set frame rate to obtain multiple second images to be analyzed, and simultaneously calculate the sharpness value of each of the second images to be analyzed; take the camera position corresponding to the second image to be analyzed with the largest sharpness value as the final coarse focus position.
[0073] In one embodiment, the fine focusing module 804 performs the following steps: determining the fine focusing range and the number of steps based on the lens depth of field and the final coarse focusing position; driving the camera to move in steps within the fine focusing range; and acquiring a fine focusing image after each step. Then, after the camera has moved the required number of steps, the camera position corresponding to the fine focusing image with the highest sharpness value is taken as the fine focusing position, completing the fine focusing stage. This includes: setting the fine focusing range and the final coarse focusing position based on the lens depth of field and the final coarse focusing position. The focus step size is determined, and the corresponding number of steps is determined; the fine focus range is four times the depth of field of the lens, and the focus step size is half the depth of field of the lens; the camera is driven to move in a step manner within the fine focus range, and after each focus step size movement, a fine focus image is acquired and the sharpness value of the fine focus image is calculated, and then the camera is driven to move to the next focus step size; after the camera has moved the number of steps, the camera position corresponding to the fine focus image with the largest sharpness value is taken as the fine focus position.
[0074] In one embodiment, the fine focus module 804 executes the steps of driving the camera to move in a stepwise manner within the fine focus range, acquiring a fine focus image and calculating the sharpness value of the fine focus image after each focus step, and then driving the camera to move to the next focus step. Specifically, this may include: driving the camera to move by the focus step during a certain step movement; acquiring the fine focus image and calculating the sharpness value of the fine focus image; determining whether the camera has completed the number of steps; if not, driving the camera to execute the next step movement process; if yes, then executing the above-mentioned step of taking the camera position corresponding to the fine focus image with the largest sharpness value as the fine focus position.
[0075] Please see Figure 9 , Figure 9 This is a schematic diagram of an embodiment of the electronic device provided in this application. The electronic device 90 in this embodiment includes a processor 902 and a memory 901 interconnected; the memory 901 stores program instructions, and the processor 902 executes the program instructions stored in the memory 901 to implement the steps of any of the above-described camera autofocus method embodiments. In a specific implementation scenario, the electronic device 90 may include, but is not limited to, a microcomputer or a server.
[0076] Specifically, processor 902 controls itself and memory 901 to implement the steps of any of the above-described camera autofocus method embodiments. Processor 902 can also be referred to as a CPU (Central Processing Unit). Processor 902 may be an integrated circuit chip with signal processing capabilities. Processor 902 can also be a general-purpose processor, digital signal processor (DSP), application-specific integrated circuit (ASIC), field-programmable gate array (FPGA), or other programmable logic device, discrete gate or transistor logic device, or discrete hardware component. A general-purpose processor can be a microprocessor or any conventional processor. Furthermore, processor 902 can be implemented using integrated circuit chips.
[0077] Please see Figure 10 , Figure 10 This is a schematic diagram of an embodiment of the computer-readable storage medium provided in this application. The computer-readable storage medium 100 of this application stores program instructions 1000 thereon, which, when executed by a processor, implement the steps of any of the above-described embodiments of the camera autofocus method.
[0078] The computer-readable storage medium 100 can be a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, or a medium that can store program instructions 1000. Alternatively, it can be a server that stores the program instructions 1000. The server can send the stored program instructions 1000 to other devices for execution, or it can execute the stored program instructions 1000 itself.
[0079] In the several embodiments provided in this application, it should be understood that the disclosed methods, devices, and apparatuses can be implemented in other ways. For example, the device and apparatus embodiments described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.
[0080] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0081] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0082] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods of various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0083] If the technical solution of this application involves personal information, the product using this technical solution has clearly informed the user of the personal information processing rules and obtained the user's voluntary consent before processing the personal information. If the technical solution of this application involves sensitive personal information, the product using this technical solution has obtained the user's separate consent before processing the sensitive personal information, and also meets the requirement of "express consent". For example, at personal information collection devices such as cameras, clear and prominent signs are set up to inform users that they have entered the scope of personal information collection and that personal information will be collected. If an individual voluntarily enters the collection scope, it is deemed that they have agreed to the collection of their personal information; or on the personal information processing device, with clear signs / information informing users of the personal information processing rules, authorization is obtained from the individual through pop-up information or by asking the individual to upload their personal information; wherein, the personal information processing rules may include information such as the personal information processor, the purpose of personal information processing, the processing method, and the types of personal information processed.
Claims
1. A method for automatic focusing of a camera, characterized in that, The camera autofocus method includes: Obtain the camera's frame rate, lens depth of field, and initial focus range; Based on the image acquisition frame rate, the lens depth of field, and the initial focus range, the camera is driven to move within the initial focus range and acquire coarse focus images in real time. Then, image frame extraction is performed on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed. Finally, the camera position corresponding to the image to be analyzed with the highest sharpness value is taken as the final coarse focus position, completing the coarse focus stage. In this coarse focus stage, image frame extraction and the calculation of the sharpness value of the image to be analyzed are performed in parallel. Based on the lens depth of field and the final coarse focus position, the fine focus range and the number of steps are determined. The camera is driven to move in steps within the fine focus range, and a fine focus image is acquired after each step. Then, after the camera has moved the required number of steps, the camera position corresponding to the fine focus image with the highest sharpness value is taken as the fine focus position, thus completing the fine focus stage. In the fine focus stage, driving the camera to move and calculating the sharpness value of the fine focus image are sequential operations. Drive the camera to the fine focus position to complete autofocus.
2. The camera autofocus method according to claim 1, characterized in that, The steps of driving the camera to move within the initial focus range and acquire coarse focus images in real time based on the image acquisition frame rate, the lens depth of field, and the initial focus range, and then performing image frame extraction on all the coarse focus images according to a set frame rate to obtain multiple images to be analyzed, and then using the camera position corresponding to the image to be analyzed with the highest sharpness value as the final coarse focus position to complete the coarse focus stage, include: Based on the image acquisition frame rate, the lens depth of field, and the initial focus range, a first coarse focus speed is set. Drive the camera to move within the initial focus range at the first coarse focus speed and acquire the first coarse focus image in real time; According to the set frame rate, all the first coarse focus images are subjected to image frame extraction to obtain multiple first images to be analyzed, and the sharpness value of each first image to be analyzed is calculated simultaneously. The camera position corresponding to the first image to be analyzed with the highest sharpness value is used as the initial coarse focus position. Based on the image acquisition frame rate, the lens depth of field, and the initial coarse focus position, a second coarse focus range is determined, and a second coarse focus speed is set; the speed value of the second coarse focus speed is half of the first coarse focus speed, and the direction is opposite. Drive the camera to move within the second coarse focus range at the second coarse focus speed and acquire the second coarse focus image in real time; According to the set frame rate, all the second coarse focus images are subjected to image frame extraction to obtain multiple second images to be analyzed, and the sharpness value of each second image to be analyzed is calculated simultaneously. The camera position corresponding to the second image to be analyzed with the highest sharpness value is taken as the final coarse focus position.
3. The camera autofocus method according to claim 2, characterized in that, The speed value of the first coarse focusing speed is set to 4df, where f is the image acquisition frame rate and d is the lens depth of field.
4. The camera autofocus method according to claim 2, characterized in that, The second coarse focus range includes a second coarse focus start position and a second coarse focus end position. The distance from the second coarse focus end position to the initial coarse focus position is three times the distance from the second coarse focus start position to the initial coarse focus position. The second coarse focus start position and the second coarse focus end position are located on both sides of the initial coarse focus position.
5. The camera autofocus method according to claim 1, characterized in that, The steps of determining the fine focus range and number of steps based on the lens depth of field and the final coarse focus position, driving the camera to move in steps within the fine focus range, and acquiring a fine focus image after each step, and then, after the camera has moved the required number of steps, taking the camera position corresponding to the fine focus image with the highest sharpness value as the fine focus position, thus completing the fine focus stage, include: Based on the lens depth of field and the final coarse focus position, the fine focus range and focus step size are set, and the corresponding number of steps is determined; the fine focus range is four times the lens depth of field, and the focus step size is half the lens depth of field. The camera is driven to move in steps within the fine focus range. After each focus step, a fine focus image is acquired and the sharpness value of the fine focus image is calculated. Then, the camera is driven to move to the next focus step. After the camera has moved the specified number of steps, the camera position corresponding to the finely focused image with the highest sharpness value is taken as the finely focused position.
6. The camera autofocus method according to claim 5, characterized in that, The step of driving the camera to move in steps within the fine focus range, acquiring a fine focus image and calculating the sharpness value of the fine focus image after each focus step, and then driving the camera to move to the next focus step, includes: During a certain step movement of the camera, the camera is driven to move by the focusing step size; Acquire the finely focused image and calculate the sharpness value of the finely focused image; Determine whether the camera has completed the specified number of steps; If not, then drive the camera to perform the next step movement process; If so, then perform the above step of taking the camera position corresponding to the finely focused image with the highest sharpness value as the finely focused position.
7. The camera autofocus method according to claim 5, characterized in that, The fine focus range includes a fine focus start position and a fine focus end position. The distance from the fine focus end position to the final coarse focus position is three times the distance from the fine focus start position to the final coarse focus position. The fine focus start position and the fine focus end position are located on both sides of the final coarse focus position.
8. The camera autofocus method according to claim 1, characterized in that, The image sharpness value is calculated using a sharpness evaluation function, specifically the Brenner function, whose formula is as follows: ; Where D1(f) represents the Brenner function value, used to quantify the sharpness of the image, (x,y) represents the pixel in the image, and f(x,y) represents the gray value of the pixel (x,y) in the image.
9. A camera autofocus device, characterized in that, The camera autofocus device includes: The acquisition module is used to acquire the camera's frame rate, lens depth of field, and initial focus range. A coarse focusing module is used to drive the camera to move within the initial focusing range and acquire coarse focusing images in real time based on the image acquisition frame rate, the lens depth of field, and the initial focusing range. It then performs image frame extraction on all the coarse focusing images according to a set frame rate to obtain multiple images to be analyzed. Finally, the camera position corresponding to the image to be analyzed with the highest sharpness value is used as the final coarse focusing position, completing the coarse focusing stage. In the coarse focusing stage, image frame extraction and the calculation of the sharpness value of the image to be analyzed are performed in parallel. A fine focus module is used to determine the fine focus range and number of steps based on the lens depth of field and the final coarse focus position. The module drives the camera to move in steps within the fine focus range, acquiring a fine focus image after each step. After the camera completes the specified number of steps, the camera position corresponding to the fine focus image with the highest sharpness value is taken as the fine focus position, completing the fine focus stage. In the fine focus stage, driving the camera to move and calculating the sharpness value of the fine focus image are sequential operations. The module also drives the camera to the fine focus position to complete autofocus.
10. An electronic device, characterized in that, The electronic device includes a memory and a processor coupled to each other, the processor being configured to execute program instructions stored in the memory to implement the camera autofocus method as described in any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program instructions that can be executed to implement the camera autofocus method as described in any one of claims 1 to 8.