Cover claw width measuring method based on contour recognition under constraint of space and angle conditions
Through the contour recognition method based on spatial and angular condition constraints, the Canny edge detection and arc segment circle detection algorithm are improved, and the contour detection and measurement are carried out in combination with bottle cap thickness information, which solves the problem of small claws and incoherent edge detection in the bottle cap production process, and achieves high-precision claw width measurement.
Patent Information
- Application Number
- CN202510067325.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-16
- Publication Date
- 2025-04-25
AI Technical Summary
During the production process, existing bottle caps have problems such as small claws and uneven rubber rings due to process reasons, resulting in problems with occlusivity and sealing. The edge contour detection is not coherent and unclear, and the inner and outer contour circles are difficult to determine.
The contour recognition method based on spatial and angular condition constraints is adopted, edge detection and outer circle contour detection are performed through the improved Canny edge detection algorithm and arc segment circle detection algorithm, and the inner contour detection is performed in combination with the fixed distance information of the bottle cap thickness, and finally the width information of the bottle cap wall is presented through the waveform diagram for measurement.
It effectively suppresses noise, solves the problem of incoherence in the edges of the image, improves the robustness of the algorithm, accurately measures the width of the contour, and achieves high experimental accuracy and good experimental results.
Smart Images

Figure CN119887814A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of cover jaw width measurement, and in particular to a cover jaw width measurement method based on contour recognition under space and angle condition constraints. Background Art
[0002] With the rapid development of economy and the continuous improvement of people's living standards, the quality and safety of food have become the focus of social attention. Bottle caps are an important part of food packaging safety, and their key position and product quality have attracted much attention. Bottle caps play an irreplaceable role in food safety. The quality of bottle caps determines the airtightness of food. The quality problems of bottle caps in the production process are the key to ensuring food safety. The conversion of bottle cap quality inspection from manual inspection to machine inspection can effectively improve the quality of bottle caps. Although modern assembly lines provide a lot of convenience for the production of bottle caps, various defects are inevitable in bottle caps due to excessive injection, machine failure, substandard mold processing and other reasons.
[0003] During the production process of existing bottle caps, due to process reasons, problems such as small cap claws and uneven rubber rings will occur, which will lead to problems such as bite and sealing. In addition, during the production process of existing bottle caps, difficult problems such as discontinuous and unclear edge contour detection and difficulty in determining the inner and outer contour circles will occur. Summary of the invention
[0004] Purpose of the invention: In view of the above problems, the present invention proposes a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints.
[0005] Technical solution:
[0006] The present invention provides a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints, comprising the following steps: step S1: using an improved Canny edge detection algorithm to perform edge detection; the improved Canny edge detection algorithm uses a bilateral filtering algorithm for filtering, calculates the gradient amplitude and direction of each pixel through a gradient algorithm, sets an adaptive double threshold, performs double threshold processing and edge connection, and obtains an edge detection image; step S2: using an arc segment circle detection algorithm to perform outer circle contour detection, the arc segment circle detection algorithm extracts arcs from the edge detection image, and obtains an edge detection image; Segment extraction: arc segments that may belong to the same circle are grouped by angle constraint and space constraint methods, and then the arc segments in the same group are quickly fitted with circles, and finally the fitted circle is verified to obtain the outer contour; step S3: using the fixed distance information of the bottle cap thickness, the inner contour is detected to obtain the inner contour; the inner contour detection includes detection area selection, breakpoint processing, and inner contour fitting; step S4: measuring the width according to the inner and outer contours; step S5: presenting the width information of the bottle cap wall in the form of a waveform diagram, and comparing the peak error to determine whether the bottle cap claw meets the requirements.
[0007] Preferably, the improved Canny edge detection algorithm comprises: step S11: filtering using a bilateral filtering algorithm; the calculation formula of the bilateral filter is:
[0008]
[0009] Where I(i,j) is the given input image, J(x,y) is the output image after filtering; f r is the range weight; f r (||I(i,j)-I(x,y)||) is the range weight function; f s is the spatial weight; f s ((i,j)-(x,y)) is the spatial weight function; W p is a normalization factor to ensure that the sum of the pixel values after filtering is 1; step S12: calculate the gradient amplitude and direction of each pixel through the gradient algorithm; step S13: set an adaptive double threshold; step S14: for each pixel, check its neighboring pixels along the gradient direction and select the largest pixel value; retain the local maximum value in the direction of the pixel gradient value, perform edge connection, and use the depth-first search method to connect the strong edge and the determined weak edge to obtain an edge detection image.
[0010] Preferably, the setting of the adaptive dual threshold comprises calculating the threshold according to the mean μ and standard deviation σ of the image gradient; the calculation method is as follows:
[0011] T high =μ+kσ (11)
[0012] T low =μ-kσ (12)
[0013] Among them, T high is the high threshold, T low is the low threshold; k is a constant that controls the threshold gap.
[0014] Preferably, the range weight function formula is:
[0015]
[0016] where σ r is the standard deviation of pixel differences, which controls the influence of similarity on pixel values;
[0017] The spatial weight function formula is:
[0018]
[0019] where σ s is the standard deviation in the spatial domain, which controls the influence of adjacent pixels in space;
[0020] The normalization factor W p The formula is:
[0021]
[0022] Preferably, the outer circle contour detection using arc segment circle detection algorithm comprises: step S21: extracting arc segments from edge detection image; step S22: grouping arc segments that may belong to the same circle by angle constraint and space constraint; step S23: removing curvature straight lines, removing line segments or arcs with too small and too large curvatures; assuming that the endpoints and midpoints of the arc are A, B, and M respectively, then when When, or When , the arc segment is removed, wherein Tc and Td are set thresholds; step S24: fast fitting of the arc segments in the same group; step S25: verifying the fitted circle, including: firstly generating a group of points from the fitted circle, generating multiple points through the equation of the circle and discrete angles to represent the coordinates of each position on the circle, and the parameterized formula of the point coordinates is:
[0023] x circle =a+rcos(θ) (24)
[0024] y circle =b+rsin(θ) (25)
[0025] Where θ∈(0, 2π), (a, b) are the coordinates of the center of the circle, and r is the radius of the circle. The Canny edge detection result is then used to generate a discrete, pixel-based binary image of edge points. Each point on the fitted circle is detected to see if it coincides with the Canny edge point and the degree of overlap is calculated. Circles whose overlap exceeds the threshold are retained, otherwise they are removed.
[0026] Preferably, the angle constraint comprises: based on the concavity and curvature of the circle, approximating the arc into a set of line segments; expressing a set of arc line segments as {l k |k=1,2,...,n}, the angle between the line segments is {θ i |i=1,2,...,n-1,θ i ∈(-π,π)}, let l k The direction vector is like
[0027]
[0028] Then θ i is considered as a sharp angle, where θ i For two continuous lines l k and l k+1 The angle between a is the angle threshold; the cross product of the vector is used to identify the inflection point of the line segment; if
[0029]
[0030] Then l k The starting dominant point is regarded as the inflection point; the arc segments are grouped according to the inflection point and the sharp angle;
[0031] The space constraint includes: setting the starting point of Gi group to J si The end point is recorded as J ei ; The starting point of the arc is K si The end point of the arc is K ei ; J si and J ei The midpoint is M i ; Set the judgment conditions:
[0032]
[0033] in, The normalized arc support direction vector of ; express Normalized vector obtained by rotating 90° clockwise; when {G1, G2} are judged to be in the same group, Pol is +1, otherwise it is -1; T b is the distance threshold; the arc segments are further grouped according to the above judgment conditions.
[0034] Preferably, the detection area selection includes: obtaining the outer contour of the bottle cap, performing color channel separation on the input image to extract the red area; assuming that the RGB channel of the image is I R ,I G ,I B Represents the pixel values of the red, green, and blue channels respectively, and the red area is extracted using the following threshold conditions:
[0035] Red=(I R >150)^(I G <100)^(I B <100) (26)
[0036] Among them, ∧ represents the logical AND operation, which is used to filter out the red part in the image; the binary mask obtained is used to locate the part of the image that contains the target circular area; the position, center coordinates and radius information of the outer contour are obtained, and two radius thresholds are set according to the thickness of the bottle cap: the outer radius threshold r out = 0.95r and inner radius threshold r in =0.85r, select the annular area and generate a mask M of the annular area ring ,
[0037]
[0038] Preferably, the breakpoint processing includes: selecting 10,000 points uniformly distributed on its edge contour, and dilating these points to resolve the influence of the breakpoints on the contour depiction.
[0039] Preferably, the inner contour fitting includes: extracting contour information in the expanded edge image using the bwboundaries algorithm; extracting the outermost contour by the bwboundaries algorithm, and inverting the expanded image so that the inner area becomes white and the outer area becomes black; thereby obtaining the inner contour image; smoothing the inner contour to obtain the inner contour of the bottle cap; and drawing the inner contour curve into the original image to obtain the detection of the inner and outer contours of the bottle cap.
[0040] Preferably, the smoothing of the inner contour includes: using cubic splines for fitting, and performing smooth interpolation of a quadratic function based on every two adjacent data points:
[0041] S(x)=ax 3 +bx 2 +cx+d (28)
[0042] Among them, a, b, c and d are coefficients determined by solving the system equations; the inner wall profile of the bottle cap is obtained.
[0043] Preferably, the present invention provides a cover jaw width measurement system based on contour recognition under spatial and angular constraints, and adopts a cover jaw width measurement method based on contour recognition under spatial and angular constraints to determine the cover jaw width.
[0044] Compared with the prior art, the present invention has the following beneficial effects:
[0045] (1) The present invention proposes an improved multi-stage Canny edge detection algorithm, which uses bilateral filtering to replace Gaussian filtering and adaptive dual threshold methods, can effectively suppress noise, solve the problem of image edge incoherence, and enhance the robustness of the algorithm; the algorithm not only has strong anti-interference ability, but also the detected edge image is clearer and more coherent for easy identification.
[0046] (2) For the detection of the outer circle, an arc segment circle detection algorithm is proposed. The algorithm quickly fits the outer circle contour by combining the convexity and concavity of the line segment with the global search judgment and the hierarchical clustering method.
[0047] (3) By utilizing the fixed distance information of the bottle cap thickness and combining non-maximum suppression and threshold processing to depict the inner circle contour, the width of the contour can be accurately measured, showing high experimental accuracy and good experimental results. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] Figure 1 It is a flow chart of a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints of the present invention;
[0049] Figure 2 1 is a schematic diagram of edge detection results of step S1 of a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints of the present invention;
[0050] Figure 3 It is a schematic diagram of angle constraint of step S2 of a method for measuring the width of a cover jaw based on contour recognition under spatial and angle constraints of the present invention;
[0051] Figure 4 It is a schematic diagram of grouping of step S2 of a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints of the present invention;
[0052] Figure 5 It is a schematic diagram of curvature straight line removal in step S2 of a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints of the present invention;
[0053] Figure 6 This is an example diagram of inner contour detection in step S3 of a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints of the present invention;
[0054] Figure 7 These are six different bottle cap detection example diagrams of a cap claw width measurement method based on contour recognition under spatial and angular constraints of the present invention;
[0055] Figure 8 These are six different curve detection examples of bottle cap widths in a method for measuring cap claw widths based on contour recognition under spatial and angular constraints according to the present invention. DETAILED DESCRIPTION
[0056] Many modifications and changes made by those skilled in the art based on the principles of the present invention belong to the protection scope of the present invention.
[0057] It will be understood by those skilled in the art that, unless expressly stated, the singular forms "a", "an", "said" and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present invention refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof. It should be understood that when an element or component is said to be "connected" to another element or component, it may be directly connected to the other element or component, or there may be an intermediate element or component. The term "and / or" used herein includes any unit and all combinations of one or more associated listed items.
[0058] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0059] like Figure 1As shown, the embodiment of the present invention provides a method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints, comprising the following steps: Step S1: using an improved Canny edge detection algorithm to perform edge detection; the improved Canny edge detection algorithm uses a bilateral filtering algorithm for filtering, calculates the gradient amplitude and direction of each pixel through a gradient algorithm, sets an adaptive double threshold, performs double threshold processing and edge connection, and obtains an edge detection image; Step S2: using an arc segment circle detection algorithm to perform outer circle contour detection, the arc segment circle detection algorithm performs edge detection from the edge detection image Arc segment extraction: arc segments that may belong to the same circle are grouped by angle constraint and space constraint methods, and then the arc segments in the same group are quickly fitted with circles, and finally the fitted circle is verified to obtain the outer contour; step S3: using the fixed distance information of the bottle cap thickness, the inner contour is detected to obtain the inner contour; the inner contour detection includes detection area selection, breakpoint processing, and inner contour fitting; step S4: measuring the width according to the inner and outer contours; step S5: presenting the width information of the bottle cap wall in the form of a waveform diagram, and comparing the peak error to determine whether the bottle cap claws meet the requirements.
[0060] Furthermore, the basic idea of the traditional Canny edge detection operator is to perform Gaussian filtering first. First, the input image is smoothed to prevent noise from interfering with edge detection. The Gaussian filter is a smoothing filter based on Gaussian distribution, and the formula is as follows:
[0061]
[0062] Among them, σ is the standard deviation, which determines the smoothness of the Gaussian filter, and x, y are the pixel positions.
[0063] The Canny algorithm determines the strength and direction of the edge by calculating the gradient magnitude and gradient direction of each pixel. The Sobel operator is a common edge detection operator that can be used to calculate the gradient of an image in both horizontal and vertical directions.
[0064]
[0065] By convolving the image, we get the horizontal gradient G x and the vertical gradient G y , and then calculate the gradient magnitude and direction of each pixel:
[0066]
[0067] Among them, M(x,y) is the gradient magnitude, which indicates the edge strength of the point in the image; θ(x,y) is the gradient direction, which indicates the direction of the edge. The purpose of non-maximum suppression is to eliminate the edge width and ensure that the edge is single-pixel wide. This step suppresses non-edge pixels by checking the neighborhood of each pixel along the gradient direction, retaining the local maximum. For each pixel, check its neighborhood in both horizontal and vertical directions according to its gradient direction. Then compare the gradient magnitude of the current pixel with the gradient magnitude of the neighboring pixels. Finally, double threshold processing and edge connection are performed.
[0068] The following solutions are proposed to solve the problems of incoherent and unclear edge contours, reflective rubber rings, and complex backgrounds in the process of bottle cap contour detection. First, bilateral filtering is used to replace Gaussian filtering. Compared with Gaussian filtering, bilateral filtering can better retain edge information while smoothing the image. It not only considers the spatial distance between pixels, but also the grayscale difference of pixels. While removing noise, it also retains the strong gradient changes at the edge, effectively avoiding the problem of edge blur, and solving the edge blur problem of the traditional Canny detection algorithm in bottle cap edge detection. At the same time, bilateral filtering also has better noise removal capabilities. Gaussian filtering is effective in removing Gaussian noise, but it may cause edge details to be lost when removing complex noise (such as salt and pepper noise, speckle noise, etc.). Bilateral filtering works better when dealing with complex noise. It determines the weight according to the similarity of pixel values, giving higher weights to similar pixels and reducing the weights to dissimilar pixels (such as noise), thereby effectively suppressing noise, effectively solving the problem of incoherent image edges, and enhancing the robustness of the algorithm.
[0069] The improved Canny edge detection algorithm comprises: step S11: filtering using a bilateral filtering algorithm; the calculation formula of the bilateral filter is:
[0070]
[0071] Where I(i,j) is the given input image, J(x,y) is the output image after filtering; f r is the range weight, which reflects the similarity between pixel values; f r (||I(i,j)-I(x,y)||) is the range weight function, which is related to the difference in pixel values; f s is the spatial weight, which reflects the distance between pixels in space. Pixels farther away have less influence on the current pixel. s (||(i,j)-(x,y)||) is the spatial weight function; W p is the normalization factor, which ensures that the sum of the pixel values after filtering is 1;
[0072] The range weight function formula is:
[0073]
[0074] where σ r is the standard deviation of the range filter, which controls the influence of the gray value similarity.
[0075] The spatial weight function formula is:
[0076]
[0077] where σ s is the standard deviation of the spatial filter, which controls the influence of spatial position similarity.
[0078] The normalization factor W p The formula is:
[0079]
[0080] Step S12: Calculate the gradient magnitude and direction of each pixel by using a gradient algorithm;
[0081] Expanding the gradient calculation in the Canny edge detection algorithm from the original two directions (horizontal and vertical) to four directions (i.e. horizontal, vertical, 45° and 135° directions) can effectively improve the granularity of the gradient direction and more accurately capture the edge changes. The gradients in the four directions are as follows:
[0082]
[0083]
[0084] Apply the Sobel operator to calculate the gradient in each direction for each pixel. In actual calculation, the gradient in each direction will produce a gradient value G x , G y , G 45° , G 135° , in order to calculate the total gradient magnitude and direction:
[0085]
[0086] Among them G θ is the total gradient magnitude, θ is the magnitude direction; the four directional gradient magnitudes of each pixel are calculated to obtain more detailed edge direction information.
[0087] The traditional Canny edge detection algorithm uses two fixed thresholds to distinguish strong edges from weak edges. However, the traditional dual threshold method is not suitable for all types of images because the contrast, brightness and noise characteristics of different images are different. If the threshold is too high, many edges may be lost; if the threshold is too low, too much noise and unimportant edges may be retained. Therefore, the adaptive dual threshold method is used to solve this problem.
[0088] The histogram is calculated based on the gradient amplitude of the image. The histogram reflects the distribution of different gradient amplitudes in the image. By performing statistical analysis on the histogram of the gradient amplitude, the threshold suitable for the image can be determined. According to the statistical characteristics of the image gradient amplitude (such as mean, standard deviation, etc.), the high threshold and low threshold can be automatically determined.
[0089] Step S13: Calculate the threshold value according to the mean μ and standard deviation σ of the image gradient; the calculation method is as follows:
[0090] T high =μ+kσ (11)
[0091] T low =μ-kσ (12)
[0092] Among them, T high is the high threshold, T low is the low threshold; k is a constant that controls the threshold gap, and the threshold can be dynamically adjusted according to the gradient statistics of the image;
[0093] Step S14: For each pixel, check its neighboring pixels along the gradient direction and select the largest pixel value; retain the local maximum value in the gradient value direction of the pixel, perform edge connection, and use the depth-first search method to connect the strong edge and the determined weak edge to obtain an edge detection image. Figure 2 It is a comparison picture of the bottle cap test results.
[0094] like Figure 2 In the results of bottle cap edge detection: Figure (a) is the original image, Figure (b) is the result of traditional Canny detection. Due to the reflection of the rubber ring and the complex background, the detection result is poor. Figure (c) is the result of post-processing after improving bilateral filtering and adjusting the low threshold. The interference removal effect is obvious, but there are too many breakpoints in the contour and the outer wall contour is not coherent. Therefore, the gradient calculation is expanded from the original two directions to four directions, and the detection result shown in Figure (d) is obtained. It can be seen that the edge contour is significantly enhanced, but false positive edges appear again. Finally, adaptive threshold processing and non-maximum suppression are added to obtain Figure (e).
[0095] Furthermore, the use of arc segment circle detection algorithm to detect the outer circle contour includes: step S21: extracting arc segments from the edge detection image;
[0096] Step S22: The arc segments that may belong to the same circle are grouped by angle constraint and space constraint. Usually a circle should be composed of multiple arc segments. Correct classification of arc segments can greatly improve the speed and accuracy of circle recognition. First, we use the highly efficient and accurate RDP (Römer-douGlas-peucker) algorithm to simplify the contour image obtained by Canny edge detection, that is, to approximate the edge curve into a group of line segments, so that it is easier to determine its polarity, spatial characteristics and curvature characteristics. The arc segments that may belong to the same circle are grouped into one arc segment group. The angle constraint ensures that the points extracted from the arc segments in the image meet the geometric characteristics of the circle through the relationship between the normal direction and the line connecting the center of the circle, reducing the possibility of false detection. The spatial constraint is to infer the approximate position range of the circle based on the consistency of the relative position, angle and radius of multiple arc segments, and to ensure that these arc segments belong to the same circle by analyzing the spatial geometric consistency between the arc segments. Unlike other circle detection algorithms, we only need to identify the outer circle contour of the bottle cap of a fixed size, so we directly remove the oversized line segments based on the curvature. However, the contour of the cap claw has almost no curvature, so the straight line segments cannot be directly removed. The specific method is as follows:
[0097] Angle constraint: Based on the convexity and curvature of the circle, the arc is approximated as a set of line segments; Figure 3 The set of arc segments shown is represented by {l k |k=1,2,...,n}, the angle between the line segments is {θ i |i=1,2,...,n-1,θ i ∈(-π,π)}, the endpoints of these line segments are called advantage points, let l k The direction vector is like
[0098]
[0099] Then θ i is considered as a sharp angle, where θ i For two continuous lines l k and l k+1 The angle between a is the angle threshold; the cross product of the vector is used to identify the inflection point of the line segment (the point where the curvature direction changes); if
[0100]
[0101] Then l kThe starting dominant point of is considered as the inflection point; therefore, Figure 3 A5 is the turning point and A9 is the sharp corner. The arc segments are grouped according to the turning points and sharp corners.
[0102] Spatial constraints: Spatial constraints play an important role in determining whether a group of arc segments belongs to the same circle, such as Figure 4 As shown in , the group {G3, G4} and the group {G1, G4} should not be paired. The reason is that the groups G3 and G4 are both outside the valid region of each other. Although G4 is in the valid region of G1, G1 is not in the valid region of G4. Therefore, if the two groups are paired, they should be in the valid region of each other. Figure 4 In the example, the starting point of the Gi group is denoted by J si The end point is recorded as J ei ; The starting point of the arc is K ei ; J si and J ei The midpoint is M i Then, we set the judgment condition:
[0103]
[0104] in, The normalized arc support direction vector is shown by the red line in the figure; express Normalized vector obtained by rotating 90° clockwise; when {G1, G2} are judged to be in the same group, Pol is +1, otherwise it is -1; T b is the distance threshold; the arc segments are further grouped according to the above judgment conditions to increase the accuracy and reliability of the grouping;
[0105] Step S23: Remove the curvature straight line, remove the line segments or arcs with too small or too large curvature; in order to further speed up the detection speed, we directly remove the line segments or arcs with too small or too large curvature. Unlike other circle detection, we do not need to detect all the circles in the figure, but only need to detect the outer circle contour of a fixed size, so the line segments or arcs with too large or too small curvature can be directly ignored. The curvature of the arc is verified according to the trigonometric inequality.
[0106] like Figure 5 As shown, let the endpoints and midpoints of the arc be A, B, and M respectively. Then when When, or When , Tc and Td are set thresholds, that is, when the arc is close to a straight line segment or is too curved, the arc segment is removed; the final extracted arc image is shown in Figure (5).
[0107] Step S24: quickly fitting a circle to the arc segments in the same group;
[0108] The RANSAC (Random Sample Consensus) algorithm is used to fit the circle. The traditional RANSAC is an iterative method that is good at estimating model parameters from data containing a large amount of noise. In order to cope with the complex working environment of the assembly line, three-point fitting is mainly used for circle fitting. The inner points belonging to the same circle are screened out from the data, and the outer points are excluded to obtain the optimal fitting parameters of the circle. The present invention combines the arc segment grouping work in the previous text, and fits each group of arc segments once, thereby avoiding the influence of points outside the arc segments on the fitting of the outer wall circle. At the same time, in order to improve the reliability of the circle fitting result, an algorithm for fitting circles to all inner points is adopted. The specific method is as follows.
[0109] First, fit a set of two-dimensional points into a circle, and calculate the center and radius of a fitted circle based on three randomly selected points. For all data points, calculate the distance from each point to the fitted circle. If the distance is less than the preset threshold, the point is considered an inlier, otherwise it is an outlier. Repeat the above steps multiple times and calculate the number of inliers, and select the circle with the most inliers as the optimal model. Suppose the coordinates of the given three points are P1(x1,y1), P2(x2,y2), and P3(x3,y3), the general expression of the circle is
[0110] x 2 +y 2 +Dx+Ey+F=0 (16)
[0111] Substitute the coordinates of the three points to solve D, E, and F, thereby solving the center and radius of the circle, and calculating the distance of each point in the data to the fitted circle:
[0112]
[0113] Points whose distance is less than the threshold distance are taken as inliers, and after a sufficient number of iterations, the circle with the most inliers is taken as the optimal solution.
[0114] We have obtained the optimal solution with the most inliers, but the circle is fitted based on three points. In order to improve the reliability of the circle, the direct least squares method is used to fit the circle with the smallest sum of squares of distances from the inliers. The objective function is as follows:
[0115]
[0116] Where n is the number of interior points, (x i ,y i ) is the coordinate of the ith point, D, E, and F are the parameters of the circle, and the function represents the sum of the squares of the distances from each point to the fitted circle. By minimizing the function, we find the optimal fitted circle. We calculate the partial derivative of the above function to obtain a linear equation, and then solve the values of D, E, and F:
[0117]
[0118] Simplify these equations to a system of linear equations with three unknowns D, E, and F:
[0119]
[0120] After solving D, E, and F, the center coordinates (a, b) and radius r are calculated using the following formula:
[0121]
[0122] Step S25: Verify the fitted circle; each arc segment is processed independently. The inner point fitting circle method can identify almost all possible circles in the image, and has good detection accuracy and recall. However, the inner point fitting method will fit a circle for each group of arc segments. Therefore, in this section, in order to further ensure the quality and correctness of the detected circle, we start with the edge overlap to verify the candidate circle.
[0123] First, generate a set of points from the fitted circle, and generate multiple points through the circle equation and discrete angles to represent the coordinates of each position on the circle. The parameterized formula of the point coordinates is:
[0124] x circle =a+rcos(θ) (24)
[0125] y circle =b+rsin(θ) (25)
[0126] Where θ∈(0, 2π), (a, b) are the coordinates of the center of the circle; then use the Canny edge detection result to generate a discrete, pixel-based binary image of edge points, detect whether the points on each fitted circle coincide with the Canny edge points, and calculate the degree of overlap. Circles with overlaps exceeding the threshold are retained, otherwise they are removed.
[0127] Furthermore, the inner contour detection includes detection area selection, breakpoint processing, and inner contour fitting;
[0128] After the circle detection algorithm has detected all possible circles in the bottle cap, we select a circle of fixed size according to the size and dimensions of the bottle cap model, and at the same time improve the matching degree between the fitted circle and the Canny edge detection line. In this way, we can obtain an accurate outer circle contour image and mark the outline of the outer circle in the result of Canny edge detection. Due to the existence of the cap claw, the inner circle of the bottle cap is not a standard circle and the outline cannot be detected by the above circle detection algorithm. Under the premise of knowing the outer circle contour, we use the fixed thickness of the bottle cap to propose a method for extracting the internal contour of a limited area. The main steps are area selection, breakpoint processing, and inner contour fitting. Figure 6The following is a detection example diagram.
[0129] First, we obtain the outer wall contour of the bottle cap and perform color channel separation on the input image to extract the red area. Let the RGB channel of the image be I R ,I G ,I B Represents the pixel values of the red, green, and blue channels respectively, and the red area is extracted using the following threshold conditions:
[0130] Red=(I R >150)∧(I G <100)∧(I B <100) (26)
[0131] Among them, ∧ represents the logical AND operation, the purpose of which is to filter out the red part in the image. The binary mask obtained by this method is used to locate the part of the image that contains the target circular area.
[0132] In this way, we get the position of the outer contour, the coordinates of the center of the circle, the radius and other information. According to the thickness of the bottle cap, we set two radius thresholds: the outer radius threshold r out = 0.95r and inner radius threshold r in =0.85r, select the annular area such as Figure 6 (b) shows that for the selected area ( Figure 6 (c)) Generate a mask Mring of a ring area,
[0133]
[0134] If you directly Figure 6 (c) Contour detection is affected by breakpoints, resulting in the following Figure 6 In the case shown in (e), the inner and outer contours are incorrectly connected together. We select 10,000 points evenly distributed on its edge contour (such as Figure 6 (d)), the effect of breakpoints on contour depiction is resolved by dilating these points, and the details of the image may be more prominent. Figure 6 (f) To make the image more intuitive, we expand the points to 25 pixels in size (set to 12 in the actual experiment).
[0135] In the expanded edge image, the bwboundaries algorithm is used to extract contour information. However, the bwboundaries algorithm extracts the outermost contour by default, so we invert the expanded image so that the inner area becomes white (value 1) and the outer area becomes black (value 0). In this way, we can correctly obtain the contour image of the inner wall only. The next step is to smooth the contour of the inner wall, thereby effectively removing noise, repairing irregular parts, and generating a smoother and more natural contour. Spline interpolation smoothing is to approximate data points by fitting a smooth curve. Usually, cubic splines are used for fitting, and smooth interpolation of quadratic functions is performed based on every two adjacent data points:
[0136] S(x)=ax 3 +bx 2 +cx+d (28)
[0137] Among them, a, b, c and d are coefficients determined by solving the system equations. The inner wall profile of the bottle cap is obtained as follows Figure 6 Finally, the contour curve is drawn into the original image. So far, we have completed the detection of the inner and outer wall contours of the bottle cap. The detection results are shown in Figure 6 (h) shown.
[0138] It should be noted that the technical solution of the present invention can be further illustrated by the following experiments.
[0139] Figure (7) shows six examples of bottle cap detection with different sizes, shapes and colors. Figure 7 The (a)-(f) rows of the table represent six different bottle caps. Figure 7 Columns (1) to (5) represent the original bottle cap image, the result of improved canny edge detection, the outer circle detection result (red outer circle), the inner contour detection result (green), and the manually annotated inner and outer circle contours, respectively.
[0140] The six types of bottle caps cover most of the detection requirements in reality. Bottle cap (a) is relatively small, so it is easy to have a viewing angle deviation when acquiring the image, resulting in the failure to recognize the circle. Bottle cap (b) is easily confused with the background when identifying the edge due to the different patterns. At the same time, the patterns on the cap claws are of two types: floral and solid colors, which is also a challenge for the correct recognition of the contour of the cap claws. Bottle cap (c) represents the most common and commonly used bottle cap type. Bottle caps (d) and (e) are a group of bottle caps of the same size but different colors. The accuracy of the system in detecting bottle caps of different colors is verified. At the same time, the color of the inner cap claws of bottle cap (d) is similar to the background color, and the inner contour detection is more difficult. Bottle cap (f) is larger, with more and narrower cap claws, making it difficult to achieve accurate detection. The results show that all outer circle contour detections have achieved good results. Although the improved canny edge detection algorithm has accurately identified the contour of the bottle cap, the removal of short and curved line segments will cause some missing corners and breakpoints in the contour of the bottle cap claw (as shown in Figures (b)-(2)), resulting in inaccurate depiction of the claw contour. In most cases, satisfactory detection results are achieved for the inner and outer contours of the bottle cap.
[0141] After correctly detecting the inner and outer contours, it becomes easy to measure the width of the cover jaws. First, determine the center of the outer circle and make enough radii evenly within 360°, and measure each line segment connecting the inner and outer contours. The cover jaw width detection image is as follows Figure 8 As shown in the figure, (a)-(f) are six kinds of bottle cap width curves, where the red curve is the system detection result, and the blue curve is the annotation result of the original image. The horizontal axis is the angle, the numerical axis is the width, and the detection accuracy is pixel level, which is equivalent to 10 micron level. From the detection results in the figure, we can see that except for Figure 8 (c) Figure 8 The error of the detection result of (f) is relatively large. The error of the cap claw detection is about 10 pixels. The main reason for the large error is that it is affected by lighting, the color of the bottle cap contour, etc., and the improved canny detection results have missing corners and unclear contours. Although the expansion algorithm has solved the problem of breakpoints, it is still difficult to achieve accurate measurement for overly blurred contours. The errors of other bottle caps are controlled within five pixels. Figure 8 (d) and Figure 8 (e) shows the accurate measurement results of the cap claw width. The detection results are basically the same as the annotation results, and all errors are within the error range, indicating that when the shooting accuracy is high and the image contour is clear, our cap claw width detection system can accurately measure the width of the contour. In general, the bottle cap cap claw width detection system shows high experimental accuracy and good experimental results, and can complete the task of measuring the cap claw width.
[0142] The applicant of the present invention has made a detailed explanation and description of the implementation examples of the present invention in conjunction with the drawings in the specification. However, those skilled in the art should understand that the above implementation examples are only preferred implementation schemes of the present invention, and the detailed description is only to help readers better understand the spirit of the present invention, but not to limit the scope of protection of the present invention. On the contrary, any improvements or modifications based on the inventive spirit of the present invention should fall within the scope of protection of the present invention.
Claims
1. A method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints, characterized in that: The method comprises the following steps: step S1: edge detection is performed by adopting an improved Canny edge detection algorithm; the improved Canny edge detection algorithm adopts a bilateral filtering algorithm for filtering, calculates the gradient amplitude and direction of each pixel by a gradient algorithm, sets an adaptive double threshold, performs double threshold processing and edge connection, and obtains an edge detection image; step S2: outer circle contour detection is performed by adopting an arc segment circle detection algorithm, the arc segment circle detection algorithm extracts arc segments from the edge detection image, groups arc segments that may belong to the same circle by angle constraint and space constraint, then performs circle fast fitting on the arc segments in the same group, and finally verifies the fitted circle to obtain the outer contour; step S3: inner contour detection is performed by using fixed distance information of the bottle cap thickness to obtain the inner contour; the inner contour detection comprises detection area selection, breakpoint processing, and inner contour fitting; step S4: width is measured according to the inner and outer contours; step S5: width information of the bottle cap wall is presented in the form of a waveform diagram, and peak error is compared to determine whether the bottle cap claw meets the requirements.
2. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 1, characterized in that: The improved Canny edge detection algorithm comprises: step S11: filtering using a bilateral filtering algorithm; the calculation formula of the bilateral filter is: Where I(i,j) is the given input image, J(x,y) is the output image after filtering; f r is the range weight; f r (||I(i,j)-I(x,y)||) is the range weight function; f s is the spatial weight; f s (||(i,j)-(x,y)||) is the spatial weight function; W p is a normalization factor to ensure that the sum of the pixel values after filtering is 1; step S12: calculate the gradient amplitude and direction of each pixel through the gradient algorithm; step S13: set an adaptive double threshold; step S14: for each pixel, check its neighboring pixels along the gradient direction and select the largest pixel value; retain the local maximum value in the direction of the pixel gradient value, perform edge connection, and use the depth-first search method to connect the strong edge and the determined weak edge to obtain an edge detection image.
3. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 2, characterized in that: The setting of the adaptive dual threshold includes calculating the threshold according to the mean μ and standard deviation σ of the image gradient; the calculation method is as follows: T high =μ+kσ (11) T low =μ-kσ (12) Among them, T high is the high threshold, T low is the low threshold; k is a constant that controls the threshold gap.
4. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 3, characterized in that: The range weight function formula is: where σ r is the standard deviation of pixel differences, which controls the influence of similarity on pixel values; The spatial weight function formula is: where σ s is the standard deviation in the spatial domain, which controls the influence of adjacent pixels in space; The normalization factor W p The formula is:
5. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 4, characterized in that: The outer circle contour detection using arc segment circle detection algorithm comprises: step S21: extracting arc segments from edge detection image; step S22: grouping arc segments that may belong to the same circle by angle constraint and space constraint; step S23: removing curvature straight lines, removing line segments or arcs with too small and too large curvatures; assuming that the endpoints and midpoints of the arc are A, B, and M respectively, then when When, or When , the arc segment is removed, where Tc and Td are set thresholds, and τ represents the curvature of the curve; Step S24: quickly fit the arc segments in the same group to a circle; Step S25: verify the fitted circle, including: firstly, generating a group of points from the fitted circle, generating multiple points through the equation of the circle and discrete angles to represent the coordinates of each position on the circle, and the parameterized formula of the point coordinates is: x circle =a+rcos(θ) (24) y circle =b+rsin(θ) (25) Where θ∈(0, 2π), (a, b) are the coordinates of the center of the circle, and r is the radius of the circle. The Canny edge detection result is then used to generate a discrete, pixel-based binary image of edge points. Each point on the fitted circle is detected to see if it coincides with the Canny edge point and the degree of overlap is calculated. Circles whose overlap exceeds the threshold are retained, otherwise they are removed.
6. The method for measuring the width of the cover jaws based on contour recognition under spatial and angular constraints according to claim 5, characterized in that: The angle constraint includes: based on the convexity and curvature of the circle, approximating the arc into a set of line segments; expressing a set of arc line segments as {l k |k=1,2,...,n}, the angle between the line segments is {θ i |i=1,2,...,n-1,θ i ∈(-π,π)}, let l k The direction vector is like Then θ i is considered as a sharp angle, where θ i For two continuous lines l k and l k+1 The angle between a is the angle threshold; the cross product of the vector is used to identify the inflection point of the line segment; if Then l k The starting dominant point is regarded as the inflection point; the arc segments are grouped according to the inflection point and the sharp angle; The space constraint includes: setting the starting point of Gi group to J si The end point is recorded as J ei ; The starting point of the arc is K si The end point of the arc is K ei ; J si and J ei The midpoint is M i ; Set the judgment conditions: in, The normalized arc support direction vector of ; express Normalized vector obtained by rotating 90° clockwise; when {G1, G2} are judged to be in the same group, Pol is +1, otherwise it is -1; T b is the distance threshold; the arc segments are further grouped according to the above judgment conditions.
7. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 1, characterized in that: The detection area selection includes: obtaining the outer contour of the bottle cap, performing color channel separation on the input image to extract the red area; assuming that the RGB channel of the image is I R ,I G ,I B Represents the pixel values of the red, green, and blue channels respectively, and the red area is extracted using the following threshold conditions: Red=(I R >150)∧(I G <100)v(I B <100) (26) Among them, ∧ represents the logical AND operation, which is used to filter out the red part in the image; the binary mask obtained is used to locate the part of the image that contains the target circular area; the position, center coordinates and radius information of the outer contour are obtained, and two radius thresholds are set according to the thickness of the bottle cap: the outer radius threshold r out = 0.95r and inner radius threshold r in =0.85r, select the annular area and generate a mask M of the annular area ring , 8. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 1, characterized in that: The breakpoint processing includes: selecting 10,000 points and evenly distributing them on the edge contour, and dilating these points to resolve the influence of the breakpoints on the contour description.
9. The method for measuring the width of a cover jaw based on contour recognition under spatial and angular constraints according to claim 1, characterized in that: The inner contour fitting includes: extracting contour information using the bwboundaries algorithm in the expanded edge image; extracting the outermost contour by the bwboundaries algorithm, and inverting the expanded image so that the inner area becomes white and the outer area becomes black; thereby obtaining the inner contour image; smoothing the inner contour to obtain the inner contour of the bottle cap; and drawing the inner contour curve into the original image to obtain the detection of the inner and outer contours of the bottle cap.
10. A cover jaw width measurement system based on contour recognition under spatial and angular constraints, characterized in that: The cover jaw width is determined by using the cover jaw width measurement method based on contour recognition under spatial and angular constraints as described in any one of claims 1 to 9.
Citation Information
Cited By
Rotary 3D contour measurement system
CN121898290A