# Study of Surface Defect Detection Techniques in Grinding of SiCp/Al Composites
## 3 Extraction of Characteristic Parameters of Surface Defects
### Edge Detection  
The Canny algorithm [24] is used for edge detection. The Canny edge detection algorithm is normally divided into four steps: (1) smoothing the image using a Gaussian filter, (2) calculating the gradient value and direction of each pixel point, (3) applying non-maximum suppression to filter pixel points with small gradient values in the local region, and (4) filtering and connecting edges with a dual-threshold algorithm. To explain the edge detection process, six detected defects on the machined surface were randomly selected and clipped using OpenCV to obtain a sample image of the defect region, as shown in Figure 6. In optical images, regions with different depths of field have different levels of sharpness. The background information is clearer and more complex, the interior of the defect is blurred, and no smoothing operation is applied, which helps distinguish the defect from the background region. Therefore, the image-smoothing operation was skipped, and gradient computation was performed directly. The Sobel filter is used to calculate gradient amplitude and direction. The Sobel filters for the x and y directions are as follows:  
\[S_{x}= \begin{bmatrix}-1&0&1\\ -2&0&2\\ -1&0&1\end{bmatrix} \,S_{y}= \begin{bmatrix}1&2&1\\ 0&0&0\\ -1&-2&-1\end{bmatrix}  \tag{5}\]  
Here, \(S_{x}\) denotes the \(x\)-direction Sobel filter for detecting edges in the vertical direction. \(S_{y}\) denotes the \(y\)-direction Sobel filter for detecting edges in the horizontal direction. If a \(3\times 3\) window in the image is \(A\), the gradient value (\(G\)) and direction (\(\theta\)) at pixel \(C\) can be calculated using Equation (6):  
\[G=\sqrt{G_{x}^{2}+G_{y}^{2}}\,\theta=\arctan\bigl{(}G_{y}/G_{x}\bigr{)} \tag{6}\]  
Here, \(G_{x}\) is the gradient in the x-direction and \(G_{y}\) is the gradient in the y-direction, and \(G_{x}\) and \(G_{y}\) are obtained from Equations (7) and (8):  
\[G_{x}=S_{x}*A= \begin{bmatrix}-1&0&1\\ -2&0&2\\ -1&0&1\end{bmatrix} * \begin{bmatrix}A_{1}&A_{2}&A_{3}\\ A_{4}&C&A_{5}\\ A_{6}&A_{7}&A_{8}\end{bmatrix}  \tag{7}\]  
Figure 6: Samples of the defective regions. \[G_{y}=S_{y}*A= \begin{bmatrix}1&2&1\\ 0&0&0\\ -1&-2&-1\end{bmatrix} * \begin{bmatrix}A_{1}&A_{2}&A_{3}\\ A_{4}&C&A_{5}\\ A_{6}&A_{7}&A_{8}\end{bmatrix}  \tag{8}\]  
It should be noted that the symbol \(*\) represents the convolution operation. The gradient values and orientations of each pixel can be obtained by sliding the Sobel filter over the image. After performing gradient computation on the image, the edges extracted based on the gradient values remain blurred. For edges, there is, and should be, only one exact response. Non-maximum suppression is an edge sparsity technique that can help suppress all gradient values other than the local maximum to zero. The gradient of the current pixel is compared with two neighboring pixels in the same direction, and if the gradient of the current pixel is greater than the gradient of the other two pixels, the pixel is retained as an edge point; otherwise, the pixel is suppressed. After non-maximum suppression, double-threshold filtering was performed. Two thresholds were set, high and low, where pixels above the threshold are considered as strong edges. Pixels below the high threshold and above the low threshold are considered as weak edges. They are set to zero for pixels below the low filtering value. For those considered as strong edge points, they are retained directly. For those considered as weak edge points, it is necessary to determine whether there are strong edge points in their eight connected field pixels; if they are present, the weak edge points are retained, and they are suppressed if they are absent. Finally, the edges can be connected. In addition, the value of the double threshold is uncertain due to the difference in the brightness and contrast of the images in different defect regions. Therefore, edge detection in samples from different defect regions is achieved here by manually adjusting the value of the double threshold.