# Research on a Surface Defect Detection Algorithm Based on MobileNet-SSD
## 2 Surface Defect Detection Method
### Image Pre-Processing
#### 2.1.2 Defect Area Planning  
The defect samples contain lots of useless background information that may affect the recognition quality of the detection algorithms. Our defect samples carry the following features: the defects concentrated on the sealing surface, whose round shape remains unchanged. In view of these, the Hough circle detection was used in the pre-processing phase to locate the edge of the cover, and mitigate the impact of useless background on the recognition accuracy. The Hough circle transform begins with the extraction of edge location and direction with a Canny operator. The Canny operation involves six steps: RGB to gray conversion, Gaussian filtering, gradient and direction computation, non-maximum suppression (NMS), double threshold selection and edge detection. Among them, Gaussian filtering is realized by the two-dimensional (2D) Gaussian kernel convolution:  
\[\mathrm{K}=\frac{1}{2\pi\sigma^{2}}\mathrm{e}^{-\frac{\sigma^{2}+\sigma^{2}}{ 2\sigma^{2}}} \tag{1}\]  
The purpose of Gaussian filtering is to smoothen the image and remove as much noise as possible. Then, the Sobel operator was introduced to obtain the gradient amplitude and its direction, which can enhance the image and highlight the points with significant changes in neighboring pixels. The operator contains two groups of 3 \(\times\) 3 kernels. One group is transverse detection kernels, and the other is vertical detection kernels. The formula of the operator is as follows:  
\[\mathrm{G_{x}}=\left[\begin{array}{ccc}-1&0&+1\\ -2&0&+2\\ -1&0&+1\\ \end{array}\right]*\mathrm{A},\mathrm{G_{y}}=\left[\begin{array}{ccc}+1&+2&+ 1\\ 0&0&0\\ -1&-2&-1\\ \end{array}\right]*\mathrm{A} \tag{2}\]  
where A is a smooth image; Gx is an image after transverse gradient detection; and Gy is an image after vertical gradient detection. The gradient can be expressed as:  
\[|\
abla f|=\mathrm{mag}(\
abla f)=\left[(\frac{\partial f}{\partial x})^{2}+( \frac{\partial f}{\partial y})^{2}\right]^{1/2} \tag{3}\]  
\begin{table}
\begin{tabular}{c c} \hline \hline
**Data Expansion Mode** & **Changes** \\ \hline Rotation & From 0\({}^{\circ}\) to 10\({}^{\circ}\) random rotation of images \\ \hline Horizontal migration & Horizontal migration images, and the offset value is 10\% of the length of the images \\ \hline Vertical migration & Vertical migration images, and the offset value is 10\% of the length of the images \\ \hline Scaling & Enlarge the images to 10\% scale \\ \hline Tangential transformation & Stretch the pixel points horizontally. The stretch value is 10\% of the pixel points to the vertical axis distance \\ \hline Horizontal flip & The images are randomly inverted with a horizontal axis as the symmetry axis \\ \hline \hline \end{tabular}
\end{table}
Table 1: The implementation method of data expansion.  
The gradient direction can be expressed as:  
\[\mathrm{a}(x,y)=\mathrm{arctan}(\frac{\mathrm{G}_{y}}{\mathrm{G}_{x}}) \tag{4}\]  
During edge detection, the NMS was adopted to find the maximum gradient of local pixels by comparing the gradients and gradient directions. After obtaining the binary edge image, the Hough circle transform was employed to detect the circles. Under the coordinate system \((\mathrm{c}_{1},\mathrm{c}_{2},\mathrm{r})\), the formula of Hough circle transform can be described as:  
\[(x-\mathrm{c}_{1})^{2}+(y-\mathrm{c}_{2})^{2}=\mathrm{r}^{2} \tag{5}\]  
where \((\mathrm{c}_{1},\mathrm{c}_{2})\) is the center of the center coordinates; and r is the radius. The detection was realized through the following steps: first, the non-zero points in the image are traversed, and line segments along the gradient direction (radius direction) and the opposite direction are drawn. The intersection point of the segments is the circle center. Then, the maximum circle is obtained by setting the threshold value. After that, the rectangular regions are generated by the maximum radius. The size of the image was normalized to 300 \(\times\) 300 \(\times\) 3. Figure 2 shows the regional planning process.