## 3 Image Edge Detection and Segmentation for Pipe Defects
### Morphological Operation  
The shape of features in an image is typically the focus of morphological operation. It has been applied to eliminate defects in a variety of shapes with the purpose of smoothing the contours and preserving the object's size and shape [33]. Erosion and dilation are the two main operations.  
Let \(f(m,n)\) be the input image and \(g(m,n)\) be a structure element, If the set of real integers is denoted by \(Z\), while assuming that \((m,n)\) is an integer \(f(m,n)\) from \(Z^{*}Z\), \(g(m,n)\) is a function of a pixel's gray value with the given coordinates \((m,n)\) and the gray value is also an integer. Namely, \(g(m,n)\) to \(f(m,n)\) for grayscale dilation can be defined as \(f\oplus g\), which is shown as Equation (7).  
\[(f\oplus g)(s,t)=\max\Bigl{\{}f(s-m,t-n)+g(m,n)\ |\ (s-m),(t-n)\in D_{f};(m,n)\in D _{g}\Bigr{\}} \tag{7}\]  
Equation (7) \(D_{f}\), \(D_{g}\) is the definition domain of \(f(m,n)\) and \(g(m,n)\), respectively, \(g(m,n)\) is the structural element of the morphological treatment is also a function, the displacement parameters \((s-m)\), \((t-n)\) must be in the definition domain of the function \(f(m,n)\).  
The dilation operation is to find the local maximum value, and the anchor point is assigned the maximum of pixels in the nucleus coverage area. The erosion operation is to find the local minimum value. The minimum value of the pixel in the kernel coverage area is assigned to the anchor point. Erosion of grayscale image is defined as Equation (8).  
\[(f\ominus g)(s,t)=\min\Bigl{\{}f(s+m,t+n)-g(m,n)\ |\ (s+m),(t+n)\in D_{f};(m,n)\in D _{g}\Bigr{\}} \tag{8}\]  
Equation (8) \(D_{f}\), \(D_{g}\) is the definition domain of \(f(m,n)\) and \(g(m,n)\), respectively, \(g(m,n)\) is the structural element of the morphological treatment is also a function, the displacement parameters \((s+m)\), \((t+n)\) must be in the definition domain of the function \(f(m,n)\).  
The expressions for the opening operation and closing operation of the grayscale image have the same form as the erosion and dilation, and the structural element \(g(m,n)\) performing the opening operation on the image \(f(m,n)\) can be defined \(f\circ g\), in Equation (9).  
\[f\circ g=(f\ominus g)\oplus g \tag{9}\]  
Figure 9: Comparison of pipeline defect image threshold methods.  
The opening operation is an erosion operation of \(g\)(\(m\),\(n\)) on \(f\)(\(m\),\(n\)) followed by an dilation operation on the result of the erosion. A similar closing operation of \(g\)(\(m\),\(n\)) on \(f\)(\(m\),\(n\)) can be defined \(f\cdot g\) in Equation (10).  
\[f\cdot g=(f\oplus g)\circ g \tag{10}\]  
In addition to the above operations, there is the morphological gradient operation; let the gradient be denoted by h in Equation (11).  
\[h=(f\oplus g)-(f\odot g) \tag{11}\]  
The basic morphological operations: closed operation (Figure 10B1-B3), dilation (Figure 10C1-C3), erosion (Figure 10D1-D3), morphological gradient (Figure 10E1-E3), and opening operation (as in Figure 10F1-F3) are applied to the threshold segmentation image obtained above (Figure 10A1-A3) to compare the results. As shown in Figure 10, The opening operation's result is to remove the image area that is slightly relative to the structure element and keeping the image area that is larger than the structure element [34]. The open operation more fully preserves the overall impact, and in this paper, we use the opening operation to fill the contours of defects in the images.