# Sewer Cleaning Robot: A Visually Assisted Cleaning Robot for Sewers
## 3 Software Algorithm Design
### Monocular Visual Localization
The gradient magnitude (\(G\)) and direction (\(\theta\)) are computed using the following equations:  
\[G=\sqrt{G_{x}^{2}+G_{y}^{2}},\quad\theta=\arctan\left(\frac{G_{y}}{G_{x}}\right) \tag{7}\]  
In Equations (7), \(G_{x}\) and \(G_{y}\) represent the horizontal and vertical gradients of the image, respectively. The gradient magnitude \(G\) reflects the strength of the edge at each pixel, while the gradient direction \(\theta\) indicates the orientation of the edge relative to the horizontal axis. By isolating the edges within the defect bounding boxes, this approach reduces the computational complexity and enhances the precision of subsequent monocular vision localization. These extracted edges serve as key features for matching across the two viewpoints, enabling accurate depth estimation and precise positioning of the defect within the pipeline environment. The detected edge information is then restored to its original position in the image. ORB feature detection and descriptor computation are performed on the edge-detected image, and feature matching is carried out using the Hamming distance:  
\[H(\mathbf{d}_{1},\mathbf{d}_{2})=\sum_{i=1}^{n}|\mathbf{d}_{1}[i]-\mathbf{d}_ {2}[i]| \tag{8}\]  
where \(\mathbf{d}_{1}[i]\) and \(\mathbf{d}_{2}[i]\) are the \(i\)-th bits of the respective descriptors, and \(n\) is the length of the descriptor. The Hamming distance measures the number of bit positions where the two descriptors differ, effectively quantifying their dissimilarity. To optimize the results, the RANSAC algorithm is used to filter out noise and incorrect matches. The RANSAC algorithm iteratively selects subsets of feature matches to estimate the fundamental matrix \(F\) while reducing outliers caused by incorrect feature correspondences. The fundamental matrix \(F\) is a \(3\times 3\) matrix that encapsulates the epipolar geometry relationship between two views. It satisfies the following constraint for corresponding points \(\mathbf{x}\) and \(\mathbf{x}^{\prime}\) in the two images:  
\[\mathbf{x}^{\prime\top}F\mathbf{x}=0 \tag{9}\]  
In Equations (9), \(\mathbf{x}\) and \(\mathbf{x}^{\prime}\) are the homogeneous coordinates of a point in the first and second images, respectively. The constraint ensures that point \(\mathbf{x}^{\prime}\) lies on the epipolar line corresponding to \(\mathbf{x}\). Compared to direct ORB feature matching applied only within the bounding box, integrating Canny edge detection focuses feature points specifically on the target edge regions. This selective approach minimizes interference from irrelevant background details, which is particularly beneficial in complex pipeline scenarios with repetitive textures or occlusions. By concentrating feature points along the object boundaries, the matching process achieves improved accuracy and computational efficiency. After computing the fundamental matrix \(F\) using the RANSAC algorithm, the essential matrix \(E\) can be derived by incorporating the camera intrinsic matrix \(K\):  
\[E=K^{\top}FK \tag{10}\]  
The essential matrix \(E\) encodes the relative rotation and translation between two camera views, and it forms the foundation for recovering the camera motion. To extract the rotation matrix \(R\) and translation vector \(\mathbf{t}\) from the essential matrix \(E\), singular value decomposition (SVD) is performed. The essential matrix \(E\) is decomposed as follows:  
\[E=U\cdot\Sigma\cdot V^{\top} \tag{11}\]  
where \(U\) and \(V\) are orthogonal matrices, and \(\Sigma=\text{diag}(s,s,0)\) is the singular value matrix, with \(s\) representing the non-zero singular values of \(E\). Based on this decomposition, two possible rotation matrices, \(R_{1}\) and \(R_{2}\), and two possible translation vectors, \(\pm\mathbf{t}\), can be obtained. To determine the correct combination of \(R\) and \(\mathbf{t}\), a triangulation step is performed, ensuring that the reconstructed 3D points lie in front of both camera planes.