# Measurement of Large-Sized-Pipe Diameter Based on Stereo Vision
## 2 Materials and Methods
### Three-Dimensional Reconstruction
Taking an image captured by the left camera as an example, for three adjacent laser markers \(P_{L}O_{L}Q_{L}\) along the one-dimensional laser marker direction, their matching element is defined as the ratio of the \(P_{L}O_{L}\) pixel length to the \(P_{L}Q_{L}\) pixel length, namely  
\[F_{L}(O)=\frac{\|P_{L}-O_{L}\|}{\|P_{L}-Q_{L}\|}, \tag{4}\]  
\(F_{L}\) and \(F_{R}\) are the matching element sets of the images captured by the left and right cameras, respectively. To obtain the longest segment of the matching sequence in \(F_{L}\) and \(F_{R}\), this paper uses dynamic programming for matching, whose corresponding state-transition equation is  
\[dp[i][j]=\left\{ \begin{array}{c}0,when\ i=0\ or\ j=0\\ dp[i-1][j-1]+1,when\ |F_{L}[i]-F_{R}[j]|\leq\text{threshold}\\ 0,when\ |F_{L}[i]-F_{R}[j]|>\text{threshold}\end{array} \right., \tag{5}\]  
where \(dp\) is the state-variable matrix; \(dp[i][j]\) is the current state, which represents the longest matching length of the first \(i\) matching elements of FL and the first \(j\) matching elements of FR; and \(dp[i-1][j-1]\) is the previous state. Then the optimal matching solution can be indicated by the position and length corresponding to the maximum element in the \(dp\) matrix. Figure 4: Schematic diagram of affine distance transformation. Figure 5: Laser markers distribution. The flowchart of feature point matching is shown in Figure 6, which is divided into two steps: coarse matching and exact matching. The specific coarse matching steps are as follows: (1) According to Formula (4), the feature points \(A\) and \(B\) extracted from the images captured by left and right cameras are converted into matching elements \(P_{A}\) and \(P_{B}\), respectively; (2) Both matching elements \(P_{A}\) and \(P_{B}\) receive regular normalization to realize normal distribution. The slope between two adjacent points is then calculated. Based on the initial parameter \(n\), the complete slope space is evenly divided into \(n\) regions, which are \([0,1,\dots\ n-1]\); (3) The matching elements are symbolized for regions corresponding to each slope in the slope sequence. The slopes are roughly classified by replacing every slope with the label of the region where the slope is located; (4) Dynamic programming is performed on the two groups of matching elements after symbolization. If the maximum element in the \(dp\) matrix occurs more than once, which means the optimal solution is not unique, the parameter \(n\) is adjusted and the process returns to the second step. Such iteration continues until a unique optimal solution is achieved; (5) In order to ensure the accuracy of matching, when the conditions for ending the iteration are met, the matching sequence numbers corresponding to the optimal solution and the sub-optimal solution are selected as the coarse matching results. It is noteworthy that the sub-optimal solution is the matching sequence corresponding to the second-largest element in the \(dp\) matrix. During exact matching, correlation calculation is performed on the candidate matching results from coarse matching, and the result from the group with the highest degree of correlation is taken as the final matching result. Figure 6: Feature point matching flowchart.