# Battery Surface and Edge Defect Inspection Based on Sub-Regional Gaussian and Moving Average Filter
## 3 Principle of Defect Inspection Algorithm
### Moving Average Filtering (MAF)  
Figure 6 shows the grayscale variation on the surface and edge of the battery. We proposed to find a grayscale trend curve by analyzing the grayscale curve. The defect position is highlighted, and the gray background is weakened through subtraction. Therefore, we used these features to smoothen the curves, perform subtraction, highlight the defects, and set an adaptive threshold to identify the defects.  
MAF is defined by averaging a number of points from the input signal to produce each point in the output signal. MAF can be regarded as a window of a certain size (N, in this case) that moves along the array that constitutes from the input signal, one element at a time (Figure 7). The average of all elements in the current window correspond to the MAF output.  
Figure 6: The grayscale variation of original ROI image of the battery. (**a**) ROI; (**b**) The grayscale variation on a row of the ROI image.  
The relationship between the signal of the input and the output of the MAF can be clearly described in Equations (3) and (4), respectively:  
\[\sum_{n-(N-1)}^{n}X(k)=\sum_{n-N}^{n-1}X(k)-X(n-N)+X(n) \tag{3}\]  
\[Y(n)=\frac{1}{n}\sum_{n-(N-1)}^{n}X(k) \tag{4}\]  
where \(X(n)\) and \(Y(n)\) are the pixel values of the input and output signals of the MAF, respectively, and N is the size of the moving window, that is, the number of samples of the input pixel per moving period.  
Integrating Equation (3) into Equation (4), the transfer function from the MAF input to its output in the time-domain can be described in Equation (5) as follows:  
\[Y(n)=Y(n-1)+\frac{X(n)-X(n-N)}{N} \tag{5}\]  
The transfer function indicates that MAF is a finite impulse response (FIR) filter, which has linear phase characteristics and good robustness.  
Figure 8 illustrates the results of MAF. Figure 8a shows the MAF curve plots with different numbers of samples, whereas Figure 8b shows the residual changes correspondingly. MAF1 has 64 samples (\(N=64\)), MAF2 has 256 samples (\(N=256\)), MAF3 has 512 samples (\(N=512\)), MAF4 has 1024 samples (\(N=1024\)), and MAF5 has 2048 samples (\(N=2048\)) within a moving period. The analysis of the curve changes indicates that the residual is prominent when the number of samples (\(N\)) is large. However, \(N=512\) is selected as the final choice of the experiment considering the computation time and the effect of the harmonics. The appropriate threshold is set on the basis of the characteristics of the normal distribution to extract the defects, and the threshold is set to dynamic threshold in our experiment in accordance with Equation (6). Figure 8c illustrates the result of defect extraction when MAF has 512 samples (\(N=512\)).  
\[\mathrm{I_{thr}}=\mu\pm 3\sigma \tag{6}\]  
Figure 7: Principle of moving average filtering.  
Figure 8: Results of moving average filtering (MAF). (**a**) Result of MAF curve plots with different numbers of samples; (**b**) Result of residuals with different numbers of samples; (**c**) Result of defects extraction when MAF has 512 samples (\(N=512\)).  
Figure 9 shows the results of partition processing on the image and the traditional processing without partition, where MAF is processed row-by-row in the inner area \(W_{2}\) and column-by-column in the edge area \(W_{1}\).