## 3 Materials and Methods
### Size Measurement Method
#### 3.2.3 Sub-Pixel Edge Detection  
The central method of the lock cylinder bead hole size measurement suggested in this paper is sub-pixel edge detection. This process can be roughly separated into two steps: (1) pixel-level edge localization based on the Canny operator [52] and (2) accurate sub-pixel edge localization based on the [PERSON] [53] sub-pixel edge detection algorithm. 1. The steps of the Canny operator to find the edge points are as follows. 1. Smooth the image with a Gaussian filter. The obtained ROI region has been Gaussian filtered in Section 3.2, so this step is omitted. 2. Calculate the gradient magnitude and direction. In this paper, the first-order partial derivative finite difference is used to calculate the gradient magnitude and direction, as shown in Equations (2)-(6). \[s_{x}= \begin{bmatrix}-1&1\\ -1&1\end{bmatrix} ,s_{y}= \begin{bmatrix}1&1\\ -1&-1\end{bmatrix} \] (2) \[P[i,j]=\frac{1}{2}(f[i+1,j]-f[i,j]+f[i+1,j+1]-f[i,j+1])\] (3) \[Q[i,j]=\frac{1}{2}(f[i,j]-f[i,j+1]+f[i+1,j]-f[i+1,j+1])\] (4) \[M[i,j]=\sqrt{P[i,j]^{2}+Q[i,j]^{2}}\] (5) \[\theta[i,j]=arc\tan(Q[i,j]/P[i,j])\] (6)  
where \(f\) is the gray value of the image, \(P\) represents the gradient amplitude in the \(X\) direction, \(Q\) represents the gradient amplitude in the \(Y\) direction, \(M\) is the amplitude of the point, and \(\theta\) is the gradient direction, that is, the angle. 3. Non-maximum suppression of the gradient magnitude. As shown in Figure 7: g\({}_{1}\), g\({}_{2}\), g\({}_{3}\), and g\({}_{4}\) all represent pixel points. Obviously, they are four of the eight neighborhoods of point c. Point c in Figure 7 is the point we need to judge. The blue line is its gradient direction; that is to say, if c is a local maximum, its gradient magnitude M needs to be greater than or equal to the intersection of the line with g\({}_{1}\)g\({}_{2}\) and g\({}_{3}\)g\({}_{4}\), that is, the gradient magnitudes at the two points p and q. However,p and q are not integer pixels but sub-pixels; that is, the coordinates are floating points. We used linear interpolation to find their gradient magnitudes. For example, p is between g1 and g2, and the magnitudes of g1 and g2 are known. As long as we know the ratio of p between g1 and g2, we can obtain its gradient amplitude, and the ratio can be calculated by the included angle \(\theta\), which is the direction of the gradient. As shown in Formulas (7) and (8): set the amplitude of g1 M(g1), the amplitude of g2 M(g2), then p can be easily obtained. \[M(p)=w\times M(g_{2})+(1-w)\times M(g_{1}) \tag{7}\]  
\[w=distance(p,g_{2})/distance(g_{1},g_{2}) \tag{8}\]  
distance(\(p,g_{2}\)) represents the distance between points \(p\) and \(g_{2}\), and distance(\(g_{1},g_{2}\)) represents the distance between points \(g_{1}\) and \(g_{2}\). In fact, w is a scale coefficient, which can be obtained by the gradient direction (tangent and cotangent of the argument \(\theta\)). The Canny algorithm has been employed to achieve coarse pixel-level positioning of the edge of the lock cylinder's bead hole. However, it is evident that this method alone is insufficient to meet the high accuracy requirements of industrial measurements. Therefore, the revised Devernay algorithm is utilized in this paper to determine the sub-pixel edge of the lock cylinder's bead hole, leveraging the information obtained from the edge points. This refined algorithm allows for more precise and accurate determination of the bead hole's sub-pixel edge. Building upon the Devernay sub-pixel edge detection algorithm, accurate sub-pixel edge finding is achieved. [PERSON] further refined the Canny algorithm by proposing that the new edge point can be determined as the maximum value of the difference between multiple adjacent gradient modulus values. This value can be interpolated using a quadratic function based on the gradient modulus values at three adjacent points along the gradient direction. Figure 8 provides a visual representation of this process. Figure 7: Gradient amplitude for non-maximum suppression.