# A UAV-Based Visual Inspection Method for Rail Surface Defects
## 2 Methodology
### Defect Segmentation Meltod Based on Gray Stretch Maximum Entropy
#### 2.3.2 The Gray Stretch Maximum Entropy Threshold Method  
[PERSON] et al. proposed the entropy threshold principle [47; 48] which uses entropy of image gray histogram to obtain the segmentation threshold. The maximum entropy (ME) algorithm [37] is proposed to optimize a threshold afterwards. ME method can confirm one threshold which maximizes the total content of information provided by cumulative object probability distribution \(\phi_{o}\) and cumulative background probability distribution \(\phi_{b}\). They are given by:  
\[P_{n}\,=\,\frac{f_{n}}{M},\,\,n\,\in\,[0,255] \tag{12}\]  
\[\phi_{0}\,=\,\sum_{n=0}^{T-1}p_{n},\quad\phi_{b}\,=\,1-\phi_{o} \tag{13}\]  
where \(P_{n}\) is the probability of gray value \(n\) in an image. Given a rail image \(I\) that is normalized to 256 gray levels, and the entropy of \(\phi_{0}\) and \(\phi_{b}\) is defined as:  
\[H_{o}(T)\,=\,-\!\!\sum_{n=0}^{T-1}\left(\frac{P_{n}}{\phi_{o}(T)}\ln\frac{P_{ n}}{\phi_{o}(T)}\right), \tag{14}\]  
\[H_{b}(T)\,=\,-\!\!\sum_{n=T}^{255}\left(\frac{P_{n}}{\phi_{b}(T)}\ln\frac{P_{ n}}{\phi_{b}(T)}\right) \tag{15}\]  
where \(M\) is the total pixel number of image \(I\), \(f_{n}\) is the frequency of gray value \(n\) in \(I\). An optimal threshold \(T^{*}\) can be obtained by:  
\[T*\,=\,\text{arg max}\,\left(H_{o}(T)+H_{b}(T)\right),\,\,\,T\in[0,255] \tag{16}\]  
The ME method takes into account both the distribution information of image pixel gray and the spatial information of pixels. However, its performance is not perfect for defect segmentation due to the aforementioned characteristics of rail images. Therefore, GSME algorithm is proposed in this paper, as shown in Figure 6.  
Figure 7: The 2-D DWT diagram for an image by using digital filters and down-sampling. An original 2-D image can be decomposed into four sub-image: a scaling component \(W_{\varphi}\) and three wavelet components, \(W_{\Psi}^{H}\), \(W_{\Psi}^{D}\), and \(W_{\Psi}^{V}\). In Figure 7, \(\otimes\) denotes convolution symbol, \(h_{\Psi}\) denotes low pass filter, and \(h_{\varphi}\) denotes high pass filter. 1. Based on one-level 2-D DWT algorithm, the rail image is decomposed into four wavelet coefficients that include approximation (low frequency region), horizontal, vertical, and diagonal details. 2. For low frequency region (LL region) of image decomposed by wavelet, the ME algorithm is used to obtain a segmentation threshold after reconstructing its coefficient, and then the gray stretch method is used to enhance contrast between background and foreground, as the following equations: \[f_{\varphi}(x,y)\ =\ \frac{1}{\sqrt{MN}}{\sum_{m}}{\sum_{n}{W_{\varphi}(j_{0},m,n) \varphi_{j_{0},m,n}(x,y)}},\] (17) \[{f_{\varphi}}^{*}(x,y)\ =\left\{ \begin{array}{ll}f_{\varphi}(x,y)\ -\ af_{\varphi}(x,y),\ \ if\ \ f_{\varphi}(x,y)<T^{*}\\ f_{\varphi}(x,y)\ +\ af_{\varphi}(x,y),\ \ \ otherwise.\end{array} \right.\] (18) where \(f_{\varphi}(x,y)\) denotes reconstructing image function, \(a\) denotes stretch factor and is set to and value between 0.1 and 0.5 in general. 3. For the image, its energy is mainly distributed in the low frequency region. In the high frequency area, the proportion of noise energy is large, so this study focuses on de-noising in this area. In Ref [39], [PERSON] et al. use the filter templates of three different directions for de-noising. For example, the line template of horizontal direction is used for \(W_{\varphi}^{H}\) de-noising, because the wavelet coefficients contain the high-frequency information in the horizontal direction and low-frequency information in the vertical direction of the image signal. Inspired by the median filtering method employed in wavelet domain, this study used the median filtering template of horizontal line, vertical line, and diagonal line to eliminate noise of three high frequency wavelet coefficients, respectively. 4. The rail image can be reconstructed based on discrete wavelet inverse transform algorithm.