## 4 Binocular Ranging Algorithm  
According to the parallax theory, binocular vision positioning is based on the geometric relationship between the camera plane and the object to be recognized. The three-dimensional position information of the object is obtained through \"feature matching\" and \"triangulation principle\". In order to reduce the computational load and appropriately ease the matching difficulty, the images captured by the binocular camera need to be calibrated in the epipolar direction, making it an ideal binocular vision system. Through the stereo multi-dimensional matching technology, the correspondence between the points of the left and right images is determined to obtain the parallax, and then the depth and three-dimensional information of the image to be recognized is obtained according to the projection model. In general, stereo matching has always been a critical, yet difficult, question in stereo vision technology due to factors such as distortion, noise, specular reflection, and projection reduction. The stereo matching method usually consists of four processes: the calculation of the matching cost, the aggregation of the cost, the acquisition of the disparity, and the refinement of the disparity. Among these steps, the calculation of the matching cost is the basis of the whole algorithm, which is the grayscale similarity detection under different parallaxes. Common detection indicators include the square or absolute value of the grayscale difference, corresponding to the different cost aggregation algorithms are adopted. After the matching costs are added, the disparity calculation of the local algorithm selects the minimum matching cost within a certain range as the matching point, while the global algorithm directly calculates the original matching cost to obtain the minimum value of the evaluation function. In addition, for some occasions with high-precision requirements, parallax refinement processing, such as image filtering, segmentation, and matching cost curve fitting, is also required. SGM, known as semi-global matching algorithm, is represented modularly as the semi-global block matching (i.e., SGBM) in OpenCV. In the algorithm, the difference map  
Figure 3: Description of key points: (**a**) image gradients around key points; (**b**) keypoint descriptors. is constructed by selecting the difference of the pixel points, and the global cost function, which is related to the difference map, is set and minimized to solve the optimal difference of pixels [20]. The effect of SGBM stereo matching is better than that of local algorithms, but at the same time more complicated. The specific process of the algorithm is as follows:  
(1) Preprocessing:  
Step1: Use the horizontal Sobel operator for image processing. Step2: Map the image pixels into a new image. (2) Cost calculation:  
Step3: The gradient cost of the preprocessed image is obtained by sampling method. Step4: The SAD cost of the original image is obtained based on sampling method, and is superimposed with the gradient cost. (3) Dynamic programming:  
Step5: Establish a global Markov energy equation, and superimpose the full path information to calculate the pixel matching cost. Step6: Add the multi-directional matching cost to obtain the total matching cost. (4) Postprocessing:  
Step7: Uniqueness detection. Step8: Sub-pixel interpolation. Step9: Consistency detection of the left and right images. The SGBM algorithm attempts to establish a global Markov energy equation through the constraints of one-dimensional paths in multiple directions on the image. The final matching cost of each pixel is the superposition of all path information, and the disparity selection of each pixel is simply decided by WTA (Winner Takes All). The energy is accumulated in each direction according to the idea of dynamic programming, and then the matching costs in each direction are added to obtain the total matching cost, as shown in formula (4):  
\[L_{\mathrm{r}}(p,d)=c(p,d)+\min\left\{