Circle edge detection algorithm for perforated images based on local pixel value statistical threshold
By adopting a method based on local pixel value statistics threshold in the edge detection algorithm, combined with the image processing function of opencv2 software, the problem of low accuracy of edge detection of laser processing holes is solved, and higher detection accuracy and adaptability are achieved.
Patent Information
- Application Number
- CN202411874629.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2044-12-19
AI Technical Summary
The existing edge detection algorithm has low accuracy in hole edge detection after laser processing, especially in the case of pixel value gradient changes and poor connectivity, resulting in a large difference between the detected contour and the real contour.
The punched image circle edge detection algorithm based on the local pixel value statistical threshold is adopted. By converting the image into a grayscale map and performing reverse operation, combined with the expansion, corrosion and denoising processing of the opencv2 software, the maximum contour is extracted and the statistical threshold is calculated, and the weighted calculation of the local pixel value is performed to improve the accuracy of the threshold.
It improves the accuracy of hole edge detection, reduces manual recognition errors, enhances the adaptability and robustness of the detection, and can more accurately extract the edge profile of the laser-processed hole.
Smart Images

Figure CN119313695B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to edge extraction technology, and in particular to a punched image circle edge detection algorithm based on local pixel value statistical threshold. Background Art
[0002] Laser processing has irreplaceable advantages in equipment processing due to its precise and controllable characteristics. However, due to the processing results of laser, the scale is as small as μm level, which cannot be measured by conventional measurement tools. The basic method is to take pictures of the hole processing effect through an electron microscope, and count the number of pixels and the scale relationship of the pixel size. After a series of proportional conversions, the effect of indirect measurement is achieved.
[0003] However, due to the special characteristics of laser energy being strong inside and weak outside during laser processing and the diversity of processing material properties, a certain burning and ablation effect will be caused at the edge of the hole, making the contour boundary of the processed circular hole relatively blurred. The diversity of shooting environment conditions makes the gradient of image pixel values on the contour boundary and the connectivity of pixel blocks poor. This leads to poor prediction results of some edge detection algorithms based on pixel value gradient changes, such as Canny and Sobel, and Laplacian algorithms. At the same time, a large number of extreme values appear in the pixel values in the central area of the laser drilling effect image, which brings strong detection interference to threshold filtering methods based on global statistics, such as the OTSU method, resulting in a large deviation in the calculated threshold, and the background noise is large, which brings great difficulties to general edge detection algorithms. The detected contour is quite different from the real contour, and the adaptability to various drilling effects is poor. It brings great difficulties to the edge detection of laser-processed holes. Summary of the invention
[0004] In view of the above-mentioned deficiencies in the prior art, the punch image circle edge detection algorithm based on local pixel value statistical threshold provided by the present invention solves the problem of low accuracy of the existing edge extraction method when extracting the edges of laser-processed holes.
[0005] In order to achieve the above-mentioned object of the invention, the technical solution adopted by the present invention is:
[0006] Provided is a punched image circle edge detection algorithm based on local pixel value statistical threshold, which includes the steps of:
[0007] S1, converting the punched image into a grayscale image, and performing a reverse operation on the grayscale value of the grayscale image to obtain a reverse image;
[0008] S2. Binarize the reverse image according to a preset pixel range to obtain a binary image A, and extract the maximum contour A and the center position A of the maximum contour A in the binary image A using opencv2 software;
[0009] S3, counting the number of pixel points in the binary image A whose pixel values are greater than the first threshold, converting the counted number of pixel points into the area of a weighted equivalent circle, and calculating the equivalent radius A of the weighted equivalent circle;
[0010] S4, counting the pixel values inside a circle drawn with the center position A as the circle center and the equivalent radius A as the radius, and using reverse weighted calculation of the statistical threshold A according to the pixel values in ascending order within the circle;
[0011] S5. Binarize the reverse image according to the statistical threshold A to obtain a binary image B, and extract the maximum contour B and the center position B of the maximum contour B in the binary image B using opencv2 software;
[0012] S6, binarizing the maximum contour area B, and performing a reverse operation to obtain a first mask; converting the pixel points of the maximum contour area B into the area of an equivalent circle, and calculating an equivalent radius B of the equivalent circle;
[0013] S7, taking the center position B as the center and the preset multiple of the equivalent radius B as the radius to obtain a circle B, and performing binarization processing on the part outside the circle B area to obtain a second mask;
[0014] S8, overlapping the first mask and the second mask, performing inverse calculation on the pixel values of the circular area to obtain a statistical area, and calculating a statistical threshold B by inverse weighting according to the ascending pixel values in the statistical area;
[0015] S9. According to the statistical threshold value B, the reverse image is binarized to obtain a binarized image C. The maximum contour C in the binarized image C is extracted using opencv2 software as the edge contour of the hole in the punched image.
[0016] Furthermore, the method of extracting the maximum contour in step S2, step S5 and step S9 is:
[0017] The dilation function, erosion function and denoising function in the opencv2 software are used to process the binary image, and then the contour recognition function of opencv2 is used to identify the contour to obtain the maximum contour in the binary image;
[0018] The method for extracting the center position of the maximum contour in step S2 and step S5 is: using the center extraction function in the opencv2 software to extract the center position of the maximum contour.
[0019] The beneficial effects of the above technical solution are: the existing opencv2 software can be used to roughly locate the geometric center of the contour. The geometric center is needed in S2 to determine the center position of the approximate extreme value area, which is convenient for determining the local area determined in the corresponding contour A. The center position of the inner contour mask is determined in S5, which helps to highlight the statistical area of the circular ring and has a better difference distribution of the pixel values of the hole wall and the pixel values of the outer plane.
[0020] Furthermore, when the maximum contour is extracted in step S2, the kernel of the dilation function is 3*3, the kernel of the erosion function is 3*3, the kernel of the denoising function is 5*5, the dilation and erosion are iterated once, and the denoising is performed once;
[0021] When the maximum contour is extracted in step S9, the core of the dilation function is 5*5, and the dilation is iterated twice; the core of the erosion function is 4*4, and the erosion is iterated four times; the core of the denoising function is 8*8, and the denoising is performed once.
[0022] The beneficial effects of the above technical solution are as follows: some small pixel blocks that meet the threshold requirements but are slightly closer to the maximum contour are connected to the maximum contour through the expansion operation. Erosion is to shrink the contour edge of the largest color block to a certain extent, so that the edge is clearer and more in line with the actual hole edge characteristics. Denoising is to remove the tiny color block noise points in the image that are too far away from the maximum contour. The selection of the kernel is to comprehensively analyze the shooting environment and the characteristics of the hole, and try to determine the kernel size with strong applicability.
[0023] Further, according to the preset pixel range, the method of performing binarization processing on the reverse image is: setting the pixel values in the reverse image within the preset pixel range to 255, and setting the remaining pixel values to 0;
[0024] According to the statistical threshold A, the method of binarizing the reverse image is as follows: the pixel values in the reverse image that are greater than the statistical threshold A are set to 255, and the remaining pixel values are set to 0;
[0025] According to the statistical threshold B, the method for binarizing the reverse image is as follows: the pixel values in the reverse image that are greater than the statistical threshold B are set to 255, and the remaining pixel values are set to 0.
[0026] The beneficial effects of the above technical solution are as follows: in terms of statistical threshold A, binarization facilitates the counting of the number of points that meet the threshold. In terms of statistical threshold B, it is convenient to use the contour extraction function of the opencv2 software to accurately extract the corresponding contour.
[0027] Furthermore, the calculation formula for adding the equivalent radius A in step S3 is:
[0028] ,
[0029] in, is the equivalent radius A; is the area of the weighted equivalent circle of the maximum contour A; is the number of pixels counted in the maximum contour A;
[0030] The calculation formulas for the equivalent radius B in step S6 are:
[0031] ,
[0032] in, is the equivalent radius B; is the area of the equivalent circle corresponding to the maximum contour B; is the number of pixels counted in the maximum contour B.
[0033] The beneficial effects of the above technical solution are: the quantitative statistics of A are through a rough threshold of the entire image, which is not very accurate, but the extreme value area can be roughly extracted through the rough threshold, which helps to perform local statistics on the pixel values of the pixel points in the extreme value area, and then more accurately determine the threshold that meets the extreme value area.
[0034] Furthermore, the expressions of the statistical threshold A and the statistical threshold B calculated by reverse weighting are both:
[0035] ,
[0036] ,
[0037] in, is statistical threshold A or statistical threshold B; w is the weight allocation rate; P is the quantity split ratio; The first number in ascending order within the circle or statistical area. i pixel value; N is the total number of pixel values within the circle or statistical area; is the mean parameter; and is the intermediate parameter.
[0038] The beneficial effect of the above technical solution is: for the case where the perforated pixel distribution has an extreme distribution, if the normal mean is used to determine the corresponding threshold, the threshold will be inaccurate due to the existence of the extreme value area. Through reverse proportional weighting, the threshold obtained by local statistics can be well adjusted, and the statistical threshold A needs to be larger than the normal mean, which is more in line with the threshold of the contour in the actual extreme value area, while the threshold B needs to be slightly larger than the normal mean or approximately equal to the threshold of the actual outer contour.
[0039] Furthermore, when calculating the statistical threshold A, the corresponding weight allocation rate is 30%, and the quantity segmentation rate is 70%; when calculating the statistical threshold B, the corresponding weight allocation rate is 61.8%, and the quantity segmentation rate is 70%.
[0040] Further, the first threshold is calculated The expression is .
[0041] Furthermore, the expression for converting the punched image into a grayscale image is:
[0042]
[0043] in, V is the gray value; R is the red component; G is the green component; B is the blue component;
[0044] The expression for the reverse operation is:
[0045]
[0046] in, For V The reversed value.
[0047] Furthermore, the punched image circle edge detection algorithm also includes:
[0048] The center position C of the maximum contour C is extracted using opencv2 software;
[0049] Taking the center position C as the starting point, a ray is made for each deflection of the preset angle, and several rays are obtained after one rotation. The intersection of each ray and the maximum contour C is determined to obtain several radii C;
[0050] The average value of all radii C is calculated as the radius D of the outer contour equivalent circle, and the radius D is used as the radius of the hole in the punched image.
[0051] Compared with the prior art, the beneficial effects of the present invention are as follows: when performing edge detection, the present scheme does not require manual calibration of corresponding features, can automatically identify and extract, and improve recognition efficiency, and also does not require manual detection, thereby reducing the error caused by human eye recognition; during the detection process, the threshold is adjusted by performing certain weighted operations based on the number of pixels inside the extracted contour and the distribution characteristics of the pixel values, and a more accurate local statistics is performed on the characteristics of the hole edge, making the threshold more accurate, and thus the accuracy is greatly improved.
[0052] This solution can determine the circularity of the holes obtained by the laser through the edge contours of the holes in the punching image, and adjust the relevant parameters of the laser to make the final holes more round and complete. Through the diameter of the holes in the punching image, it can be further determined whether the diameter of the holes is too large or too small, so that the laser parameters can be adjusted in time to ensure that the holes punched meet the actual required size. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 It is a flowchart of the punched image circle edge detection algorithm based on local pixel value statistical threshold;
[0054] Figure 2 The original image of the punched image that needs edge extraction;
[0055] Figure 3 Visualization of the distribution of grayscale pixel values in the image;
[0056] Figure 4 It is the inverse grayscale image of pixel value;
[0057] Figure 5 To roughly process the binary image;
[0058] Figure 6 Binarized image for dilation and corrosion denoising;
[0059] Figure 7 is a schematic diagram of the maximum contour B;
[0060] Figure 8 is the first mask image;
[0061] Fig. 9 is the second mask image;
[0062] Fig.10 It is the statistical area map of the ring threshold;
[0063] Fig.11 This is the outer contour effect diagram;
[0064] Fig.12 This is a schematic diagram for calculating the inner and outer radius;
[0065] Fig.13 This is the contour effect diagram (the blue circle is the outer contour equivalent circle). DETAILED DESCRIPTION
[0066] The specific implementation modes of the present invention are described below so that those skilled in the art can understand the present invention. However, it should be clear that the present invention is not limited to the scope of the specific implementation modes. For those of ordinary skill in the art, as long as various changes are within the spirit and scope of the present invention as defined and determined by the attached claims, these changes are obvious, and all inventions and creations utilizing the concept of the present invention are protected.
[0067] In this solution, the holes in the punched image are conical holes, and the pixel values of the grayscale image can be observed from the edge of the hole inward, showing a trend of decreasing pixel values inward.
[0068] refer to Figure 1 , Figure 1 FIG. 4 shows a flowchart of a punched image circle edge detection algorithm based on local pixel value statistical threshold; FIG. Figure 1 As shown, the method S includes steps S1 to S9.
[0069] In step S1, a punched image (a schematic diagram of a punched image can be referred to Figure 2 ) is converted into a grayscale image (a schematic diagram of a grayscale image can be found in Figure 3 ), and perform a reverse operation on the grayscale value of the grayscale image to obtain a reverse image (a schematic diagram of the reverse image can be referred to Figure 4 );
[0070] During implementation, the solution preferably converts the punched image into a grayscale image using the expression:
[0071]
[0072] in, V is the gray value; R is the red component; G is the green component; B is the blue component;
[0073] The expression for the reverse operation is:
[0074]
[0075] in, For V The reversed value.
[0076] In step S2, the reverse image is binarized according to the preset pixel range to obtain a binarized image A (a schematic diagram of the binarized image A can be referred to Figure 5), and use opencv2 software to extract the maximum contour A and the center position A of the maximum contour A in the binary image A; according to the preset pixel range, the method of binarizing the reverse image is: setting the pixel values in the reverse image within the preset pixel range to 255, and setting the remaining pixel values to 0; wherein the preset pixel range is 60-255.
[0077] In step S3, the number of pixel points in the binary image A whose pixel values are greater than the first threshold is counted, the counted pixel points are converted into the area of the equivalent circle, and the equivalent radius A of the equivalent circle is calculated; when implemented, this scheme preferably calculates the first threshold The expression is .
[0078] In step S4, the pixel values inside a circle drawn with the center position A as the center and the equivalent radius A as the radius are counted, and a statistical threshold A is calculated by reverse weighting according to the pixel values in ascending order inside the circle;
[0079] In step S5, the reverse image is binarized according to the statistical threshold A to obtain a binarized image B, and the maximum contour B in the binarized image B is extracted using opencv2 software (for details, please refer to Figure 7 ) and the center position B of the maximum contour B; according to the statistical threshold A, the method of binarizing the reverse image is: setting the pixel values in the reverse image that are greater than the statistical threshold A to 255, and setting the remaining pixel values to 0.
[0080] In step S6, the maximum contour area B is binarized, and the first mask is obtained by reverse operation (see Figure 8 ); Convert the pixel points of the maximum contour B area into the area of the equivalent circle, and calculate the equivalent radius B of the equivalent circle;
[0081] During implementation, the calculation formula of the equivalent radius A in the preferred step S3 of this solution is:
[0082] ,
[0083] in, is the equivalent radius A; is the area of the weighted equivalent circle of the maximum contour A; is the number of pixels counted in the maximum contour A;
[0084] The calculation formulas for the equivalent radius B in step S6 are:
[0085] ,
[0086] in, is the equivalent radius B; is the area of the equivalent circle corresponding to the maximum contour B; is the number of pixels counted in the maximum contour B.
[0087] In step S7, a circle B is obtained with the center position B as the center and a preset multiple of the equivalent radius B as the radius, and the portion outside the circle B is binarized to obtain a second mask (reference Fig. 9 ); The preset multiple is preferably 2.5~2.8, and the best value is 2.5.
[0088] In step S8, the first mask and the second mask are overlapped, and the pixel values of the circular area are reversed to obtain the statistical area (refer to Fig.10 ), according to the ascending pixel values in the statistical area, the statistical threshold B is calculated by reverse weighting;
[0089] In one embodiment of the present invention, the expressions of the statistical threshold A and the statistical threshold B calculated by inverse weighting are both:
[0090] ,
[0091] ,
[0092] in, is statistical threshold A or statistical threshold B; w is the weight allocation rate; P is the quantity split ratio; The first number in ascending order within the circle or statistical area. i pixel value; N is the total number of pixel values within the circle or statistical area; is the mean parameter; and is the intermediate parameter.
[0093] Among them, the corresponding weight allocation rate when calculating statistical threshold A is 30%, and the quantity segmentation rate is 70%; the corresponding weight allocation rate when calculating statistical threshold B is 61.8%, and the quantity segmentation rate is 70%.
[0094] In step S9, the reverse image is binarized according to the statistical threshold B to obtain a binarized image C. The maximum contour C in the binarized image C is extracted using opencv2 software (refer to Fig.11 ) as the edge contour of the hole in the punched image. According to the statistical threshold B, the method of binarizing the reverse image is: the pixel values in the reverse image that are greater than the statistical threshold B are set to 255, and the remaining pixel values are set to 0.
[0095] In one embodiment of the present invention, the method of extracting the maximum contour in step S2, step S5 and step S9 is:
[0096] The dilation function, corrosion function and denoising function in opencv2 software are used to process the binary image. The processed image can be referred to Figure 6 , and then use the contour recognition function of opencv2 to identify the contour and obtain the maximum contour in the binary image;
[0097] The method for extracting the center position of the maximum contour in step S2 and step S5 is: using the center extraction function in the opencv2 software to extract the center position of the maximum contour.
[0098] During implementation, in the preferred step S2 of this scheme, when the maximum contour is extracted, the core of the dilation function is 3*3, the core of the erosion function is 3*3, the core of the denoising function is 5*5, the dilation and erosion are iterated once, and denoising is performed once; when the maximum contour is extracted in step S9, the core of the dilation function is 5*5, the dilation is iterated twice; the core of the erosion function is 4*4, the erosion is iterated 4 times; the core of the denoising function is 8*8, and denoising is performed once.
[0099] In one embodiment of the present invention, the punched image circle edge detection algorithm further includes:
[0100] The center position C of the maximum contour C is extracted using opencv2 software;
[0101] Taking the center position C as the starting point, make a ray for each deflection of the preset angle, and obtain several rays after one rotation. Determine the intersection of each ray with the maximum contour C to obtain several radii C. For details, please refer to Fig.12 ;
[0102] Calculate the average value of all radii C as the radius D of the equivalent circle of the outer contour, and use the radius D as the radius of the hole in the punched image. The circle drawn based on this diameter can refer to Fig.13 Blue outline.
[0103] In the process of determining the diameter of the hole, the preset angle is preferably 1°, that is, 360 rays can be obtained after one rotation.
[0104] In summary, the edge detection algorithm provided by this solution solves the problem of inaccurate contour detection in existing edge detection methods, and effectively improves the accuracy of edge detection compared with other existing methods. Compared with manual measurement, it greatly improves the efficiency of inspecting hole quality and size, achieving a fast and accurate effect.
Claims
1. A punched image circle edge detection algorithm based on local pixel value statistical threshold, characterized in that: Includes steps: S1, converting the punched image into a grayscale image, and performing a reverse operation on the grayscale value of the grayscale image to obtain a reverse image; S2. Binarize the reverse image according to a preset pixel range to obtain a binary image A, and extract the maximum contour A and the center position A of the maximum contour A in the binary image A using opencv2 software; S3, counting the number of pixel points in the binary image A whose pixel values are greater than the first threshold, converting the counted number of pixel points into the area of a weighted equivalent circle, and calculating the equivalent radius A of the weighted equivalent circle; S4, counting the pixel values inside a circle drawn with the center position A as the circle center and the equivalent radius A as the radius, and using reverse weighted calculation of the statistical threshold A according to the pixel values in ascending order within the circle; S5. Binarize the reverse image according to the statistical threshold A to obtain a binary image B, and extract the maximum contour B and the center position B of the maximum contour B in the binary image B using opencv2 software; S6, performing binarization processing on the maximum contour area B, and performing reverse operation to obtain a first mask; Convert the pixel points of the maximum contour area B into the area of an equivalent circle, and calculate the equivalent radius B of the equivalent circle; S7, taking the center position B as the center and the preset multiple of the equivalent radius B as the radius to obtain a circle B, and performing binarization processing on the part outside the circle B area to obtain a second mask; S8, overlapping the first mask and the second mask, performing inverse calculation on the pixel values of the circular area to obtain a statistical area, and calculating a statistical threshold B by inverse weighting according to the ascending pixel values in the statistical area; S9. According to the statistical threshold value B, the reverse image is binarized to obtain a binarized image C. The maximum contour C in the binarized image C is extracted using opencv2 software as the edge contour of the hole in the punched image.
2. The punched image circle edge detection algorithm according to claim 1, characterized in that: The method of extracting the maximum contour in step S2, step S5 and step S9 is: The dilation function, erosion function and denoising function in the opencv2 software are used to process the binary image, and then the contour recognition function of opencv2 is used to identify the contour to obtain the maximum contour in the binary image; The method for extracting the center position of the maximum contour in step S2 and step S5 is: using the center extraction function in the opencv2 software to extract the center position of the maximum contour.
3. The punched image circle edge detection algorithm according to claim 2, characterized in that: When the maximum contour is extracted in step S2, the kernel of the dilation function is 3*3, the kernel of the erosion function is 3*3, and the kernel of the denoising function is 5*5. The dilation and erosion are iterated once, and the denoising is performed once. When the maximum contour is extracted in step S9, the core of the dilation function is 5*5, and the dilation is iterated twice; the core of the erosion function is 4*4, and the erosion is iterated four times; the core of the denoising function is 8*8, and the denoising is performed once.
4. The punched image circle edge detection algorithm according to claim 1, characterized in that: According to the preset pixel range, the method of binarizing the reverse image is as follows: setting the pixel values in the reverse image within the preset pixel range to 255, and setting the remaining pixel values to 0; According to the statistical threshold A, the method of binarizing the reverse image is as follows: the pixel values in the reverse image that are greater than the statistical threshold A are set to 255, and the remaining pixel values are set to 0; According to the statistical threshold B, the method for binarizing the reverse image is as follows: the pixel values in the reverse image that are greater than the statistical threshold B are set to 255, and the remaining pixel values are set to 0.
5. The punched image circle edge detection algorithm according to claim 1, characterized in that: The calculation formula of the equivalent radius A in step S3 is: , in, is the equivalent radius A; is the area of the weighted equivalent circle of the maximum contour A; is the number of pixels counted in the maximum contour A; The calculation formulas for the equivalent radius B in step S6 are: , in, is the equivalent radius B; is the area of the equivalent circle corresponding to the maximum contour B; is the number of pixels counted in the maximum contour B.
6. The punched image circle edge detection algorithm according to claim 1, characterized in that: The expressions of the statistical threshold A and the statistical threshold B calculated by reverse weighting are: , , in, is statistical threshold A or statistical threshold B; w is the weight allocation rate; P is the quantity split ratio; The first number in ascending order within the circle or statistical area. i pixel value; N is the total number of pixel values within the circle or statistical area; is the mean parameter; and is the intermediate parameter.
7. The punched image circle edge detection algorithm according to claim 6, characterized in that: When calculating the statistical threshold A, the corresponding weight allocation rate is 30%, and the quantity segmentation rate is 70%; when calculating the statistical threshold B, the corresponding weight allocation rate is 61.8%, and the quantity segmentation rate is 70%.
8. The punched image circle edge detection algorithm according to claim 1, characterized in that: Calculate the first threshold The expression is .
9. The punched image circle edge detection algorithm according to claim 1, characterized in that: The expression for converting a punched image into a grayscale image is: in, V is the gray value; R is the red component; G is the green component; B is the blue component; The expression for the reverse operation is: in, For V The reversed value.
10. The punched image circle edge detection algorithm according to any one of claims 1 to 9, characterized in that: Also includes: The center position C of the maximum contour C is extracted using opencv2 software; Taking the center position C as the starting point, a ray is made for each deflection of the preset angle, and several rays are obtained after one rotation. The intersection of each ray and the maximum contour C is determined to obtain several radii C; The average value of all radii C is calculated as the radius D of the outer contour equivalent circle, and the radius D is used as the radius of the hole in the punched image.
Citation Information
Patent Citations
Human image optimization method capable of automatically detecting mask in human face key areas
CN104794693A
Workpiece size visual measurement method for complex machining environment
CN117011282A