An image focusing accuracy evaluation method and system
By extracting image edge lines and gradient information, the detection frame is constructed to calculate the clarity, which solves the problem of inaccurate judgment of focus accuracy in the prior art, and achieves fast and accurate image focusing evaluation.
Patent Information
- Application Number
- CN202210522635.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-13
- Publication Date
- 2025-08-05
- Estimated Expiration
- 2042-05-13
AI Technical Summary
In the prior art, it is relatively reliable to judge the accuracy of image focus and requires a lot of manpower, resulting in high detection costs.
By obtaining the image to be evaluated and preprocessing, extracting edge lines and gradient information, setting the sampling ratio or number of sampling points, uniformly sampling the edge lines, building a detection box, calculating edge clarity, and evaluating the focus effect.
It improves the accuracy of image focus accuracy evaluation, reduces the amount of calculation during the detection process, guides the focus process in real time, and is suitable for a variety of detection scenarios.
Smart Images

Figure CN114972084B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of machine vision, and in particular to a method and system for evaluating image focusing accuracy. Background Art
[0002] Machine vision-assisted industrial component measurement requires a clear image. The imaging surface of the target being observed may not necessarily coincide with the camera's photosensitive surface. To obtain a clear image, the imaging surface must be aligned with the photosensitive surface, effectively focusing the image. The better the focus, the clearer the edges of the target image, and the more consistent they are with the actual object's edge position. This results in more accurate results for measurement, edge detection, and other applications. Otherwise, the edges of the object in the image will appear blurred, making it impossible to accurately locate the target's edge, impacting machine vision inspection accuracy.
[0003] In existing image detection technologies, most of them magnify the edge position in the image and manually judge whether the focus is accurate by the naked eye. Due to the lack of quantitative data, the reliability of using existing methods to judge the focus accuracy is low and requires a lot of manpower, resulting in high detection costs. Summary of the Invention
[0004] The purpose of the present invention is to provide an image focus accuracy evaluation method to solve the problems that the existing methods for judging focus accuracy have low reliability and require a lot of manpower, resulting in high detection costs.
[0005] In one aspect, an embodiment of the present invention provides a method for evaluating image focus accuracy, comprising the following steps:
[0006] S1, obtaining the image to be evaluated and preprocessing it to obtain the grayscale image to be evaluated;
[0007] S2, extracting the edge line of the grayscale image to be evaluated and the gradient information of each pixel on the edge line;
[0008] The S2 further comprises the following steps:
[0009] S2-1, extracting edge lines of the grayscale image to be evaluated obtained in S1 based on an edge detection operator;
[0010] S2-2, performing noise removal processing on the edge line to obtain the edge of the target object;
[0011] S2-3, calculating the change gradients DX and DY of each pixel of the grayscale image to be evaluated in the horizontal and vertical directions based on the edge detection operator;
[0012] S2-4, combining the edge of the target object obtained in S2-2 and the change gradients DX and DY obtained in S2-3 to calculate the gradient direction of each pixel on the edge line;
[0013] S3, set the sampling ratio or the number of sampling points, uniformly sample the edge line, and obtain sampling points;
[0014] S4, obtaining a detection frame for each sampling point based on the gradient information;
[0015] S4 further includes: based on the edge line and its gradient direction obtained in S2, extending L pixels inward and outward of the sampling point along the gradient direction, and extending W pixels on both sides of the gradient line in a direction perpendicular to the gradient direction, to construct a detection frame consisting of W*L pixels, where the length of the detection frame is L and the width is W;
[0016] S5, calculating the edge clarity of the detection frame of each sampling point;
[0017] S6, calculating the edge clarity of the entire image based on the edge clarity of the detection frame at each sampling point, and evaluating the focusing effect based on the edge clarity of the entire image;
[0018] Calculating the edge clarity of the entire image includes: calculating the average of the edge clarity of the detection frame of each sampling point as the edge clarity of the entire image.
[0019] Furthermore, in S1, the image to be evaluated is obtained by photographing the target object with a camera, and if the photographed image to be evaluated is a color image, it is converted into a grayscale image through preprocessing.
[0020] Furthermore, in S2-1, the edge detection operator used is the Canny operator; and in S2-3, the edge detection operator used is the Sobel operator.
[0021] Furthermore, in S3, the sampling ratio ranges from 0.01 to 0.3; the number of sampling points satisfies: number of sampling points = sampling ratio × number of edge points, and the number of sampling points is 3 or more.
[0022] Furthermore, the length L is greater than or equal to 3, and L is less than or equal to 1 / 4 of the length of the short side of the image; the width W is greater than or equal to 3, and less than 1 / sampling ratio.
[0023] Furthermore, the S5 further includes the following steps:
[0024] S5-1, obtain the maximum value V of the grayscale of each pixel in the detection frame max and minimum value V min , and calculate the grayscale difference H of the detection frame, H = V max -V min ;
[0025] S5-2, calculate the grayscale median value in the detection frame
[0026] S5-3, calculate the offset ΔV between the grayscale of each pixel in the detection frame and the median value j , the calculation formula is:
[0027]
[0028] Where, ΔV j Indicates the offset of the j-th pixel, V j represents the gray value of the j-th pixel;
[0029] S5-4, the offset is integrated and normalized to obtain the edge clarity D of each detection frame, which is calculated as follows:
[0030]
[0031] Furthermore, the calculation of the edge clarity of the entire image in S6 includes: calculating the average of the edge clarity of the detection boxes of each sampling point as the edge clarity of the entire image, and the calculation formula is as follows:
[0032]
[0033] in, is the edge clarity of the whole image, N is the number of sampling points, D i Indicates the edge clarity of the detection box corresponding to the i-th sampling point.
[0034] On the other hand, an embodiment of the present invention further provides an image focus accuracy evaluation system, comprising an acquisition module, a gradient extraction module, a sampling module, a detection frame acquisition module, a calculation module, and an evaluation module;
[0035] Among them, the acquisition module is used to collect images of the target object and perform preprocessing to obtain the grayscale image to be evaluated; the gradient extraction module is used to obtain the gradient direction of each pixel on the edge line of the image to be evaluated; the sampling module can sample the edge line and obtain sampling points; the detection frame acquisition module can combine the sampling points and gradient information to obtain the detection frame of each sampling point; the calculation module is used to calculate the edge clarity of the detection frame of each sampling point and the edge clarity of the entire image; the evaluation module can judge the focus effect of the acquired image based on the edge clarity of the entire image;
[0036] The gradient extraction module obtains the gradient direction of each pixel on the edge line of the image to be evaluated, specifically comprising: extracting the edge line of the grayscale image to be evaluated based on an edge detection operator; performing noise screening on the edge line to obtain the edge of the target object; calculating the change gradients DX and DY of each pixel in the grayscale image to be evaluated in the horizontal and vertical directions based on the edge detection operator; and calculating the gradient direction of each pixel on the edge line based on the obtained edge of the target object and the change gradients DX and DY.
[0037] The detection frame acquisition module acquires the detection frame of each sampling point by: expanding L pixels inward and outward of the sampling point along the gradient direction according to the edge line and its gradient direction, and expanding W pixels on both sides of the gradient line in a direction perpendicular to the gradient direction, thereby constructing a detection frame consisting of W*L pixels, where the length of the detection frame is L and the width is W;
[0038] The calculation module calculates the edge clarity of the entire image, specifically including: calculating the average of the edge clarity of the detection frame of each sampling point as the edge clarity of the entire image.
[0039] The beneficial effects of the present invention are as follows: the image focus accuracy evaluation method proposed in the present invention calculates the clarity of the image to be detected based on edge detection, gradient change and grayscale change sharpness, thereby improving the accuracy of image evaluation; through edge detection and uniform sampling on the edge line, the total amount of calculation in the detection process can be reduced, the calculation speed can be accelerated, and the focusing process can be guided in real time; by selecting a certain range as the detection frame according to the gradient frame around the sampling point and calculating the grayscale statistics in the detection frame, the interference of noise points can be effectively eliminated, making the detection results more reliable, and this method can be applied to detection in a variety of scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work. In the drawings:
[0041] Figure 1 Schematic diagram of the overall process of the image focus accuracy evaluation method provided in an embodiment of the present invention;
[0042] Figure 2 Schematic diagram of edge point sampling positions in an embodiment of the present invention;
[0043] Figure 3 Schematic diagram of the projection distribution of pixel grayscale in the gradient direction in the detection frame according to an embodiment of the present invention;
[0044] Figure 4 Schematic diagram of the projection distribution of pixel grayscale in the gradient direction in an ideal detection frame in an embodiment of the present invention;
[0045] Figure 5 Schematic diagram of the overall structure of the image focus accuracy evaluation system provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0046] To make the purpose, technical solutions and advantages of the embodiments of the present invention more clear, the embodiments of the present invention are further described in detail below with reference to the accompanying drawings. Here, the exemplary embodiments of the present invention and their descriptions are used to explain the present invention, but are not intended to limit the present invention.
[0047] In the description of this specification, the terms "include", "including", "have", "contain", etc. are all open terms, which mean including but not limited to. The descriptions with reference to the terms "one embodiment", "a specific embodiment", "some embodiments", "for example", etc. mean that the specific features, structures or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic expressions of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures or characteristics described can be combined in a suitable manner in any one or more embodiments or examples. The order of steps involved in each embodiment is used to schematically illustrate the implementation of the present application, and the order of steps therein is not limited and can be appropriately adjusted as needed.
[0048] An embodiment of the present invention provides a method for evaluating image focus accuracy. Figure 1 This is a schematic diagram of the overall process of the image focus accuracy evaluation method. Figure 1 As shown, the method specifically includes the following steps:
[0049] S1, obtaining an image of the target object to be evaluated and preprocessing it to obtain a grayscale image to be evaluated;
[0050] The image to be evaluated is obtained by photographing the target object with a camera, and if the photographed image to be evaluated is a color image, it is converted into a grayscale image through preprocessing.
[0051] Specifically, the camera used in this embodiment is a CCD camera; the preprocessing method includes a weighted method and an average method, and the weighted method is preferably used in this embodiment.
[0052] S2, extracting the edge line of the grayscale image to be evaluated and the gradient information of each pixel on the edge line;
[0053] The accuracy of image focus is primarily reflected at the edges of the object image. Because the grayscale changes at edges are larger, they better reflect image clarity. However, the grayscale changes in the block areas of the image are more gradual and less valuable for clarity evaluation. Therefore, the present invention uses an edge detection algorithm to extract edge information from the image to be evaluated. The resulting results can significantly reduce the amount of image data required for processing, thereby filtering out a large amount of unnecessary information and reducing the computational effort required for image accuracy testing.
[0054] Specifically, S2 further includes the following steps:
[0055] S2-1, extracting edge lines of the grayscale image to be evaluated obtained in S1 based on an edge detection operator;
[0056] In this embodiment, the edge detection operator used to extract the edge lines of the grayscale image to be evaluated is the Canny operator.
[0057] S2-2, performing noise removal processing on the edge line to obtain the edge of the target object;
[0058] According to the characteristics of the target object, the edges of the target object are filtered out from the edges obtained in S2-1. For example, various closed contours in the entire image are obtained through connectivity analysis, and noise points are filtered out. The noise point filtering process includes breakpoint connection and length filtering.
[0059] S2-3, calculating the change gradients DX and DY of each pixel of the grayscale image to be evaluated in the horizontal and vertical directions based on the edge detection operator;
[0060] Among them, in this embodiment, the edge detection operator used to calculate the change gradient of each pixel of the grayscale image to be evaluated is the Sobel operator, that is, two sets of 3×3 masks are used, one in the horizontal direction and the other in the vertical direction, and they are planar convolved with the image respectively to obtain the change gradient in the horizontal and vertical directions respectively.
[0061] S2-4, combining the edge of the target object obtained in S2-2 and the change gradients DX and DY obtained in S2-3 to calculate the gradient direction of each pixel on the edge line.
[0062] Specifically, the gradient direction is perpendicular to the tangent line of the edge line, and the gradient direction calculation formula is:
[0063]
[0064] Among them, G i Indicates the gradient direction of the i-th pixel, DX i and DY i They represent the horizontal and vertical gradients of the i-th pixel respectively.
[0065] S3, set the sampling ratio or the number of sampling points, uniformly sample the edge line, and obtain sampling points;
[0066] Specifically, the edge lines of the grayscale image to be evaluated obtained in S2-1 are sampled according to a set sampling ratio or number of sampling points to obtain N sampling points.
[0067] In this embodiment, the sampling ratio or the number of sampling points can be set. The sampling ratio ranges from 0.01 to 0.3, and is preferably 0.05. The number of sampling points satisfies: number of sampling points = sampling ratio × number of edge points, and the number of sampling points is 3 or more.
[0068] S4, obtaining a detection frame for each sampling point based on the gradient information;
[0069] S4 further includes: based on the edge line and its gradient direction obtained in S2, extending L pixels inward and outward of the sampling point along the gradient direction, and extending W pixels on both sides of the gradient line in a direction perpendicular to the gradient direction, to construct a detection frame consisting of W*L pixels. The gradient line is a line segment along the gradient direction with a length of L pixels and centered at the edge line sampling point.
[0070] Specifically, refer to Figure 3 As shown in FIG, the gradient direction is perpendicular to the tangent direction of the edge line and points from dark to light, that is, from the side with small grayscale to the side with large grayscale.
[0071] Those skilled in the art should understand that the size of the constructed detection frame can be adjusted according to the size of the image. Specifically, the length L should satisfy that L is greater than 3 and less than 1 / 4 of the length of the short side of the image; the width W should be an odd number and satisfy that W is greater than 3 and less than 1 / sampling ratio.
[0072] Furthermore, the pixel length L is preferably set to 12, and the width W is preferably set to 5. Figure 3 When the pixel length L is 12 and the width W is 5, a detection box consisting of 12*5 pixels is constructed.
[0073] S5, calculating the edge clarity of the detection frame of each sampling point;
[0074] Specifically, S5 further includes the following steps:
[0075] S5-1, obtain the maximum value V of the grayscale of each pixel in the detection frame max and minimum value V min , and calculate the grayscale difference H of the detection frame, H = V max -V min ;
[0076] S5-2, calculate the grayscale median value in the detection frame
[0077] S5-3, calculate the offset ΔV between the grayscale of each pixel in the detection frame and the median value j , the calculation formula is:
[0078]
[0079] Where, ΔV j Indicates the offset of the j-th pixel, V j represents the gray value of the j-th pixel;
[0080] S5-4: The offsets are combined and normalized to obtain edge definition D of each detection frame. Under ideal definition, that is, when definition D=1, the offset of each pixel should be H / 2. In this embodiment, H / 2 is used for normalization. The edge definition D is calculated as follows:
[0081]
[0082] S6, calculating the edge clarity of the entire image based on the edge clarity of the detection frame at each sampling point, and evaluating the focusing effect based on the edge clarity of the entire image;
[0083] Specifically, calculating the edge clarity of the entire image in S6 includes calculating the average of the edge clarity of the detection boxes of each sampling point as the edge clarity of the entire image, and the calculation formula is as follows:
[0084]
[0085] in, is the edge clarity of the whole image, N is the number of sampling points, D i Indicates the edge clarity of the detection box corresponding to the i-th sampling point.
[0086] Furthermore, the edge clarity of the entire image is calculated The range is [0,1] and is determined by the edge clarity of the entire image. The size of the image is used to evaluate the focus effect. In this embodiment, the edge clarity of the whole image is The closer the value is to 1, the clearer the captured image is and the better the focusing effect is.
[0087] Specifically, when the edge clarity of the whole image When the value of is greater than 0.8, the image is considered clear and the focus effect is good;
[0088] When the edge clarity of the whole image When the value is 0.5 to 0.8, the image clarity and focus are considered average, and the image should be recaptured.
[0089] When the edge clarity of the whole image When the value is less than 0.5, it means that the detection process is wrong and the detection should be repeated.
[0090] like Figure 5 As shown, the present invention also provides an image focus accuracy evaluation system, and the above-mentioned image focus accuracy evaluation method can be implemented by this system. Specifically, the system includes an acquisition module, a gradient extraction module, a sampling module, a detection frame acquisition module, a calculation module and an evaluation module;
[0091] Among them, the acquisition module is used to capture and preprocess the image of the target object to obtain the grayscale image to be evaluated; the gradient extraction module is used to obtain the gradient direction of each pixel on the edge line of the image to be evaluated; the sampling module can sample the edge line and obtain the sampling points; the detection frame acquisition module can combine the sampling points and gradient information to obtain the detection frame of each sampling point; the calculation module is used to calculate the edge clarity of the detection frame of each sampling point and the edge clarity of the entire image; the evaluation module can judge the focus effect of the acquired image based on the edge clarity of the entire image.
[0092] It will be understood by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Thus, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0093] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowcharts and / or block diagrams. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0094] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0095] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0096] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for evaluating image focus accuracy, characterized in that: The following steps are involved: S1, obtaining the image to be evaluated and preprocessing it to obtain the grayscale image to be evaluated; S2, extracting the edge line of the grayscale image to be evaluated and the gradient information of each pixel on the edge line; The S2 further comprises the following steps: S2-1, extracting edge lines of the grayscale image to be evaluated obtained in S1 based on an edge detection operator; S2-2, performing noise removal processing on the edge line to obtain the edge of the target object; S2-3, calculating the change gradients DX and DY of each pixel of the grayscale image to be evaluated in the horizontal and vertical directions based on the edge detection operator; S2-4, combining the edge of the target object obtained in S2-2 and the change gradients DX and DY obtained in S2-3 to calculate the gradient direction of each pixel on the edge line; S3, set the sampling ratio or the number of sampling points, uniformly sample the edge line, and obtain sampling points; S4, obtaining a detection frame for each sampling point based on the gradient information; S4 further includes: based on the edge line and its gradient direction obtained in S2, extending L pixels inward and outward of the sampling point along the gradient direction, and extending W pixels on both sides of the gradient line in a direction perpendicular to the gradient direction, to construct a detection frame consisting of W*L pixels, where the length of the detection frame is L and the width is W; S5, calculating the edge clarity of the detection frame of each sampling point; S6, calculating the edge clarity of the entire image based on the edge clarity of the detection frame at each sampling point, and evaluating the focusing effect based on the edge clarity of the entire image; Calculating the edge clarity of the entire image includes calculating the average of the edge clarity of the detection boxes of each sampling point as the edge clarity of the entire image.
2. The image focusing accuracy evaluation method according to claim 1, wherein: In S1, the image to be evaluated is obtained by photographing the target object with a camera, and if the photographed image to be evaluated is a color image, it is converted into a grayscale image through preprocessing.
3. The image focusing accuracy evaluation method according to claim 1, wherein: In S2-1, the edge detection operator used is the Canny operator; in S2-3, the edge detection operator used is the Sobel operator.
4. The image focusing accuracy evaluation method according to claim 3, wherein: In S3, the sampling ratio ranges from 0.01 to 0.3; the number of sampling points satisfies: number of sampling points = sampling ratio × number of edge points, and the number of sampling points is 3 or more.
5. The image focusing accuracy evaluation method according to claim 4, wherein: The length L is greater than or equal to 3, and L is less than or equal to 1 / 4 of the length of the short side of the image; the width W is greater than or equal to 3, and less than 1 / sampling ratio.
6. The image focusing accuracy evaluation method according to claim 5, wherein: The S5 further comprises the following steps: S5-1, obtain the maximum value V of the grayscale of each pixel in the detection frame max and minimum value V min , and calculate the grayscale difference H of the detection frame, H = V max -V min ; S5-2, calculate the grayscale median value in the detection frame S5-3, calculate the offset ΔV between the grayscale of each pixel in the detection frame and the median value j , the calculation formula is: Where, ΔV j Indicates the offset of the j-th pixel, V j represents the gray value of the j-th pixel; S5-4, the offset is integrated and normalized to obtain the edge clarity D of each detection frame, which is calculated as follows:
7. The image focusing accuracy evaluation method according to claim 6, wherein: Calculating the edge clarity of the entire image in S6 includes calculating the average of the edge clarity of the detection frames of each sampling point as the edge clarity of the entire image, and the calculation formula is as follows: in, is the edge clarity of the whole image, N is the number of sampling points, D i Indicates the edge clarity of the detection box corresponding to the i-th sampling point.
8. An image focus accuracy evaluation system, characterized in that: It includes acquisition module, gradient extraction module, sampling module, detection frame acquisition module, calculation module and evaluation module; Among them, the acquisition module is used to collect images of the target object and perform preprocessing to obtain the grayscale image to be evaluated; the gradient extraction module is used to obtain the gradient direction of each pixel on the edge line of the image to be evaluated; the sampling module can sample the edge line and obtain sampling points; the detection frame acquisition module can combine the sampling points and gradient information to obtain the detection frame of each sampling point; the calculation module is used to calculate the edge clarity of the detection frame of each sampling point and the edge clarity of the entire image; the evaluation module can judge the focus effect of the acquired image based on the edge clarity of the entire image; The gradient extraction module obtains the gradient direction of each pixel on the edge line of the image to be evaluated, specifically comprising: extracting the edge line of the grayscale image to be evaluated based on an edge detection operator; performing noise screening on the edge line to obtain the edge of the target object; calculating the change gradients DX and DY of each pixel in the grayscale image to be evaluated in the horizontal and vertical directions based on the edge detection operator; and calculating the gradient direction of each pixel on the edge line based on the obtained edge of the target object and the change gradients DX and DY. The detection frame acquisition module acquires the detection frame of each sampling point by: expanding L pixels inward and outward of the sampling point along the gradient direction according to the edge line and its gradient direction, and expanding W pixels on both sides of the gradient line in a direction perpendicular to the gradient direction, thereby constructing a detection frame consisting of W*L pixels, where the length of the detection frame is L and the width is W; The calculation module calculates the edge clarity of the entire image, specifically including: calculating the average of the edge clarity of the detection frame of each sampling point as the edge clarity of the entire image.
Citation Information
Patent Citations
Image definition detecting method and device
CN103793918A
Image definition evaluation method and device
CN110717922A