# Research on O-ring Dimension Measurement Algorithm Based on Cubic Spline Interpolation
## 3 O-Ring Dimension Measurement Algorithm Design
### 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 \(\times\) 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\({}^{\circ}\)[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:  
\[E_{i}=\sum_{j=1}^{7}\big{|}p_{j+1}-p_{j}\big{|},(i=a,b,c,d) \tag{8}\]  
Figure 3: Comparison of noise-resistant mathematical morphological edge detection algorithm and whole-pixel edge detection algorithm. (**a**) Original image; (**b**) noise-resistant mathematical morphology edge detection results; (**c**) the Sobel operator edge detection result; (**d**) plot of the edge detection results of the Canny operator. 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, \(a=x_{0}<x_{1}<\cdots<x_{N}<x_{N+1}=b\) on \([a,b]\), each node (\(x_{i}\)) corresponding to a data point (\(x_{i},f(x_{i})\)), using these data points to construct a cubic spline function \(S(x)\) and the function needs to satisfy the following three conditions:  
1. \(S(x)\in C^{2}[a,b]\), i.e., \(S(x)\),\(S^{\prime}(x)\),\(S^{\prime\prime}(x)\) is continuous on \([a,b]\);
2. \(S(x)\) is a cubic polynomial over each small interval \([x_{i},x_{i+1}]\);
3. \(S(x_{i})=f(x_{i})=y_{i}(i=0,1,\cdots,N+1)\)  
Let \(M_{j}=S^{\prime\prime}(x_{j}),j=0,1,\cdots,N+1\), since \(S(x)\) is a sectional cubic polynomial, \(S^{\prime\prime}(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,\cdots,N+1\). Then on \(\big{[}x_{j-1},x_{j}\big{]}\) there is:  
\[S^{\prime\prime}(x)=M_{j-1}\frac{x_{j}-x}{h_{j}}+M_{j}\frac{x-x_{j-1}}{h_{j}},x\in[x_{j-1},x_{j}] \tag{9}\]  
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^{\prime}(x)\). \[S^{\prime}(x)=-\frac{M_{j-1}}{2h_{j}}(x_{j}-x)^{2}+\frac{M_{j}}{2h_{j}}(x-x_{j -1})^{2}+C_{1} \tag{10}\]  
\[S(x)=\frac{M_{j-1}}{6h_{j}}(x_{j}-x)^{3}+\frac{M_{j}}{6h_{j}}(x-x_{j-1})^{3}+C _{1}x+C_{2} \tag{11}\]  
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:  
\[C_{1}=\frac{h_{j}}{6}(M_{j-1}-M_{j})+\frac{1}{h_{j}}(y_{j}-y_{j-1})\]  
\[C_{2}=\frac{h_{j}}{6}(x_{j}M_{j-1}-x_{j-1}M_{j})+\frac{1}{h_{j}}(x_{j}y_{j-1}- x_{j-1}y_{j})\]  
Figure 4: Schematic diagram of gradient direction. Substituting the integration constants into Equation (11) and organizing it gives  
\[ \begin{array}{c}S(x)=\frac{M_{j-1}}{6h_{j}}(x_{j}-x)^{3}+\frac{M_{j}}{6h_{j}}(x -x_{j-1})^{3}+(y_{j-1}-\frac{M_{j-1}h_{j}^{2}}{6})\frac{x_{j}-x}{h_{j}}\\ +\left(y_{j}-\frac{M_{j}h_{j}^{2}}{6}\right)\frac{x-x_{j-1}}{h_{j}}\end{array}