# Follow-Up Control and Image Recognition of Neck Level for Standard Metal Gauge
## 3 Image Segmentation Algorithm Based on Edge Detection and K-Means Clustering Algorithm
### Improved Edge Detection Algorithm for Image  
Bilateral filtering was used to denoise the original image while preserving edge features, which facilitated vertical edge detection for the liquid tube and scale. Edge information in other directions was lessened to reduce interference. This improved vertical edge detection model was designed using specific image features and a conventional edge detection algorithm. The edges of the image are a set of points on the boundary of two different characteristic regions, reflecting the discontinuity and distortion of local features. As a result, most of the useful information contained in the image resides on the edge. Edges are denoted by large discrepancies between a pixel and its neighboring pixels, which can be represented by a vector with the attributes of amplitude and direction [14; 15]. Common edge detection algorithms include the Roberts, Prewitt, Sobel, Canny, and Laplace operators. The Roberts operator uses only the \(2\times 2\) neighborhood of the current pixel, which is computationally simple but sensitive to noise. This operation can be expressed mathematically as: [16]:  
\[G_{1}=\left[ \begin{array}{cc}1&0\\ 0&-1\\ \end{array} \right]\,G_{2}=\left[ \begin{array}{cc}0&1\\ -1&0\\ \end{array} \right] \tag{3}\] \begin{table}
\begin{tabular}{c c c} \hline \hline
**Index** & **Bilateral Filtering** & **Anisotropic Diffusion** \\ \hline PSNR & 41.5381 & 35.9861 \\ SSIM & 0.9952 & 0.9908 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: An evaluation index for the two filtering algorithms. The Prewitt operator reduces the influence of noise on edges using a \(3\times 3\) neighborhood, defined as [17]:  
\[G_{x}=\left[ \begin{array}{ccc}-1&-1&-1\\ 0&0&0\\ 1&1&1\end{array} \right]\,G_{y}=\left[ \begin{array}{ccc}-1&0&1\\ -1&0&1\\ -1&0&1\end{array} \right] \tag{4}\]  
The final edge image can then be acquired using \(G=max\big{(}G_{x},G_{y}\big{)}\) or \(G=G_{x}+G_{y}\). The Sobel operator improves on the accuracy of the Prewitt detection by using a weighted approach to calculate differences. This technique not only detects edge points more accurately, it can further suppress the influence of noise. The operator can be represented as [18]:  
\[G_{x1}=\left[ \begin{array}{ccc}-1&0&1\\ -2&0&2\\ -1&0&1\end{array} \right]\,G_{y1}=\left[ \begin{array}{ccc}-1&-2&-1\\ 0&0&0\\ 1&2&1\end{array} \right] \tag{5}\]  
Sobel edge detection is one of the most common discrete differential operators. It can be used to calculate the norm of a gradient vector corresponding to pixel points, indicating edges in either the horizontal or vertical direction. As such, the Sobel operator is better suited for processing edge information along the X and Y axes (horizontal and vertical vector components) and can be inaccurate for angled structures. In contrast, the Canny [19] operator identifies edges using the maximum value of an image signal function. This involves removing noise with a Gaussian filter, calculating the amplitude and direction of the gradient, performing nonmaximum value suppression, and connecting the edges of an image with a lag threshold technique. The Canny operator also exhibits strong noise reduction capabilities. It can balance noise, effectively detect edge information, and smooth pixel gradients [20]. The Laplace kernel is an isotropic two-order differential operator. It is independent of direction and exhibits rotational invariance. Its second derivative is defined as [21]:  
\[\
abla^{2}f(x,y)=\frac{\partial^{2}f}{\partial x^{2}}+\frac{\partial^{2}f}{ \partial y^{2}} \tag{6}\]  
The Laplace template is:  
\[G_{11}=\left[ \begin{array}{ccc}0&1&0\\ 0&-4&1\\ 0&1&0\end{array} \right]\,G_{21}=\left[ \begin{array}{ccc}1&1&1\\ 1&-8&1\\ 1&1&1\end{array} \right] \tag{7}\]  
This approach is sensitive to singular and boundary points and is often applied to image sharpening.