# Glass Defect Detection with Improved Data Augmentation under Total Reflection Lighting
## 3 Principle and Improvement of Defect Detection Algorithm
### Data Augmentation and Improvement
#### 3.4.2 Entropy Segmentation  
The image can be divided into a foreground image and background image, and the former is the ideal target to be detected. Edge-based, threshold-based, and region-based approaches are the common defect segmentation methods. The second method, which is the most intuitive, is simple and fast to process, making it the most commonly used image segmentation method. The defect area accounts for a small proportion of the defect image of the mobile phone flat glass. The image histogram takes the form of a single-peak distribution, and there are many kinds, scales, and shapes of defects in mobile phone flat glass. As a result, global threshold-based segmentation always fails to perform well. In addition, local threshold-based segmentation produces target images with poor connectivity and fails to retain the edge information of defects. In order to ensure that the edge information of the image can be preserved completely in the later image fusion, this paper divided the complete glass defect image into single images for later segmentation. In this way, the original edge information can be retained and the needs of the later image fusion can also be met. Figure 12: Frequency domain filtering. (**a**) Original defect image; (**b**) frequency domain image; (**c**) filtered image. Figure 13: Comparison experiment. This paper adopted the one-dimensional maximum entropy segmentation method. Information entropy is a concept created to measure the amount of information lost. Entropy actually defines the uncertainty of a random variable, where the maximum value means the maximum uncertainty of a random variable. The gray scale of a picture is L, whose probability distribution is represented by \(P_{1}\), \(P_{2}\), \(P_{3}\), \(\ldots\), \(P_{r}\). Assume gray scale \(t\) as the segmentation threshold and the image constituting the background (B) as a pixel lower than scale \(t\), and  
\[P_{b}=\sum_{i=1}^{t}P_{i} \tag{7}\]  
\[i=(1,2,3,\ldots,t)\]  
represents the sum of the gray scale probabilities from 1 to \(t\). Then, the probability distribution of each gray scale in the whole background pixel is \(\frac{P_{1}}{P_{b}}\), \(\frac{P_{1}}{P_{b}}\), \(\frac{P_{2}}{P_{b}}\), \(\frac{P_{3}}{P_{b}}\), \(\ldots\), \(\frac{P_{3}}{P_{b}}\). The pixels constituting the target image (\(O\)) are higher than the gray scale \(t\). Assume  
\[P_{0}=\sum_{i=t+1}^{l}P_{i} \tag{8}\]  
\[i=(t+1,t+2,t+3,\ldots,l)\]  
represents the sum of the gray scale probabilities from \(t+1\) to l, and then the probability distribution of each gray scale in the target image within the whole pixel of the target image is \(\frac{P_{i+1}}{1-P_{b}}\), \(\frac{P_{i+2}}{1-P_{b}}\), \(\frac{P_{i+3}}{1-P_{b}}\), \(\ldots\), \(\frac{P_{3}}{1-P_{b}}\). The information entropy of the background and target images is calculated as follows:  
\[H(B)=-\sum_{i=1}^{t}\frac{P_{i}}{P_{b}}ln\frac{P_{i}}{P_{b}} \tag{9}\]  
\[i=(1,2,3,\ldots,t)\]  
\[H(O)=-\sum_{i=t+1}^{l}\frac{P_{i}}{1-P_{b}}ln\frac{P_{i}}{1-P_{b}} \tag{10}\]  
\[i=(t+1,t+2,t+3,\ldots,l)\]  
\(H(B)\) represents the information entropy of the background, \(H(O)\) is the information entropy of the target image, and the sum \(\Psi(t)\) of the information entropy of \(B\) and \(O\) is  
Figure 13:0  
Figure 13:1  
We can search all the gray scales and find out \(\Psi(t)\), the maximum value \(t\), the optimal dividing line threshold. The defect mask image, generated after image segmentation, is a binary image composed of 0 and 1. We multiply the original image with the mask image to acquire a completely extracted region with only the defect itself, and then eliminate the background area. If an image with a size of \(M\times N\) is represented as \(A_{(M\times N)}\), its mask image is \(Mask_{(M\times N)}\), and its extracted target area is \(ROI\), then the calculation formula is as follows:  
Figure 13:2  
The extraction results are shown in Figure 13:3