An auto-focusing method and electronic device
By estimating the initial focus position using the defocus depth method and combining it with contrast focusing, the problems of long time consumption and high system complexity in existing autofocus methods are solved, achieving fast and accurate autofocus and improving the user experience.
Patent Information
- Application Number
- CN202610631892.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-09
- Publication Date
- 2026-07-03
AI Technical Summary
Among existing autofocus methods, the focus search algorithm is time-consuming, while the rangefinding method increases the system complexity.
The system employs a defocus depth-based method to estimate the initial focus position, combined with a contrast-based focusing method, to achieve fast and accurate autofocus.
It achieves fast and accurate autofocus, improving the user experience.
Smart Images

Figure CN122340357A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to an autofocus method and electronic device. Background Technology
[0002] Currently, common autofocus methods mainly include focus search algorithms and rangefinding methods.
[0003] The focus search algorithm drives the motor to a certain position, acquires the image at that position, calculates the image sharpness, and if the image sharpness does not meet the requirements, it continues to drive the motor to move according to the direction of the search algorithm until the target image sharpness meets the requirements.
[0004] Rangefinders can quickly adjust focus by receiving the reflected light waves from actively emitted light waves to measure the distance and orientation of the object being photographed. Then, based on the laws of imaging, the distance to the image plane is calculated, thereby adjusting the lens to achieve automatic focusing.
[0005] The focus search algorithm needs to continuously move the lens to get closer to the focus position during the focusing process. This process requires a lot of images, resulting in a large number of steps for the motor to move, so the focusing process takes a long time.
[0006] The ranging method requires additional ranging equipment, which increases the complexity of the system. Summary of the Invention
[0007] This invention aims to solve at least one of the technical problems existing in the prior art, and proposes an autofocus method and electronic device. The autofocus method of this invention has a fast focusing speed and high accuracy, which greatly improves the user experience.
[0008] In a first aspect, the present invention discloses an autofocus method, comprising the following steps:
[0009] Preliminary focus position is estimated based on the defocus depth method;
[0010] Starting from the initial focus position, adjust the lens and calculate the image sharpness at each focus position, and determine the focus position with the highest sharpness as the final focus position.
[0011] In some embodiments, the preliminary focus position is estimated based on the defocus depth method, specifically including: acquiring two defocus images with different defocus amounts in the same scene;
[0012] Calculate the blur difference between two out-of-focus images;
[0013] The distance to the target object is obtained based on the blur difference, the image distance is obtained based on the distance to the target object, and the lens position corresponding to the image distance is determined as the initial focus position.
[0014] In some embodiments, two defocused images with different defocus amounts in the same scene are two defocused images corresponding to the same scene obtained by changing only the image distance under the conditions of the same object distance, the same focal length, and the same lens diameter.
[0015] In some embodiments, acquiring two out-of-focus images with different out-of-focus amounts in the same scene specifically includes: determining the lens focus position of the two out-of-focus images estimated by the out-of-focus depth method;
[0016] Based on the lens focus position of the two determined out-of-focus images, two out-of-focus images are obtained for out-of-focus depth estimation.
[0017] In some embodiments, determining the lens focus position of the two out-of-focus images for out-of-focus depth estimation specifically includes:
[0018] Within the effective focusing stroke range corresponding to the Hall value of the focusing motor, three positions are selected: the preset position on the near-focus side, the preset position on the far-focus side, and the preset position in the middle area of the stroke.
[0019] From the three positions mentioned above, select the two positions with the highest image clarity as the lens focus positions for the two out-of-focus images.
[0020] In some embodiments, calculating the blur difference between two out-of-focus images specifically includes: performing a Laplacian operation on the same target region of the two out-of-focus images, calculating the sharpness quantization value of the two out-of-focus images in the target region based on the Laplacian operation result, and obtaining the blur difference between the two out-of-focus images based on the difference in the sharpness quantization value of the two out-of-focus images.
[0021] In some embodiments, the quantification of sharpness is the integral of the product of the pixel gray level and the result of the Laplacian operation within the target area.
[0022] In some embodiments, starting from the initial focus position, the lens is adjusted and the image sharpness at each focus position is calculated. The focus position with the highest sharpness is determined as the final focus position. Specifically, this includes:
[0023] Starting from the initial focus position, the lens is adjusted step by step in both forward and backward directions. The image sharpness of each focus position is calculated. If the initial focus position has the highest sharpness, then the initial focus position is the focus position with the highest sharpness. Otherwise, the search is carried out in the direction with greater sharpness until the focus position with the highest sharpness is found. The focus position with the highest sharpness is then determined as the final focus position.
[0024] In some embodiments, calculating the image sharpness at each focus position specifically includes:
[0025] The Sobel operator is used to extract the horizontal and vertical gradients of each pixel within a defined region of the image.
[0026] Calculate the gradient magnitude of each pixel based on the horizontal and vertical gradients;
[0027] The image sharpness is obtained by summing the squares of all gradient magnitudes that are greater than a preset threshold.
[0028] Secondly, the present invention also discloses an electronic device, comprising:
[0029] One or more processors;
[0030] Memory, used to store one or more programs;
[0031] When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in the first aspect.
[0032] Thirdly, the present invention also discloses a computer-readable storage medium on which a computer program is stored, which, when executed by a processor, implements the steps of the method as described in the first aspect.
[0033] The present invention has at least the following beneficial effects:
[0034] This invention is an autofocus method based on the defocus depth method. For any starting position of the motor, two defocused images with different image distances are acquired for coarse focus estimation. Then, a contrast focusing method is used to achieve fine focus, thereby achieving fast and accurate autofocus. The focusing speed is fast and the accuracy is high, which greatly improves the user experience. Attached Figure Description
[0035] Figure 1 This is a flowchart illustrating an autofocus method provided in an embodiment of the present invention.
[0036] Figure 2 This is a flowchart illustrating an optional specific implementation method of step S1 in an embodiment of the present invention;
[0037] Figure 3 This is a schematic diagram of the defocusing of the imaging system;
[0038] Figure 4 This is a structural block diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0039] To enable those skilled in the art to better understand the technical solutions of the present invention, exemplary embodiments of the present invention are described below in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to aid understanding. These should be considered merely exemplary. Therefore, those skilled in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0040] Where there is no conflict, the various embodiments of the present invention and the features thereof may be combined with each other.
[0041] As used herein, the term “and / or” includes any and all combinations of one or more related enumerated entries.
[0042] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used herein, the singular forms “a” and “the” are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will also be understood that when the terms “comprising” and / or “made of” are used in this specification, the presence of the stated feature, integral, step, operation, element, and / or component is specified, but the presence or addition of one or more other features, integrals, steps, operations, elements, components, and / or groups thereof is not excluded. Terms such as “connected” or “linked” are not limited to physical or mechanical connections but can include electrical connections, whether direct or indirect.
[0043] Unless otherwise specified, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by one of ordinary skill in the art. It will also be understood that terms such as those defined in commonly used dictionaries should be interpreted as having the meaning consistent with their meaning in the context of the relevant art and the invention, and will not be interpreted as having an idealized or overly formal meaning unless expressly so defined herein.
[0044] In the technical solution of this invention, the collection, storage, use, processing, transmission, provision, and disclosure of user personal information all comply with relevant laws and regulations and do not violate public order and good morals. The use of user data in this technical solution follows relevant national laws and regulations (e.g., the "Information Security Technology - Personal Information Security Specification"). For example: appropriate measures are taken for personal information access control; restrictions are imposed on the display of personal information; the purpose of using personal information does not exceed the scope of direct or reasonable association; and explicit identity targeting is eliminated when using personal information to avoid precisely locating a specific individual.
[0045] Figure 1 This is a flowchart illustrating an autofocus method according to an embodiment of the present invention. See also... Figure 1 An autofocus method includes the following steps:
[0046] S1. Estimate the initial focus position based on the defocus depth method;
[0047] S2. Starting from the initial focus position, adjust the lens and calculate the image sharpness at each focus position, and determine the focus position with the highest sharpness as the final focus position.
[0048] This invention is an autofocus method based on the defocus depth method. For any starting position of the motor, two defocused images with different image distances are acquired for coarse focus estimation. Then, a contrast focusing method is used to achieve fine focus, thereby achieving fast and accurate autofocus. The focusing speed is fast and the accuracy is high, which greatly improves the user experience.
[0049] In some embodiments, see Figure 2 The initial focus position is estimated based on the defocus depth method, which specifically includes: acquiring two defocus images with different defocus amounts in the same scene;
[0050] Calculate the blur difference between two out-of-focus images;
[0051] The distance to the target object is obtained based on the blur difference, the image distance is obtained based on the distance to the target object, and the lens position corresponding to the image distance is determined as the initial focus position.
[0052] In some embodiments, two defocused images with different defocus amounts in the same scene are two defocused images corresponding to the same scene obtained by changing only the image distance under the conditions of the same object distance, the same focal length, and the same lens diameter.
[0053] In some embodiments, acquiring two out-of-focus images with different out-of-focus amounts in the same scene specifically includes: determining the lens focus position of the two out-of-focus images estimated by the out-of-focus depth method;
[0054] Based on the lens focus position of the two determined out-of-focus images, two out-of-focus images are obtained for out-of-focus depth estimation.
[0055] In some embodiments, determining the lens focus position of the two out-of-focus images for out-of-focus depth estimation specifically includes:
[0056] For different distances (such as 0.3m, 0.5m, 1m, 2m, 3m, 5m, 8m, 10m, 12m, 15m, 18m, 20m, 25m, 30m, etc.), calculate the image sharpness corresponding to different lens focus positions, and obtain the Hall value corresponding to the position with the highest image sharpness at each distance.
[0057] Within the effective focusing stroke range corresponding to the Hall value of the focusing motor, three positions are selected: the preset position on the near-focus side, the preset position on the far-focus side, and the preset position in the middle area of the stroke.
[0058] From the three positions mentioned above, select the two positions with the highest image clarity as the lens focus positions for the two out-of-focus images.
[0059] This invention selects a Hall value at a position near the focal point, a Hall value at a position far from the focal point, and finally a Hall value in the middle region within the entire Hall stroke range of the motor. A total of three positions are selected throughout the entire stroke; the actual position selected needs to be determined by the user based on the stroke range of their specific motor.
[0060] This invention is an autofocus method based on the depth-of-focus (DFD) method. Within the effective range, the two input images of the DFD should be distributed on both sides of the sharpness peak, with a small difference in sharpness. Because the Hall data varies significantly near the focal point, and less so in the central and distant focal regions, to ensure minimal focus deviation at different distances, the Hall values corresponding to the two input images of the DFD should be set to positions with minimal Hall variation.
[0061]
[0062] The table above shows the Hall values corresponding to the positions with the highest sharpness at different distances. It can be seen from the table that the Hall values vary significantly from 0.3m to 1m, while the Hall values vary very little from 1m to 10m. Therefore, three positions can be selected for depth-of-focus estimation. These three positions are the positions with the maximum Hall value (F_hall), the minimum Hall value (N_hall), and a position near the middle of the Hall's effective travel (M_hall). For example, in one embodiment, the Hall values at 0.3m and 10m are used as N_hall and F_hall, respectively, and a position near the middle of the Hall's effective travel is used as the M_hall value. The sharpness at the corresponding N_hall, F_hall, and M_hall positions is compared, and the two positions with the highest sharpness are selected as the positions of the two out-of-focus images for DFD estimation. For example, based on the Hall values in the table above, the values of N_hall, F_hall, and M_hall can be selected as 2800, 5000, and 4000, respectively. These values can be obtained empirically.
[0063] In some embodiments, calculating the blur difference between two out-of-focus images specifically includes: performing a Laplacian operation on the same target region of the two out-of-focus images, calculating the sharpness quantization value of the two out-of-focus images in the target region based on the Laplacian operation result, and obtaining the blur difference between the two out-of-focus images based on the difference in the sharpness quantization value of the two out-of-focus images.
[0064] In some embodiments, the quantification of sharpness is the integral of the product of the pixel gray level and the result of the Laplacian operation within the target area.
[0065] In some embodiments, the blur difference between two defocused images is obtained based on the difference in the sharpness quantification values of the two defocused images. Specifically, this includes dividing the difference in the sharpness quantification values of the two defocused images by the normalized denominator and multiplying it by a coefficient (the coefficient in this embodiment is 8) to obtain the blur difference between the two defocused images. The normalized denominator is the sum of the integrals of the squares of the Laplacian operation results of the two defocused images over the target region.
[0066] The two images used for coarse focus estimation are two defocused images g1(x,y) and g2(x,y) obtained by changing only the image distance under the conditions of the same object distance, same focal length, and same lens diameter, and these two images correspond to the same f(x,y).
[0067]
[0068] According to the forward convolution formula of S-transform, the defocused images g1(x,y) and g2(x,y) have the following relationship with their corresponding in-focus images f(x,y) and blur parameters σ1 and σ2:
[0069]
[0070] The right side of the above formula represents the same focused image, from which we can obtain:
[0071]
[0072] in Let g be the Laplacian operator at image g(x,y). When calculating the Laplacian operator, a 7x7 Laplacian template is required, as shown below, with the following expression:
[0073]
[0074] In the formula, x represents an integer from -n to n, and in some embodiments, n is 3.
[0075] Based on the defocused images g1(x,y) and g2(x,y), we can obtain g3(x,y):
[0076]
[0077] Simplifying the above equation, we can obtain the blur difference between the two defocused images g1(x,y) and g2(x,y):
[0078]
[0079] In some embodiments, estimating the object distance based on the blur difference to obtain the preliminary focus position specifically includes: obtaining the target object distance based on the blur difference; calculating the ideal image distance based on the target object distance and the lens focal length parameter, which is the preliminary focus position.
[0080] Figure 3 This is a schematic diagram of the defocusing of the imaging system; Figure 3 In this diagram, u represents the ideal object distance, v represents the ideal image distance, u' represents the actual object distance, v' represents the actual image distance, f represents the focal length of the lens, D represents the aperture diameter of the lens, and R represents the radius of the blur circle.
[0081] Based on the geometric relationships of similar triangles and optical imaging, the expression for the radius R of the dispersion circle is:
[0082]
[0083] In practical applications, the point spread function (PSF) of an imaging system is approximated by a Gaussian function:
[0084]
[0085] And satisfy
[0086]
[0087] In the formula, σ is the fuzzy parameter, equivalent to the standard deviation of the Gaussian function, and is proportional to the radius of the fuzzy circle.
[0088]
[0089] Where k is a constant greater than 0, in most cases, .
[0090] Since the two out-of-focus images only change the image distance v, the change is Δv, which will lead to a change in the blur parameter σ+Δσ corresponding to the out-of-focus image. The expression for the blur parameter σ is as follows:
[0091]
[0092] Therefore, we get:
[0093]
[0094] g1(x,y) and g2(x,y) have the following relationship:
[0095] g1(x,y) = g2(x,y) * g β (x,y)
[0096] in, , .
[0097] The relationship between fuzzy difference and fuzzy parameter σ is as follows:
[0098]
[0099] Substituting the expressions for σ and Δσ into the above equation, we get:
[0100]
[0101] in Solving the quadratic equation above with respect to x yields a valid solution:
[0102]
[0103] in, The expression for obtaining the reciprocal of u' is as follows:
[0104]
[0105] In the above formula, u' represents the distance to the target object to be estimated (the distance u' of the target object is obtained using the defocus depth method).
[0106] The image distance can be calculated by using the relationship between image distance v, object distance u, and focal length f. The coarse focus position of the lens can then be determined according to the following formula.
[0107]
[0108] Where u represents the ideal object distance, v represents the ideal image distance, and f represents the focal length of the lens.
[0109] Substituting u' into the formula above, the image distance (the focus position estimated by DFD) is: 1 / v = 1 / f - 1 / u'. Here, v is the final image distance.
[0110] The focus position estimated by the depth-of-focus (DFD) method deviates from the actual focus position. Therefore, contrast-based focusing is required as a fine-tuning method to obtain a more accurate focus position.
[0111] In some embodiments, starting from the initial focus position, the lens is adjusted and the image sharpness at each focus position is calculated. The focus position with the highest sharpness is determined as the final focus position. Specifically, this includes:
[0112] Starting from the initial focus position, the lens is adjusted step by step in both forward and backward directions. The image sharpness of each focus position is calculated. If the initial focus position has the highest sharpness, then the initial focus position is the focus position with the highest sharpness. Otherwise, the search is carried out in the direction with greater sharpness until the focus position with the highest sharpness is found. The focus position with the highest sharpness is then determined as the final focus position.
[0113] This invention employs a small-step search strategy, which determines the final focus position by searching for and selecting the position with the highest sharpness.
[0114] Specifically, taking the initial focus position xf as a reference, the coordinates of hall and clarity at position xf are (hall_xf, clarity_xf). The lens is adjusted step by step in both the forward and backward directions to obtain the hall and clarity one step before and after hall_xf. If clarity_xf is the maximum, the focus position is hall_xf; otherwise, the lens is adjusted (i.e., the motor is moved) in the direction of greater clarity until the focus position with the greatest clarity is found. The focus position with the greatest clarity is then determined as the final focus position.
[0115] The image sharpness of this invention is calculated using the Tenengrad function. The Tenengrad function is defined as the sum of squares of the gradients of each pixel, and a threshold T is set for the gradient to adjust the sensitivity of the function.
[0116] In some embodiments, calculating the image sharpness at each focus position specifically includes:
[0117] The Sobel operator is used to extract the horizontal and vertical gradients of each pixel within a defined region of the image.
[0118] Calculate the gradient magnitude of each pixel based on the horizontal and vertical gradients;
[0119] The image sharpness is obtained by summing the squares of all gradient magnitudes that are greater than a preset threshold.
[0120] The summation of the squares of all gradient magnitudes greater than a preset threshold is shown in the following expression:
[0121]
[0122] Where G(x,y) is the gradient at pixel (x,y), and the expression for G(x,y) is as follows:
[0123]
[0124] Among them G x (x,y) and Gy (x, y) represent the gradient values of the pixel in the horizontal and vertical directions, G x (x,y) and G y The expression for (x,y) is shown below:
[0125]
[0126] in, g is the convolution symbol. x g y The horizontal and vertical templates for the Sobel operator are as follows:
[0127]
[0128] This invention is a fast autofocus method based on the defocus depth method. Compared with current autofocus methods, it has a faster focusing speed and higher focusing accuracy. This defocus depth method can be used in fields such as microscopes, endoscopes, camera systems, and intelligent monitoring technology.
[0129] Based on the same inventive concept, embodiments of the present invention also provide an electronic device. Figure 4 This is a structural block diagram of an electronic device provided in an embodiment of the present invention. Figure 4 As shown, an embodiment of the present invention provides an electronic device including: one or more processors 101, a memory 102, and one or more I / O interfaces 103. The memory 102 stores one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement any of the autofocus methods described in the above embodiments; the one or more I / O interfaces 103 are connected between the processor and the memory, configured to enable information interaction between the processor and the memory.
[0130] The processor 101 is a device with data processing capabilities, including but not limited to a central processing unit (CPU); the memory 102 is a device with data storage capabilities, including but not limited to random access memory (RAM, more specifically SDRAM, DDR, etc.), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), and flash memory (FLASH); the I / O interface (read / write interface) 103 is connected between the processor 101 and the memory 102, and can realize information interaction between the processor 101 and the memory 102, including but not limited to a data bus (Bus).
[0131] In some embodiments, the processor 101, memory 102, and I / O interface 103 are interconnected via bus 104, and thus connected to other components of the computing device.
[0132] In some embodiments, the one or more processors 101 include a field-programmable gate array.
[0133] This invention also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of any of the autofocus methods described in the above embodiments. The computer-readable storage medium may be volatile or non-volatile.
[0134] This invention also provides a computer program product, including computer-readable code, or a non-volatile computer-readable storage medium carrying computer-readable code. When the computer-readable code is run in the processor of an electronic device, the processor in the electronic device executes the above-described autofocus method.
[0135] Those skilled in the art will understand that all or some of the steps, systems, and apparatuses disclosed above, and their functional modules / units, can be implemented as software, firmware, hardware, or suitable combinations thereof. In hardware implementations, the division between functional modules / units mentioned above does not necessarily correspond to the division of physical components; for example, a physical component may have multiple functions, or a function or step may be performed collaboratively by several physical components. Some or all physical components may be implemented as software executed by a processor, such as a central processing unit, digital signal processor, or microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit (ASIC). Such software can be distributed on a computer-readable storage medium, which may include computer storage media (or non-transitory media) and communication media (or transient media).
[0136] As is known to those skilled in the art, the term computer storage medium includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information, such as computer-readable program instructions, data structures, program modules, or other data. Computer storage media includes, but is not limited to, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM), static random access memory (SRAM), flash memory or other memory technologies, portable compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical disc storage, magnetic cartridges, magnetic tape, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and is accessible to a computer. Furthermore, it is known to those skilled in the art that communication media typically contain computer-readable program instructions, data structures, program modules, or other data in modulated data signals such as carrier waves or other transmission mechanisms, and may include any information delivery medium.
[0137] The computer-readable program instructions described herein can be downloaded from computer-readable storage media to various computing / processing devices, or downloaded via a network, such as the Internet, local area network, wide area network, and / or wireless network, to an external computer or external storage device. The network may include copper transmission cables, fiber optic transmission, wireless transmission, routers, firewalls, switches, gateway computers, and / or edge servers. A network adapter card or network interface in each computing / processing device receives the computer-readable program instructions from the network and forwards them to the computer-readable storage media in the respective computing / processing device.
[0138] The computer program instructions used to perform the operations of this invention may be assembly instructions, instruction set architecture (ISA) instructions, machine instructions, machine-dependent instructions, microcode, firmware instructions, state setting data, or source code or object code written in any combination of one or more programming languages, including object-oriented programming languages such as Smalltalk, C++, etc., and conventional procedural programming languages such as the "C" language or similar programming languages. The computer-readable program instructions may be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or may be connected to an external computer (e.g., via the Internet using an Internet service provider). In some embodiments, electronic circuitry, such as programmable logic circuitry, field-programmable gate arrays (FPGAs), or programmable logic arrays (PLAs), is personalized by utilizing state information from the computer-readable program instructions. This electronic circuitry can execute the computer-readable program instructions to implement various aspects of the invention.
[0139] The computer program product described herein can be implemented specifically through hardware, software, or a combination thereof. In one alternative embodiment, the computer program product is specifically embodied in a computer storage medium; in another alternative embodiment, the computer program product is specifically embodied in a software product, such as a software development kit (SDK), etc.
[0140] Various aspects of the present invention are described herein with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer-readable program instructions.
[0141] These computer-readable program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing apparatus to produce a machine such that, when executed by the processor of the computer or other programmable data processing apparatus, they create means for implementing the functions / actions specified in one or more blocks of the flowchart and / or block diagram. These computer-readable program instructions can also be stored in a computer-readable storage medium that causes a computer, programmable data processing apparatus, and / or other device to operate in a particular manner; thus, the computer-readable medium storing the instructions comprises an article of manufacture that includes instructions for implementing aspects of the functions / actions specified in one or more blocks of the flowchart and / or block diagram.
[0142] Computer-readable program instructions may also be loaded onto a computer, other programmable data processing apparatus, or other device to cause a series of operational steps to be performed on the computer, other programmable data processing apparatus, or other device to produce a computer-implemented process, thereby causing the instructions executed on the computer, other programmable data processing apparatus, or other device to perform the functions / actions specified in one or more boxes of a flowchart and / or block diagram.
[0143] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of an instruction, which contains one or more executable instructions for implementing a specified logical function. In some alternative implementations, the functions marked in the blocks may occur in a different order than those shown in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, may be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0144] Example embodiments have been disclosed herein, and while specific terminology has been used, it is for illustrative purposes only and should be construed as such, and is not intended to be limiting. In some instances, it will be apparent to those skilled in the art that features, characteristics, and / or elements described in conjunction with particular embodiments may be used alone, or in combination with features, characteristics, and / or elements described in conjunction with other embodiments, unless otherwise expressly indicated. Therefore, those skilled in the art will understand that various changes in form and detail may be made without departing from the scope of the invention as set forth in the appended claims.
Claims
1. An autofocus method, characterized in that, Includes the following steps: Preliminary focus position is estimated based on the defocus depth method; Starting from the initial focus position, adjust the lens and calculate the image sharpness at each focus position, and determine the focus position with the highest sharpness as the final focus position.
2. The method according to claim 1, characterized in that: The initial focus position is estimated based on the defocus depth method, which specifically includes: acquiring two defocused images with different defocus amounts in the same scene; Calculate the blur difference between two out-of-focus images; The distance to the target object is obtained based on the blur difference, the image distance is obtained based on the distance to the target object, and the lens position corresponding to the image distance is determined as the initial focus position.
3. The method according to claim 2, characterized in that: Two defocused images of the same scene with different defocus amounts are two defocused images of the same scene obtained by changing only the image distance, under the conditions of the same object distance, same focal length, and same lens diameter.
4. The method according to claim 2 or 3, characterized in that: Acquiring two out-of-focus images with different defocus amounts in the same scene specifically includes: determining the lens focus position of the two out-of-focus images for defocus depth estimation; Based on the lens focus position of the two determined out-of-focus images, two out-of-focus images are obtained for out-of-focus depth estimation.
5. The method according to claim 4, characterized in that: Determine the lens focus position of the two out-of-focus images for depth-of-focus estimation, specifically including: Within the effective focusing stroke range corresponding to the Hall value of the focusing motor, three positions are selected: the preset position on the near-focus side, the preset position on the far-focus side, and the preset position in the middle area of the stroke. From the three positions mentioned above, select the two positions with the highest image clarity as the lens focus positions for the two out-of-focus images.
6. The method according to claim 2, characterized in that: The calculation of the blur difference between two out-of-focus images specifically includes: performing a Laplacian operation on the same target region of the two out-of-focus images; calculating the sharpness quantization value of the two out-of-focus images in the target region based on the Laplacian operation result; and obtaining the blur difference between the two out-of-focus images based on the difference in the sharpness quantization values of the two out-of-focus images.
7. The method according to claim 6, characterized in that: The quantification of sharpness is the integral of the product of the pixel gray level and the result of the Laplacian operation within the target area.
8. The method according to claim 1, characterized in that: Starting from the initial focus position, the lens is adjusted and the image sharpness at each focus position is calculated. The focus position with the highest sharpness is determined as the final focus position, specifically including: Starting from the initial focus position, the lens is adjusted step by step in both forward and backward directions. The image sharpness of each focus position is calculated. If the initial focus position has the highest sharpness, then the initial focus position is the focus position with the highest sharpness. Otherwise, the search is carried out in the direction with greater sharpness until the focus position with the highest sharpness is found. The focus position with the highest sharpness is then determined as the final focus position.
9. The method according to claim 1 or 8, characterized in that: Calculating image sharpness at each focus position specifically includes: The Sobel operator is used to extract the horizontal and vertical gradients of each pixel within a defined region of the image. Calculate the gradient magnitude of each pixel based on the horizontal and vertical gradients; The image sharpness is obtained by summing the squares of all gradient magnitudes that are greater than a preset threshold.
10. An electronic device, characterized in that, include: One or more processors; Memory, used to store one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1 to 9.
