# Multizone Leak Detection Method for Metal Hose Based on YOLOv5 and OMD-ViBe Algorithm
## 2 Leak Detection Methods
### Omd-ViBe
#### 2.3.2 Improving ViBe  
The traditional ViBe algorithm is prone to ghosting if moving bubbles are in the initial frame. Meanwhile, the water surface ripple can easily affect the detection resulting from image interference under the detection conditions of a metal hose. The originally fixed threshold \(R\) cannot adapt to this detection condition well. Therefore, this paper improves the background modeling and foreground detection part for the ViBe problem. The traditional ViBe algorithm builds up a background sample library by the first frame, but if there are, for example, floating bubbles or impurities in the first frame, this can cause interference pixels in the background sample library and eventually lead to false detection. Therefore, we use the first \(n\) frames of the images to improve the background initialization: the first \(n\) frames of images are used to create a total image set, each pixel point in the metal hose picture is traversed, and the pixel values are randomly extracted on  
Figure 3: Threshold segmentation. the corresponding neighborhood of the image set \(M\) times to form a background sample library \(BK_{M}\). This library is expressed as follows:  
\[BK_{M}=g_{N}(f^{n}(x,y)|(x,y)eN_{G}(x,y,M)),M=range(10)). \tag{8}\]  
where \(f^{n}(x,y)\) is the pixel value of the pixels in the neighbourhood extracted by all the pixes \((x,y)\) in the first \(n\) frames of the metal hose image, \(N_{G}(x,y)\) is the neighborhood of the pixel \((x,y)\). \(g_{N}(*)\) represents the execution of \(N\) times, and \(range(*)\) represents the random number. The main idea of the \(OTSU\) algorithm is to divide the image into background and foreground according to the gray characteristics so that the variance between background and foreground is the largest. However, the \(OTSU\) algorithm can easily misjudge the foreground point as the background in image segmentation. In this paper, the background model in the ViBe algorithm and the \(OTSU\) algorithm are combined to calculate the dynamic thresholds \(T_{1}\) and \(T_{2}\) by using the image difference idea. Additionally, the maximum intraclass and interclass variance ratio methods are introduced, and the threshold \(T_{3}\) is calculated by using the characteristics of this method. The final dynamic threshold is derived by combining the thresholds with the calculation. The \(OTSU\) and the maximum intraclass and interclass variance ratio methods rely on the mean difference and variance to calculate the results via Formulas (9) and (10). \[\left\{ \begin{array}{l}\mu_{1}=\frac{1}{N_{\rm C1}}\sum_{f(x,y)\in{\rm C1}}f (x,y),\sigma_{1}^{2}=\sum_{f(x,y)\in{\rm C1}}\left(f(x,y)-\mu_{1}\right)^{2} \\ \mu_{2}=\frac{1}{N_{\rm C2}}\sum_{f(x,y)\in{\rm C2}}f(x,y),\sigma_{2}^{2}= \sum_{f(x,y)\in{\rm C2}}\left(f(x,y)-\mu_{2}\right)^{2}\end{array} ,\right. \tag{9}\]  
\[p_{1}=\frac{N_{\rm C1}}{N_{image}},p_{2}=\frac{N_{\rm C2}}{N_{image}}, \tag{10}\]  
where \(f(x,y)\) is the picture of the video, \(C1\) belongs to the foreground set, \(C2\) belongs to the background set, \(N_{\rm C1}\) is the number of foreground pixels, \(N_{\rm C2}\) is the number of background pixels, \(N_{image}\) is the total number of pixels in the image, \(\mu_{1},\mu_{2}\) is the mean value, \(\sigma_{1}^{2},\sigma_{2}^{2}\) is the variance, and \(p_{1},p_{2}\) is the distribution probability. Based on the maximum gray value and minimum gray value of the image, the maximum of \(p_{1}\sigma_{1}^{2}+p_{2}\sigma_{2}^{2}\) is obtained by traversing the two value intervals, and the threshold \(TH\) calculated by the \(OTSU\) algorithm is accepted at this time. Then, \(T_{1}\) and \(T_{2}\) are computed using \(OTSU\) combined with the background model difference results and frame difference results. In the maximum intraclass and interclass variance ratio method, \(\mu_{1},\mu_{2}\), \(\sigma_{1}^{2},\sigma_{2}^{2}\) is used to calculate \(S_{1}\) and \(S_{2}\), and the ratio of \(S_{1}\) to \(S_{2}\) is searched from the minimum gray value to the maximum gray value to obtain \(T_{3}\).