## 2 Theoretical Background and Proposed Method
### Adaptive Threshold Segmentation
It is clear that the adaptive threshold segmentation produces a much better result than the OTSU algorithm, because the defect in the image has been segmented with accuracy. The reason is that the adaptive threshold segmentation algorithm takes advantage of the fact that a target object is brighter among its local background. In regions without a target object, the gray scale changes in a graded manner, with the result that the original gray scale differs not much from the smoothed one (less than or equal to \(T\)), and is then regarded as the background. In contrast, a region with a target object changes drastically in gray value, with the result that the original gray differs significantly from the smoothed one (greater  
than \(T\)), and is then regarded as an object. The adaptive threshold segmentation algorithm focuses on local gray variation and is therefore more robust than fixed threshold segmentation algorithms when the target objects are smaller and the background gray is more complex, but undesirably, the noise points and the edges of light regions or dark regions in the image, which change drastically too in gray, are segmented out too. As shown in Figure 2d, some portions not related to defects are segmented out. Figure 1: Example of adaptive threshold: (**a**) original image; (**b**) smoothed image; the light green pixels indicate the source neighborhood for the light green destination pixel; (**c**) true thresholds of original image when \(T=5\); (**d**) result of adaptive threshold. Figure 2: Segmentation results using adaptive threshold and OTSU methods: (**a**) original image with defect; (**b**) result of OTSU method; (**c**) smoothing image by a \(25\times 25\) mean filter; (**d**) result of adaptive threshold method. The above analysis shows that the smoothing factor size and the threshold value are two determining factors in the adaptive threshold segmentation algorithm. Suitable operators for image smoothing are mean filtering, Gaussian filtering, or median filtering operators, the size of which is the size of the local area. This size determines the size of the objects that can be segmented. Too small a filter size is unable to give an accurate estimate of the local background brightness at the center of the object, resulting in segmentation failure. The larger the filter size, the better the filtered result will represent the local background, and the more likely the object is accurately segmented out. But too large a filter size will result in higher computational load, and adjacent objects, too, may have an undesirable effect on the filtering results. Experience suggests that when the filter is about the size of the object to be recognized, an accurate estimation of the background gray level of the defect and an accurate segmentation of the defect can be obtained at once. The value of the threshold \(T\) varies with the object to be segmented. A larger threshold suppresses the noise better, but may lead to the loss of the edge pixels of the target object, resulting in incomplete segmentation. A smaller threshold ensures that the target object is completely segmented, but noise and light and dark edges may exert some influence. For Figure 2a, the segmentation results of a smaller and a larger filter size but with the \(T\) value maintained at 2 are shown in in Figure 3a,b, respectively, while the segmentation results of a smaller \(T\) value and a larger \(T\) value but with the filter size maintained unchanged are shown in in Figure 3c,d, respectively. Figure 3a shows the segmentation result when the filter for smoothing is set at 9 \(\times\) 9, the smaller size. Too small a filter window leads to local background estimation inaccuracy, and compared with Figure 2d, the defect is not a whole one but consists of discrete pieces. Figure 3b shows the segmentation result when the filter size is increased to \(51\times 51\).