Research on O-ring Dimension Measurement Algorithm Based on Cubic Spline Interpolation

: Current O-ring dimension measurement algorithms based on machine vision are mainly whole-pixel level algorithms, which have the disadvantage of a low measurement accuracy. In order to improve the stability and accuracy of O-ring dimension measurement, a sub-pixel edge detection algorithm based on cubic spline interpolation is proposed for O-ring dimension measurement. After image pre-processing of the O-ring graphics, the whole-pixel-level O-ring edges are obtained by using a noise-resistant mathematical morphology method, and then the sub-pixel edge contours are obtained using a sub-pixel edge detection algorithm based on cubic spline interpolation. Finally, the edge curve is ﬁtted with the least squares method to obtain its inner and outer diameter as well as the size of the wire diameter. The experimental data show that the algorithm has a mean square error of 4.8 µ m for the outer diameter and 0.18 µ m for the wire diameter. The outer diameter error is kept within ± 100 µ m and the wire diameter error can be kept within ± 15 µ m. Compared with the whole pixel algorithm, the measurement accuracy has been greatly improved.


Introduction
The engine is the core component of a car, and the O-ring plays a very important role in the airtight structure of a car's engine; the quality of the seal directly affects the life and reliability of the engine's operation [1,2]. There are currently thousands of O-ring manufacturers in China, but the size of an O-ring still requires manual inspection; in the face of large-scale testing needs, this is not only time-consuming and labor-intensive, but also, due to contact measurement, the detection accuracy is not high and is susceptible to the effects of personal subjective factors [3]. Current O-ring dimension measurement algorithms are mainly based on algorithms at the whole pixel level, such as the Sobel operator, Canny operator, morphological algorithms, etc. However, an algorithm at the whole pixel level has the disadvantages of having poor image edge localization and low accuracy of size measurements [4].
To address these problems with whole-pixel algorithms, the authors of this paper design a sub-pixel O-ring dimension measurement algorithm based on cubic spline interpolation, and achieve sub-pixel edge detection of O-rings through algorithmic processing. Compared with the whole-pixel edge detection algorithm, it has the advantages of a high detection accuracy, good image edge localization and strong detection stability. It can effectively improve the accuracy of current O-ring dimension measurements.

Image Acquisition System Design
The principle of the machine vision-based O-ring inspection method is to obtain a clear image of the seal using an industrial camera, and then apply the technology of image processing to obtain the dimensions of the inner and outer diameters, as well as the wire diameter. Therefore, capturing a clear image of the seal is the first step in the implementation of the measurement system [5].
The structure of the image acquisition system is shown in Figure 1, which mainly consists of high-definition industrial camera, inspection platform, lighting system, etc. The O-ring is placed on the inspection platform, the front and back lighting LED lights are turned on, the seal is irradiated, and then a high-definition camera is used to get a clear image of the O-ring. In order to meet the accuracy requirements of O-ring dimension measurements, the camera has a resolution of 3664 × 2748 pixels. The lens is an M1614-MP2, which has focal length of 16 mm [6].

O-Ring Dimension Measurement Algorithm Design
The edge of an image is the most basic feature of an image, and the accuracy of the positioning accuracy of an image's edge points directly affects the results of dimensional measurements. Edge points are pixel points in the image where there is a step change or roof change in the grey scale of the surrounding pixels, i.e., where the derivative of the grey scale value is large or extremely large. Traditional edge detection algorithms run fast, but accuracy is hard to achieve. Therefore, a traditional algorithm can be used to coarsely locate the edge position of the image, and then the coarsely located edge points are interpolated by cubic spline interpolation to find the sub-pixel edge with the largest derivative of the grey value, which can enhance the edge pixel point from the pixel level to the sub-pixel level, thus greatly improving measurement accuracy. Finally, the outer diameter and wire diameter of the O-ring are obtained via least squares circle fitting [7].

Pixel-Level Edge Algorithm Based on Noise-Resistant Mathematical Morphology
A morphologically based whole-pixel edge detection algorithm that compared and analyzed through traditional edge detection algorithms was adopted. Morphology was described with the help of the language of set theory and was mainly applied in image processing, such as in image refinement, pixelation, edge detection, noise removal, etc. It is one of the most widely used techniques in image processing. The basic operations of morphology include erosion, expansion, open operations, and closed operations [8].
The corrosion operation on A using S is denoted as AΘS = {z|(S) z ⊆ A }, corrosion can dissolve the boundary of an object. When selecting an appropriate size and shape of structural elements, it is possible to filter out all noise points that do not fully contain the structural elements. The expansion operation using S is denoted as In contrast to erosion, expansion expands the boundaries of objects, and is often used to bridge the same objects in an image that would otherwise be broken apart [9].
Both open and closed operations are compounded by corrosion and expansion; the open operation is corrosion followed by expansion, while the closed operation is expansion followed by corrosion. The open operation on A, using the structure element S, is denoted as A • S and can be expressed as A • S = (AΘS) ⊕ S. In general, the on operation makes the contour of the image smooth, breaks narrow connections and eliminates fine burrs, and the on-operation filters noise without significantly affecting the shape and contour of an object. The closed operation on A using structure element S is denoted as A•S = (A ⊕ S)ΘS. The closed operation also smooths the contour, but in contrast to the open operation, it usually bridges narrow interruptions and fills small holes [10].
These four basic operations are the basis for morphological edge detection [11]. The basic idea is to select the appropriate elements to perform mathematical morphological operations on a target image, and subtract the result from the original image to obtain the edge of the image. Let the image be represented by A, S is a suitable structural element; first let A be corrupted by S and then let image A be the difference with it, then inner edge image D 1 is obtained.
Similarly, using the morphological expansion algorithm, we can obtain outer edge image D 2 , which is expressed by: Using the expansion and erosion algorithm, gradient edge image D is obtained.
The three edge detection algorithms described above are composed simply by using a combination of erosion and expansion operations and do not achieve the desired level of accuracy for noise removal, especially Gaussian noise, or for edge localization. Through basic operations of mathematical morphology, it is known that the erosion and opened operations are good at suppressing peak noise in images, and the expansion and closed operations are good at suppressing trough noise in the image. Using this feature of mathematical morphology, traditional morphological edge detection algorithms can be improved [12].
Edge image D 4 is computed using a morphological edge detection algorithm that suppresses trough noise obtained using an expansion and closure operation, as shown in Equation (4), and edge image D 5 is computed using a morphological edge algorithm that suppresses peak noise obtained using an erosion and opening operation, as shown in Equation (5).
The above morphological operations are combined for the suppression characteristics of different noises. The four morphological operations are combined and applied to edge detection to obtain a noise-resistant mathematical morphological edge detection algorithm, and can be expressed as Equation (6).
Using mathematical morphology for image processing, in addition to the flexible combination of the four basic operations, different structural elements can also have an impact on edge detection [13]. Moreover, for the extraction of edges using expansion etching, a structural element will only significantly work on edges that are perpendicular to it. Therefore, we used a set of four-direction structure elements as morphological structure elements, as shown in Figure 2. (a), (b), (c), and (d) are used as structural elements, respectively, in the noise-resistant mathematical morphology edge detection algorithm. After four structure elements are calculated, the four structures are summed with average weights to obtain the final processed image. The expression for this is Equation (7).
The Sobel operator, Canny operator and noise-resistant mathematical morphology edge detection algorithm were used to detect the edge of an O-ring, respectively, and the obtained results are shown in Figure 3. It can be seen that the obtained O-ring edge image using the algorithm in this paper is more delicate compared with other classical algorithms, the extraction of the edge is more accurate, and the details of the edge can be better retained. This vision measurement system uses a noise-resistant mathematical morphology edge detection algorithm for whole-pixel edge extraction of O-ring seals.

Sub-Pixel Level Edge Measurement Algorithm Based on Cubic Spline Interpolation
As vision measurement becomes more and more widely used in industrial production, the accuracy of measurements is becoming more and more important to people [14]. Positioning edge locations to pixel-level accuracy is also increasingly unsatisfactory. The sub-pixel edge detection algorithm is used to first coarsely localize the image edges to the pixel level using an integer pixel edge detection algorithm, and then the pixel, the basic unit, is subdivided by analyzing the grey values around the integer pixel so that the edges are localized to the sub-pixel level [15].
In order to accurately determine the interpolation nodes, we need to know the direction of the grey gradient of the whole-pixel edge points of an image [16]. A whole pixel point is used first and a 7 × 7 rectangular area point is drawn around it, as shown in Figure 4. In the figure, a, b, c and d represent one grey gradient direction, and the angle between two adjacent grey gradient directions is 45 • [17].
The absolute value of the difference between the greyscale values of adjacent pixels in the four gradient directions a, b, c and d are summed, and the sum of the absolute values E i is the maximum of the greyscale gradient direction of the pixels [7,18]. The calculation formula is as follows: When the gradient direction is obtained, the greyscale and coordinate values of the original image in the gradient direction are taken out and used in the following cubic spline interpolation [18].
Cubic spline interpolation is a set of nodes, , using these data points to construct a cubic spline function S(x) and the function needs to satisfy the following three conditions: is a sectional cubic polynomial, S (x) is a primary polynomial over all of the subintervals x j−1 , x j . Denote h j = x j − x j−1 , j = 1, 2, · · · , N + 1. Then on x j−1 , x j there is: where h j = x j − x j−1 . Integrating Equation (9) twice over [x j−1 , x j ] yields the expressions for S(x) and S (x).
where C 1 and C 2 are constants, the interpolation condition is S(x j−1 ) = y j−1 , and S(x j ) = y j can be used to find the integration constant as: Substituting the integration constants into Equation (11) and organizing it gives Then, finding the derivative of S(x) to obtain, With Equation (12) we can see that the key to finding S(x) is to determine M j = S (x),j = 1, 2, · · · , N + 1.
Since node x j (j = 1, 2, · · · , N) is continuously differentiable at S (x), then, From Equation (14) we get: Then, the above equation can be reduced to: where ). Equation (16) is a linear system of equations with respect to M 0 , M 1 , · · · , M N+1 . To determine these N + 2 unknowns, two additional boundary conditions are required.
Since we do not know the values of the first and second order derivatives of S(x) on boundaries x 0 and x N+1 , here we choose to use the natural boundary conditions, i.e., S (x 0 ) = S (x N+1 ) = 0, M 0 = M N+1 = 0. Then M j is written in the form of a matrix: With the coordinate values in the direction of the gradient as the independent variable, i.e., x j (j = 1, 2, . . . , 7), as a function of the greyscale value of the corresponding pixel, i.e., f (x j ) = p j , cubic spline interpolation is performed to obtain the cubic spline function S(x) [19]. Then, the second order derivative of its third spline function S (x) is found.
Since the grey value in the gradient direction must be monotonically decreasing and the sub-pixel edge points are the points where the grey value changes most significantly in the gradient direction, the sub-pixel edge points must follow two principles [20]: (1) S (x 0 ) = 0 and the positive and negative signs on both sides of S (x 0 ) are not equal.
(2) If there are multiple inflection points for the second order derivative S (x), choose the minimal value point on the first order derivative.
Using the above theoretical calculation, the sub-pixel edge point coordinate x jmax in the direction of the gray gradient is obtained. The coordinates of the point consist of the integer part x pixel jmax and the fractional part x subpixel jmax , using the following Equation (18) [21].
where the integer part x pixel jmax of x jmax is the pixel-level coordinate of the edge point, and the fractional part x subpixel jmax of x jmax is the sub-pixel-level coordinate of the edge point. The coordinates of the corresponding pixel levels (x, y) in the greyscale map can be determined from the integer part of x jmax . According to the direction of the gradient, the calculation formulas of sub-pixel coordinates (x subpixel , y subpixel ) are as follows [22]: If the direction of the grey gradient is the 'a' direction the expression is Equation (19): x subpixel = x y subpixel = y + x subpixel jmax (19) If the direction of the grey gradient is the 'b, c' direction the expression is Equation (20): If the direction of the grey gradient is the 'd' direction the expression is Equation (21): A flow chart of the whole algorithm is shown in Figure 5.
As the theoretical part is rather complex, an example of the calculation of the sub-pixel coordinates of an O-ring contour point is presented. Taking the coordinates of a point (136,528) of the O-ring contour point as an example, the grey scale values of the pixels in a 7 × 7 rectangular area centered on this point is shown in Figure 6.
According to the gradient direction formula (Equation (8)), we can calculate that E a = 25,E b = 181, E c = 185, E d = 186. Therefore, the gradient direction is direction d.
The third spline interpolation is carried out for each point in direction d. The image of the third spline function S(x), the first order derivative S (x), and the second order derivative S (x) are shown in Figure 7. The calculation gives x jmax = 1.8159. Finally, the coordinates of the sub-pixel edge point are (134.5769, 530.5769) according to the formula for sub-pixel coordinates.
As shown in Figure 8, a sub-pixel edge point extraction map of the O-ring is shown. Since the sub-pixel edge points cannot be represented in the pixel map, the coordinate points are plotted to visually reflect the sub-pixel edge extraction results [23].

Least Square Method for Circle Fitting
After obtaining its sub-pixel edge points, it is necessary to fit the sub-pixel edge points by least squares and then obtain the center point coordinates of the circle, as well as the radius diameter and other related parameters [24].
We know that the curve of a circle is given by Equation (22): Let a = −2A,b = −2B,c = A 2 + B 2 − R 2 , then another expression for the equation of the circular curve can be obtained as Equation (23): The sub-pixel level edge coordinates of the O-ring are obtained by the cubic spline interpolation algorithm and are recorded as the sample (X i , Y i ), (i = 1, 2, 3, . . . , N). Let the distance of the sample to the center of the circle be d i . The expression is Equation (24): The difference between the square of the distance from the sampling point (X i , Y i ) to the fitted circle and the square of the radius of the fitted circle is Equation (25) [25].
Let Q(a, b, c) be the sum of squares of σ i , Equation (26), So, it is only necessary to find a, b, c such that the value of Q(a, b, c) is minimized. The final solution for a, b, c is [26], Among them: The radius and the coordinates of the center point of the circle can be found by substituting the sample (X i , Y i )(i = 1, 2, 3, . . . , N) into the above least square method. As shown in Figure 9, Figure 9a shows the O-ring outer diameter circle fitting and Figure 9b shows the O-ring inner diameter circle fitting.

Calibration Experiments
To get the actual size of the O-ring from the sub-pixel image, we have to know the actual size of each pixel point of the camera, so the pixel calibration must be done in advance [27].
We have selected a standard block with a diameter of 17 mm using a sub-pixel edge measurement algorithm based on third spline interpolation for 20 measurements to obtain the pixel diameter size of the fitted circle [28]. The image of standard gauge block is shown in Figure 10. Then its actual value is calculated as a ratio to the pixel length, here we call it K-value. k-value is calculated as follows: where r is the diameter of the block, d pixel is the pixel diameter size of the block, and the k subpixel value is in mm/pixel. The measurement results are shown in the data in Table 1, and we averaged the 20 values obtained so as to obtain a standard k subpixel value of 0.037623 mm/pixel. In order to compare with the results of the algorithm in this paper, the classical algorithms, the Sobel and Canny algorithms, were chosen here for a standard block with a diameter of 17 mm and also 20 measurements were carried out, consistent with the above principle, and obtained k sobel = 0.037202 mm/pixel, k canny = 0.037305 mm/pixel respectively.

Measurement Experiments
In order to evaluate the accuracy of the algorithm measurement, a qualified O-ring with an outside diameter of 20 mm and a wire size of 2.3 mm was selected and measured 20 times using the algorithm in this paper. Additionally, for comparison with the algorithms in this paper, the classical whole-pixel edge detection algorithms, the Sobel and Canny algorithms, were chosen here to also perform 20 measurements for calculation, and the results are shown in Table 2, all in mm.

Analysis of Results
In terms of outside diameter measurement, it can be seen from Table 2 that the mean square error of the three algorithms is calculated separately using the outside diameter of 20 mm as the true value, and the mean square error using the algorithm of this paper is 0.00480, while for the Sobel algorithm it is 0.02246 and for the Canny algorithm it is 0.00858. The comparison graph of the outside diameter measurement error in Figure 12a shows that the measurement error of the algorithm in this paper can be stable within 0.1 mm, while the measurement errors of both the Sobel and Canny algorithms exceed 0.1 mm. Drawing the comparison graph of the outside diameter measurement values of the three algorithms, as shown in Figure 11a, and observing the dispersion of the data, we can see that the algorithm in this paper also has good stability. Therefore, in terms of outside diameter measurement, the algorithm in this paper has better accuracy and stability than the classical algorithms Sobel and Canny for a whole pixel.  In terms of wire diameter measurement, it can be seen from Table 2 that the mean square error of the three algorithms was calculated separately using the wire diameter of 2.4 mm as the true value, and the mean square error using the algorithm of this paper was 0.00018, while the Sobel algorithm was 0.00143 and the Canny algorithm was 0.00101. Analyzing the comparison graph of the line diameter measurement error in Figure 12b, only the measurement error obtained using the algorithm of this paper exceeds −0.015 mm for −0.021 mm for the 10th sample, and the rest of the measurement errors can be stably maintained within −0.015 mm, while the measurement errors of the Sobel and Canny algorithms both exceed −0.02 mm. Drawing a comparison of the wire diameter measurements of the three algorithms, as shown in Figure 11b, and observing the dispersion of the data, we can see that the algorithm in this paper also has good stability. Therefore, the algorithm of this paper has better accuracy and stability than the classical whole-pixel algorithms, the Sobel and Canny algorithms, in terms of line diameter measurements.

Conclusions
In this paper, we first used the noise-resistant mathematical morphology edge detection algorithm to coarsely locate the edges of an O-ring image, then we used cubic spline interpolation to precisely locate the O-ring edges at the sub-pixel level, and finally performed circle fitting by least squares to calculate the line diameter and diameter of the Oring. The experimental data show that the algorithm has a mean square error of 0.00480 mm for the outer diameter and 0.00018 mm for the wire diameter. The outer diameter error is kept within ±0.1 mm and the wire diameter error can be kept within ±0.015 mm. By comparing with the whole-pixel edge detection algorithms (Sobel and Canny), the algorithm in this paper has a higher accuracy and good stability in the dimensional measurement of O-rings. This meets the design requirements of the algorithm.

Conflicts of Interest:
The authors declare no conflict of interest.