Camera virtual focus detection method, electronic equipment and computer readable storage medium

By obtaining the gradient of image pixels in camera defocus detection, filtering out the pixels to be analyzed and setting an appropriate transition threshold, and using the edge width to determine defocus, the problem of inaccurate defocus detection results is solved, and higher detection accuracy is achieved.

CN121664972APending Publication Date: 2026-03-13ZHEJIANG PIXFRA TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-20
Publication Date
2026-03-13

AI Technical Summary

Technical Problem

In existing technologies, camera defocus detection is easily affected by scene changes, leading to inaccurate detection results.

Method used

By acquiring the image to be detected from the camera, traversing all pixels to determine the gradient, filtering out the pixels to be analyzed, setting a transition threshold that is adapted to the scene, and determining whether the camera is out of focus based on the edge width of the edge pixels.

Benefits of technology

It improves the accuracy and reliability of out-of-focus detection, and can provide accurate out-of-focus detection results in different scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121664972A_ABST
    Figure CN121664972A_ABST
Patent Text Reader

Abstract

The invention discloses a camera virtual focus detection method, electronic equipment and a computer readable storage medium, and the method comprises the steps: obtaining a to-be-detected image collected by a camera, and determining a gradient corresponding to each pixel point in the to-be-detected image; screening a part of pixel points from the to-be-detected image by using the gradient of each pixel point to serve as to-be-analyzed pixel points, and obtaining a jump threshold matched with the pixel values of the to-be-analyzed pixel points; based on the pixel values corresponding to every two adjacent pixel points in the preset direction of the to-be-analyzed pixel point and the jump threshold, obtaining edge pixel points of the to-be-analyzed pixel point in the preset direction, and determining the edge width between the edge pixel points in the preset direction; and determining a virtual focus detection result of the camera based on the edge width corresponding to the to-be-analyzed pixel point. In this way, the accuracy of virtual focus detection can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of image detection technology, and in particular to a method for detecting camera defocus, an electronic device, and a computer-readable storage medium. Background Technology

[0002] Images can carry a wealth of visual information, but the effective information conveyed by images captured by a camera in out-of-focus mode is significantly reduced. Therefore, to ensure that the camera can capture clear images, out-of-focus detection is necessary. However, existing technologies for out-of-focus detection are easily affected by scene changes, leading to inaccurate results. Therefore, improving the accuracy of out-of-focus detection has become an urgent problem to be solved. Summary of the Invention

[0003] The main technical problem addressed by this application is to provide a camera defocus detection method, electronic device, and computer-readable storage medium that can improve the accuracy of defocus detection.

[0004] To address the aforementioned technical problems, this application provides a camera defocus detection method, comprising: acquiring a camera-captured image to be detected; determining the gradient corresponding to each pixel in the image to be detected; using the gradient of each pixel to filter a subset of pixels from the image to be detected as pixels to be analyzed; acquiring a transition threshold that matches the pixel value of the pixels to be analyzed; obtaining edge pixels of the pixels to be analyzed in a preset direction based on the pixel value corresponding to every two adjacent pixels in a preset direction and the transition threshold; determining the edge width between the edge pixels in the preset direction; and determining the camera defocus detection result based on the edge width corresponding to the pixels to be analyzed.

[0005] To address the aforementioned technical problems, a second aspect of this application provides an electronic device comprising: a memory and a processor coupled to each other, wherein the memory stores program data, and the processor invokes the program data to execute the method described in the first aspect.

[0006] To address the aforementioned technical problems, a third aspect of this application provides a computer-readable storage medium storing program data thereon, wherein the program data, when executed by a processor, implements the method described in the first aspect.

[0007] The beneficial effects of this application are as follows: Unlike existing technologies, this application acquires a camera-captured image to be detected, traverses all pixels in the image, and determines the gradient corresponding to each pixel. The gradient of each pixel is used to filter pixels in the image to be detected, and the filtered pixels are used as pixels to be analyzed. A jump threshold matching the pixel value of the pixel to be analyzed is obtained. A matching jump threshold is then set based on the pixel value of the pixel to be analyzed, ensuring that the jump threshold is adapted to the scene. Based on the pixel value and jump threshold of every two adjacent pixels in a preset direction, edge pixels of the pixel to be analyzed in the preset direction are determined. Using a jump threshold adapted to the scene, pixels whose pixel value changes match the jump threshold are found from the preset direction, thus obtaining edge pixels. The edge width between two edge pixels in the preset direction is determined, improving the reliability of the edge width. Based on the edge width corresponding to the pixel to be analyzed, whether the camera is out of focus is determined. Therefore, using a reliable edge width, accurate out-of-focus detection results are obtained. Attached Figure Description

[0008] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. Wherein: Figure 1 This is a flowchart illustrating one embodiment of the camera defocus detection method of this application; Figure 2 This is a flowchart illustrating another embodiment of the camera defocus detection method of this application; Figure 3 This is a schematic diagram of the structure of one embodiment of the electronic device of this application; Figure 4 This is a schematic diagram of one embodiment of the computer-readable storage medium of this application. Detailed Implementation

[0009] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments, and different implementation methods can be adaptively combined. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0010] 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.

[0011] The camera defocus detection method provided in this application is used to detect whether an image is out of focus when the camera acquires it. The corresponding execution entity is a processing unit capable of image processing. Furthermore, the above-mentioned camera defocus detection method is adaptable to different types of cameras, such as visible light cameras or thermal imaging cameras. In images acquired by thermal imaging cameras, the pixel values ​​of individual pixels can reflect the energy of those pixels, thus enabling more accurate results when applying the camera defocus detection method of this application.

[0012] Please see Figure 1 , Figure 1 This is a flowchart illustrating one embodiment of the camera defocus detection method of this application. The method includes: S101: Acquire the image to be detected captured by the camera and determine the gradient corresponding to each pixel in the image to be detected.

[0013] Specifically, the image to be detected is acquired by the camera, all pixels in the image are traversed, and the gradient corresponding to each pixel is determined.

[0014] It should be noted that each pixel corresponds to a pixel value, and the gradient feedback is the degree of change of pixel value between each pixel and its surrounding pixels.

[0015] In one embodiment, an image to be detected is acquired by a camera, all pixels in the image to be detected are traversed, the pixel value corresponding to the current pixel and the pixels within a preset range are obtained, and the gradient corresponding to each pixel is determined based on the difference between the pixel value of the current pixel and the other pixels within the preset range.

[0016] In one embodiment, an image to be detected is acquired by a camera. All pixels in the image are traversed, and the pixel values ​​corresponding to adjacent pixels in a preset direction are obtained. Based on the difference in pixel values ​​between adjacent pixels in the preset direction, the gradient corresponding to each pixel is determined. The preset direction includes both horizontal and vertical directions.

[0017] In some implementation scenarios, the image to be detected acquired by the camera is a thermal image. This thermal image is obtained by image signal processing based on raw data (Raw data). Raw data is the energy signal received by the thermal imaging camera's detector. Each pixel in a thermal image occupies 2 bytes (16 bits), but the actual effective sampling bits are the lower 14 bits. Based on the pixel values ​​of each pixel and other pixels within a preset range or in a preset direction, the gradient corresponding to each pixel is determined.

[0018] In some implementation scenarios, the image to be detected captured by the camera is a color image. Based on the mean of the RGB three channels of the color image, the pixel value of the pixel on the color image is determined. Based on the pixel value of each pixel and other pixels within a preset range or in a preset direction, the gradient corresponding to each pixel is determined.

[0019] S102: Filter out a portion of pixels from the image to be detected using the gradient of each pixel, and use them as pixels to be analyzed. Obtain a transition threshold that matches the pixel value of the pixels to be analyzed.

[0020] Specifically, the gradient of each pixel is used to filter pixels in the image to be detected. The filtered pixels are used as pixels to be analyzed. A transition threshold that matches the pixel value of the pixel to be analyzed is obtained. Then, a matching transition threshold is set based on the pixel value of the pixel to be analyzed, so that the transition threshold is adapted to the scene.

[0021] It should be noted that the pixels to be analyzed correspond to the pixels with larger gradients in the image to be detected, thus selecting pixels with larger pixel value changes as those that need to be further analyzed.

[0022] In one embodiment, a gradient threshold of the image to be detected is obtained, and pixels with gradients greater than the gradient threshold are taken as pixels to be analyzed.

[0023] In one embodiment, the top N pixels with the largest gradient in the image to be detected are obtained, and these top N pixels with the largest gradient are used as the pixels to be analyzed.

[0024] Furthermore, based on the pixel value of the pixel to be analyzed, a transition threshold is set for each pixel to be analyzed.

[0025] In one embodiment, a preset adjustment ratio is obtained, and the pixel value of the pixel to be analyzed is adjusted according to the adjustment ratio to obtain the jump threshold corresponding to the pixel to be analyzed.

[0026] In one embodiment, the mean gradient value corresponding to the gradient of all pixels in the image to be detected is obtained, the gradient difference between the gradient of the pixel to be analyzed and the mean gradient value is determined, and the ratio of the gradient difference to a preset value is used as the transition threshold corresponding to the pixel to be analyzed.

[0027] It is understandable that the pixel value and gradient of each pixel in the image to be detected are associated with the scene of the image to be detected. After setting a jump threshold that is adaptive to the pixel value for the pixel to be analyzed, the pixel to be analyzed can obtain a jump threshold that is adapted to the scene in the images to be detected collected in different scenes, thereby effectively reducing the influence of different scenes on the uncertainty of the threshold.

[0028] S103: Based on the pixel value and transition threshold of each pair of adjacent pixels in the preset direction of the pixel to be analyzed, obtain the edge pixels of the pixel to be analyzed in the preset direction, and determine the edge width between the edge pixels in the preset direction.

[0029] Specifically, based on the pixel values ​​and transition thresholds of every two adjacent pixels in a preset direction of the pixel to be analyzed, the edge pixels of the pixel to be analyzed in the preset direction are determined. Then, by using a transition threshold adapted to the scene, the pixel values ​​of adjacent pixels in the preset direction that match the transition threshold are found to obtain the edge pixels. The edge width between two edge pixels in the preset direction is determined, thereby improving the reliability of the edge width.

[0030] In one embodiment, starting from the pixel to be analyzed, the absolute difference between the pixel values ​​corresponding to every two adjacent pixels is obtained. It is then determined whether the absolute difference reaches a threshold. If it does, the absolute difference between the pixel values ​​corresponding to two adjacent pixels is further determined in a preset direction. Otherwise, the pixel whose absolute difference in the preset direction reaches the threshold and is located at the outermost edge is taken as an edge pixel. This yields the edge pixels corresponding to both sides of the pixel to be analyzed in the preset direction. The pixel distance between the edge pixels located on both sides of the pixel to be analyzed in the preset direction is taken as the edge width in the preset direction. Here, the absolute difference is the absolute value of the difference between the two values.

[0031] In one embodiment, based on the pixel values ​​corresponding to the pixel to be analyzed and its adjacent pixels on any side of a preset direction, the trend of pixel value change in the preset direction is determined. For every two adjacent pixels in the preset direction, it is determined whether the pixel closer to the pixel to be analyzed, after increasing or decreasing a jump threshold according to the trend, still satisfies the trend compared to the pixel farther from the pixel to be analyzed. This process continues until the trend changes. The pixel before the change is designated as an edge pixel. Specifically, when the trend is increasing, the pixel closer to the pixel to be analyzed is added to the jump threshold; when the trend is decreasing, the pixel closer to the pixel to be analyzed is subtracted from the jump threshold. The pixel distance between two edge pixels that satisfy the trend in the preset direction is taken as the edge width in the preset direction.

[0032] It is understandable that the preset direction includes the horizontal and vertical directions. By using the jump threshold, two sets of edge pixels can be obtained in each of the two directions. Thus, in both the horizontal and vertical directions, the value with the larger edge width is selected as the edge width of the corresponding pixel to be analyzed.

[0033] S104: Determine the camera's defocus detection result based on the edge width corresponding to the pixel to be analyzed.

[0034] Specifically, based on the edge width corresponding to the pixel to be analyzed, the camera is judged to be out of focus, thereby using reliable edge width to obtain accurate out-of-focus detection results.

[0035] In one embodiment, the average width of the edge width of all pixels to be analyzed is obtained, and the average width is compared with a preset width threshold. When the average width is greater than or equal to the width threshold, the camera's defocus detection result is determined to be in focus. When the average width is less than the width threshold, the camera's defocus detection result is determined to be in focus, i.e., in focus.

[0036] In one embodiment, from all the pixels to be analyzed, the number of pixels with an edge width greater than or equal to a preset width threshold is determined as the number to be compared. When the number to be compared is greater than or equal to the number of defocus detections, the camera's defocus detection result is determined to be that defocus exists. When the number to be compared is less than the number of defocus detections, the camera's defocus detection result is determined to be that defocus does not exist.

[0037] In one embodiment, the preset direction includes a horizontal direction and a vertical direction. The larger value of the edge width in the two directions is divided by the smaller value to obtain the width ratio. The pixel points to be analyzed with a width ratio greater than the preset ratio are taken as feature edge points. The edge width of the image to be detected is obtained using the edge width of the feature edge points. The image edge width is compared with a preset width threshold. When the image edge width is greater than or equal to the width threshold, the camera's defocus detection result is determined to be in focus. When the image edge width is less than the width threshold, the camera's defocus detection result is determined to be in focus, i.e., in focus.

[0038] The above scheme acquires the image to be detected captured by the camera, traverses all pixels in the image, and determines the gradient corresponding to each pixel. Using the gradient of each pixel, pixels in the image are filtered, and the filtered pixels are used as the pixels to be analyzed. A transition threshold matching the pixel value of the pixel to be analyzed is obtained. A matching transition threshold is then set based on the pixel value of the pixel to be analyzed, ensuring the transition threshold is adapted to the scene. Based on the pixel value and transition threshold of every two adjacent pixels in a preset direction, edge pixels of the pixel to be analyzed in the preset direction are determined. Using the transition threshold adapted to the scene, pixels whose pixel value changes match the transition threshold are found in the preset direction, thus obtaining edge pixels. The edge width between two edge pixels in the preset direction is determined, improving the reliability of the edge width. Based on the edge width corresponding to the pixel to be analyzed, whether the camera is out of focus is determined. Therefore, using reliable edge widths, accurate out-of-focus detection results are obtained.

[0039] Please see Figure 2 , Figure 2 This is a flowchart illustrating another embodiment of the camera defocus detection method of this application, the method comprising: S201: Acquire the image to be detected captured by the camera and determine the gradient corresponding to each pixel in the image to be detected.

[0040] Specifically, the image to be detected is acquired by the camera, all pixels in the image are traversed, and the gradient of each pixel is analyzed.

[0041] In one embodiment, an image to be detected is acquired by a camera, and the pixel value of each pixel in the image to be detected is determined; the pixels in the image to be detected are traversed, and the gradient corresponding to each pixel is determined based on the pixel values ​​of adjacent pixels in a preset direction for each pixel in the image to be detected.

[0042] Specifically, the process involves acquiring the image to be detected from the camera, determining the pixel value of each pixel in the image, and when the image is a thermal image, the pixel value reflects the pixel's energy. The process then iterates through the pixels in the image, using the pixel values ​​of adjacent pixels in a preset direction for each pixel to calculate the degree of change in the pixel value, thereby obtaining the gradient corresponding to the pixel and ensuring the accuracy of the gradient.

[0043] Optionally, the filter operator is convolved with each pixel in the image to be detected, and the gradient of each pixel is calculated by traversing each pixel. The filter operator is a 3x3 feature matrix = [[0,-1,0],[-1,0,1],[0,1,0]]. The gradient of the corresponding pixel is determined by multiplying the pixels around each pixel with the feature matrix according to their positions and summing the results. The absolute value is adjusted when calculating the sum. For example, if the pixel value in row i and column j is p(i,j), then its gradient g(i,j) = (|p(i,j+1)–p(i,j-1)|+|p(i+1,j)–p(i-1,j)|) / 2. The original image data is convolved with each pixel to obtain a gradient image.

[0044] S202: Obtain the number of pixels corresponding to each gradient. Based on the number of pixels corresponding to different gradients, determine the gradient threshold of the image to be detected. Pixels whose gradient reaches the gradient threshold are taken as pixels to be analyzed.

[0045] Specifically, the number of pixels corresponding to each gradient is counted, and based on the number of pixels corresponding to different gradients, the gradient threshold of the image to be detected is selected from all gradients so that the gradient threshold is adapted to the scene of the image to be detected.

[0046] It is understandable that the pixels whose gradient reaches the gradient threshold, that is, the pixels whose gradient is greater than or equal to the gradient threshold, are selected as the pixels to be analyzed.

[0047] In one embodiment, the number of pixels corresponding to each gradient is obtained, and a gradient threshold for the image to be detected is determined based on the number of pixels corresponding to different gradients. Pixels whose gradients reach the gradient threshold are selected as pixels to be analyzed. This includes: sorting all gradients corresponding to the image to be detected from high to low, and obtaining the number of pixels corresponding to each gradient; obtaining gradients whose total number of pixels reaches a preset number based on the number of pixels corresponding to the top-ranked gradients, and using this as a gradient threshold; and selecting pixels whose gradients reach the gradient threshold and are greater than the preset gradient as pixels to be analyzed.

[0048] Specifically, all available gradients in the image to be detected are obtained, the gradients are sorted from highest to lowest, and the number of pixels corresponding to each gradient is determined to obtain the gradient histogram matching the gradients of all pixels in the image to be detected.

[0049] Furthermore, starting from the gradient with the largest value in the gradient histogram, the algorithm searches backwards to determine whether the total number of pixels corresponding to the gradients at the top of the sorted list has reached a preset number. If the preset number has been reached, the last gradient found is used as the gradient threshold, thereby ensuring the matching degree between the gradient threshold and the image to be detected, and effectively controlling the number of pixels to be analyzed.

[0050] It should be noted that pixels whose gradient reaches the gradient threshold and is greater than the preset gradient are selected as pixels to be analyzed, thereby filtering out pixels whose gradient is less than or equal to the preset gradient. This avoids introducing too many pixels with low degree of change when the overall gradient of the image to be detected does not change much, ensuring the accuracy of the analysis results and improving processing efficiency.

[0051] S203: Based on the pixel value of the pixel to be analyzed, determine the adjustment ratio of the pixel to be analyzed, and adjust the pixel value of the pixel to be analyzed according to the adjustment ratio to obtain the jump threshold corresponding to the pixel to be analyzed.

[0052] Specifically, based on the pixel value of the pixel to be analyzed, an adjustment ratio that matches the pixel value is adaptively set for the pixel to be analyzed, so that the pixel value of the pixel to be analyzed is adjusted according to the adjustment ratio, and the adjusted pixel value is used as the jump threshold corresponding to the pixel to be analyzed.

[0053] It should be noted that the transition threshold is adjusted to be less than the pixel value of the pixel to be analyzed, and each pixel to be analyzed needs to have its own transition threshold calculated. The adjustment ratio changes with the pixel value of the pixel to be analyzed.

[0054] In some implementation scenarios, when the pixel value of the pixel to be analyzed is greater than the separation threshold, the corresponding pixel value is divided by a first value to obtain the transition threshold. When the pixel value of the pixel to be analyzed is less than or equal to the separation threshold, the corresponding pixel value is divided by a second threshold to obtain the transition threshold. The first value is less than the second value, thus resulting in a larger transition threshold when the pixel value is larger and a smaller transition threshold when the pixel value is smaller.

[0055] S204: Based on the pixel value and transition threshold of every two adjacent pixels in the preset direction of the pixel to be analyzed, obtain the edge pixels of the pixel to be analyzed in the preset direction, and determine the edge width between the edge pixels in the preset direction.

[0056] Specifically, based on the pixel values ​​and transition thresholds of every two adjacent pixels in the preset direction of the pixel to be analyzed, the edge pixels of the pixel to be analyzed in the preset direction are determined. Then, by using the transition threshold adapted to the scene, the pixel values ​​of adjacent pixels that match the transition threshold are found from the preset direction to obtain the edge pixels and determine the edge width between two edge pixels in the preset direction.

[0057] In one embodiment, based on the pixel values ​​corresponding to the pixel to be analyzed and the adjacent pixels on any side of the preset direction, the trend of pixel value change in the preset direction is determined; according to the trend of change, it is determined whether the difference between the pixel values ​​of every two adjacent pixels in the preset direction reaches the jump threshold, until the jump threshold is not reached, and the edge pixel in the preset direction is obtained; the pixel distance between the edge pixels at both ends of the preset direction is taken as the edge width in the preset direction.

[0058] Specifically, the pixel values ​​of the pixel to be analyzed and its adjacent pixels on any side of a preset direction are obtained. The pixel values ​​of the two adjacent pixels are compared to determine the trend of pixel value change along the preset direction. Following this trend, the difference between the pixel values ​​of every two adjacent pixels along the preset direction is sequentially determined to see if it reaches a change threshold. This process continues until the difference between the pixel values ​​of two adjacent pixels along the preset direction does not reach a jump threshold. The pixel closer to the pixel to be analyzed among the two outermost adjacent pixels is then identified as the edge pixel along the preset direction. Therefore, after determining the trend, it is possible to judge whether every two adjacent pixels along the preset direction satisfy the trend and whether the degree of change reaches the jump threshold, thus accurately identifying the edge pixels.

[0059] It is understandable that there are opposite trends of change on both sides of the pixel to be analyzed, and corresponding edge pixels can be obtained on both sides of the pixel to be analyzed. The pixel distance between the edge pixels at both ends of the preset direction is taken as the edge width in the preset direction.

[0060] For ease of understanding, assume that the pixel value at the \(i\)-th row and \(j\)-th column is \(p(i, j)\), and the jump threshold is \(p_{Thre}\). First, look to the left. If \(p(i, j - 1)\geq p(i, j)+p_{Thre}\), then the maximum value in the horizontal direction is on the left and the minimum value is on the right. Traverse every two adjacent pixel points to the left in sequence. If \(p(i, j - m)\geq p(i, j - m + 1)+p_{Thre}\) and \(p(i, j - m - 1)\lt p(i, j - m)+p_{Thre}\), then \(p(i, j - m)\) is the left edge pixel point. Then, look to the right. Traverse two adjacent pixel points to the right in sequence. If \(p(i, j + n)\leq p(i, j + n - 1)-p_{Thre}\) and \(p(i, j + n + 1)\gt p(i, j + n)-p_{Thre}\), then \(p(i, j + n)\) is the right edge pixel point. Further, the edge width \(W_h\) in the horizontal direction is \(j + n-(j - m)+1=n + m + 1\).

[0061] It can be understood that if \(p(i, j - 1)\leq p(i, j)-p_{Thre}\), then the minimum value in the horizontal direction is on the left and the maximum value is on the right. By analogy with the above calculation, the edge width \(W_h\) in the horizontal direction can be obtained. This application will not elaborate on this any further.

[0062] Furthermore, calculate the edge width \(W_v\) in the vertical direction at this point. First, look up. If \(p(i - 1, j)\geq p(i, j)+p_{Thre}\), then the maximum value in the vertical direction is above and the minimum value is below. Traverse every two adjacent pixel points upward in sequence. If \(p(i - u, j)\geq p(i - u + 1, j)+p_{Thre}\) and \(p(i - u - 1, j)\lt p(i - u, j)+p_{Thre}\), then \(p(i - u, j)\) is the upper edge pixel point. Then, look down. Traverse every two adjacent pixel points downward. If \(p(i + d, j)\leq p(i + d - 1, j)-p_{Thre}\) and \(p(i + d + 1, j)\gt p(i + d, j)-p_{Thre}\), then \(p(i + d, j)\) is the lower edge pixel point. Further, the edge width \(W_v\) in the vertical direction is \(d + u + 1\).

[0063] It can be understood that if \(p(i - 1, j)\leq p(i, j)-p_{Thre}\), then the minimum value in the vertical direction is above and the maximum value is below. The calculation method is analogous to the above. Therefore, the edge width \(W_p\) corresponding to the pixel point at the \(i\)-th row and \(j\)-th column is \(max(W_h, W_v)\), that is, the larger value between \(W_h\) and \(W_v\).

[0064] S205: Determine the defocus detection result of the camera based on the edge width corresponding to the pixel point to be analyzed.

[0065] Specifically, based on the edge width corresponding to the pixel point to be analyzed, judge whether the camera is defocused, so as to analyze and obtain an accurate defocus detection result using a reliable edge width.

[0066] It is understandable that the preset direction includes the horizontal direction and the vertical direction, and the horizontal direction and the vertical direction each correspond to an edge width. The final edge width of the pixel to be analyzed is the larger of the two values.

[0067] In one embodiment, determining the camera's defocus detection result based on the edge width corresponding to the pixel to be analyzed includes: obtaining the width ratio between the horizontal and vertical edge widths of the pixel to be analyzed; taking the pixel to be analyzed whose width ratio satisfies a preset ratio condition as a feature edge point; and determining the camera's defocus detection result based on the final edge width of all feature edge points.

[0068] Specifically, the width ratio between the horizontal and vertical edge widths of the pixel to be analyzed is obtained, and the pixel to be analyzed that meets the preset ratio condition is selected. The selected pixel is used as feature edge points so as to obtain feature edge points that match the blurred target object edge.

[0069] It should be noted that the preset ratio condition is usually a ratio greater than or less than a preset multiplier, for example, Wh / Wv>3 or Wh / Wv<1 / 3. In cases of camera defocus, at the edges of objects in the image to be detected, the gradient change is typically smaller in one direction due to the object itself, resulting in a narrower edge width, while the gradient change is larger at the edge boundaries in the other direction, resulting in a wider edge width. Therefore, the preset ratio condition can effectively filter out feature edge points at the edges, improving the accuracy of feature edge point identification.

[0070] Furthermore, the final edge width of all feature edge points is used to detect whether the camera is out of focus, thereby ensuring the accuracy of the out-of-focus detection results.

[0071] In some implementation scenarios, the camera's defocus detection result is determined based on the final edge width of all feature edge points. This includes: obtaining the total number of edge points of all feature edge points; obtaining the feature edge width of the image to be detected based on the final edge width of all feature edge points and the total number of edge points; and determining the camera's defocus detection result based on the total number of edge points and the feature edge width.

[0072] Specifically, the total number of edge points corresponding to all feature edge points is counted, the sum of the final edge widths of all feature edge points is calculated, and then divided by the total number of edge points to obtain the average edge width of all feature edge points, which is used as the feature edge width of the image to be detected. By combining the two dimensions of the total number of edge points and the feature edge width of the image, it is possible to jointly determine whether the camera is out of focus and determine the camera's out-of-focus detection result. Thus, the image edge width is used as the basis for judging whether the image is clear, which has the uniformity of the whole scene.

[0073] It should be noted that the camera's defocus detection result is determined based on the total number of edge points and the width of the feature edge, including: determining that the camera's defocus detection result is defocused when the total number of edge points is less than a quantity threshold; determining that the camera's defocus detection result is not defocused when the total number of edge points is greater than or equal to a quantity threshold and the width of the feature edge is less than a width threshold; and determining that the camera's defocus detection result is defocused when the total number of edge points is greater than or equal to a quantity threshold and the width of the feature edge is greater than or equal to a width threshold.

[0074] Specifically, when the total number of edge points is less than the number threshold, the camera's defocus detection result is determined to be defocused, so that when the number of feature edge points is small, it is determined that there is a lack of necessary judgment basis in the image to be detected, thus giving the conclusion that there is defocus.

[0075] Furthermore, when the total number of edge points is greater than or equal to the quantity threshold and the width of the feature edge is less than the width threshold, the camera's defocus detection result is determined to be that there is no defocus. This is to give the conclusion that there is no defocus when there are many feature edge points and the edge width of the image is narrow, so that the detection result matches the state of focus and ensures the accuracy of the result.

[0076] Similarly, when the total number of edge points is greater than or equal to the number threshold, and the width of the feature edge is greater than or equal to the width threshold, the camera's defocus detection result is determined to be defocused. This is to give the conclusion that defocused exists when there are many feature edge points and the image edge width is wide, so that the detection result matches the defocused state and ensures the accuracy of the result.

[0077] In this embodiment, based on the number of pixels corresponding to different gradients, a gradient threshold for the image to be detected is selected from all gradients to ensure that the gradient threshold is adapted to the scene of the image to be detected. Pixels with gradients greater than or equal to the gradient threshold are obtained as pixels to be analyzed. Based on the pixel value of the pixel to be analyzed, an adjustment ratio adapted to the pixel value is adaptively set for the pixel to be analyzed, thereby adjusting the pixel value of the pixel to be analyzed according to the adjustment ratio. The adjusted pixel value is used as the jump threshold corresponding to the pixel to be analyzed. Based on the pixel value and jump threshold corresponding to every two adjacent pixels in a preset direction of the pixel to be analyzed, the edge pixels of the pixel to be analyzed in the preset direction are determined. Then, using the jump threshold adapted to the scene, pixels whose pixel value changes match the jump threshold are found in the preset direction to obtain the edge pixels, and the edge width between two edge pixels in the preset direction is determined. The algorithm obtains the width ratio between the horizontal and vertical edge widths of the pixels to be analyzed, filters out the pixels whose width ratios meet the preset ratio conditions, and uses the filtered pixels as feature edge points to facilitate the selection of feature edge points that match the blurred target object edge. The algorithm uses the final edge width of all feature edge points to detect whether the camera is out of focus, thereby ensuring the accuracy of the out-of-focus detection results.

[0078] Please see Figure 3 , Figure 3 This is a schematic diagram of an embodiment of the electronic device of this application. The electronic device 30 includes a memory 301 and a processor 302 coupled to each other. The memory 301 stores program data (not shown in the figure). The processor 302 calls the program data to implement the method in any of the above embodiments. For the description of the relevant content, please refer to the detailed description of the above method embodiments, which will not be repeated here.

[0079] Please see Figure 4 , Figure 4 This is a schematic diagram of a computer-readable storage medium according to an embodiment of the present application. The computer-readable storage medium 40 stores program data 400. When the program data 400 is executed by a processor, it implements the method in any of the above embodiments. For a detailed description of the relevant content, please refer to the detailed description of the above method embodiments, which will not be repeated here.

[0080] It should be noted that the units described as separate components may or may not be physically separate, and 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 according to 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] The above description is merely an embodiment of this application and does not limit the scope of protection of this application. Any equivalent structural or procedural transformations made based on the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the scope of protection of this application.

Claims

1. A method for detecting camera defocus, characterized in that, The method includes: Acquire the image to be detected captured by the camera, and determine the gradient corresponding to each pixel in the image to be detected; A subset of pixels are selected from the image to be detected using the gradient of each pixel, and these pixels are used as pixels to be analyzed. A transition threshold that matches the pixel value of the pixels to be analyzed is then obtained. Based on the pixel value corresponding to every two adjacent pixels in the preset direction of the pixel to be analyzed and the transition threshold, the edge pixels of the pixel to be analyzed in the preset direction are obtained, and the edge width between the edge pixels in the preset direction is determined. The defocus detection result of the camera is determined based on the edge width corresponding to the pixel to be analyzed.

2. The camera defocus detection method according to claim 1, characterized in that, The step of acquiring the image to be detected captured by the camera and determining the gradient corresponding to each pixel in the image to be detected includes: Acquire the image to be detected captured by the camera, and determine the pixel value of each pixel in the image to be detected; The pixels in the image to be detected are traversed, and the gradient corresponding to each pixel is determined based on the pixel value of the adjacent pixels in a preset direction of each pixel in the image to be detected.

3. The camera defocus detection method according to claim 1, characterized in that, The step of filtering a subset of pixels from the image to be detected using the gradient of each pixel, and obtaining a transition threshold that matches the pixel value of the pixels to be analyzed, includes: The number of pixels corresponding to each gradient is obtained. Based on the number of pixels corresponding to different gradients, the gradient threshold of the image to be detected is determined. Pixels whose gradient reaches the gradient threshold are taken as the pixels to be analyzed. Based on the pixel value of the pixel to be analyzed, the adjustment ratio of the pixel to be analyzed is determined, and the pixel value of the pixel to be analyzed is adjusted according to the adjustment ratio to obtain the jump threshold corresponding to the pixel to be analyzed.

4. The camera defocus detection method according to claim 3, characterized in that, The step of obtaining the number of pixels corresponding to each gradient, determining the gradient threshold of the image to be detected based on the number of pixels corresponding to different gradients, and taking the pixels whose gradient reaches the gradient threshold as the pixels to be analyzed includes: Sort all gradients corresponding to the image to be detected from high to low, and obtain the number of pixels corresponding to each gradient. Based on the number of pixels corresponding to the top-ranked gradients, the gradient whose total number of pixels reaches a preset number is obtained and used as the gradient threshold. Pixels whose gradient reaches the gradient threshold and is greater than the preset gradient are selected as the pixels to be analyzed.

5. The camera defocus detection method according to claim 1, characterized in that, The step of obtaining the edge pixels of the pixel to be analyzed in the preset direction based on the pixel values ​​corresponding to every two adjacent pixels in the preset direction and the transition threshold, and determining the edge width between the edge pixels in the preset direction, includes: Based on the pixel values ​​corresponding to the pixel to be analyzed and the adjacent pixels on any side of the preset direction, the trend of pixel value change in the preset direction is determined. In the preset direction, according to the changing trend, it is determined whether the difference between the pixel values ​​of every two adjacent pixels reaches the jump threshold, until the jump threshold is not reached, thus obtaining the edge pixel points in the preset direction; The pixel distance between the edge pixels at both ends of the preset direction is taken as the edge width in the preset direction.

6. The camera defocus detection method according to claim 1, characterized in that, The preset direction includes a horizontal direction and a vertical direction, and the horizontal direction and the vertical direction each correspond to an edge width. The final edge width of the pixel to be analyzed is the larger of the two values. The step of determining the camera's defocus detection result based on the edge width corresponding to the pixel to be analyzed includes: Obtain the width ratio between the horizontal and vertical edge widths of the pixel to be analyzed, and take the pixel to be analyzed whose width ratio satisfies the preset ratio condition as the feature edge point; The defocus detection result of the camera is determined based on the final edge width of all the described feature edge points.

7. The camera defocus detection method according to claim 6, characterized in that, The process of determining the camera's defocus detection result based on the final edge width of all the feature edge points includes: Obtain the total number of edge points of all the aforementioned feature edge points, and based on the final edge width of all the aforementioned feature edge points and the total number of edge points, obtain the feature edge width of the image to be detected; The defocus detection result of the camera is determined based on the total number of edge points and the width of the feature edge.

8. The camera defocus detection method according to claim 7, characterized in that, The process of determining the camera's defocus detection result based on the total number of edge points and the width of the feature edge includes: In response to the total number of edge points being less than a quantity threshold, the camera's defocus detection result is determined to be defocused. In response to the total number of edge points being greater than or equal to a quantity threshold, and the width of the feature edge being less than a width threshold, the camera's defocus detection result is determined to be no defocus. In response to the total number of edge points being greater than or equal to a quantity threshold, and the width of the feature edge being greater than or equal to a width threshold, the camera's defocus detection result is determined to be defocused.

9. An electronic device, characterized in that, include: A memory and a processor are coupled to each other, wherein the memory stores program data, and the processor invokes the program data to perform the method as described in any one of claims 1-8.

10. A computer-readable storage medium storing program data thereon, characterized in that, When the program data is executed by the processor, it implements the method as described in any one of claims 1-8.