# Visual SLAM Mapping Based on YOLOv5 in Dynamic Scenes
## 3 Dynamic Feature Point Rejection
### Dynamic Feature Point Detection Based on Optical Flow Method
The level 0 image is down-sampled to get the level 0 pyramid image, and then continues down-sampling to the set number of layers. At this point, the motion of the moving target between two adjacent frames is small enough to meet the assumption bar of the LK optical flow method, and the optical flow value of the layer can be calculated. As shown in Figure 6, the calculation principle of the pyramid optical flow method is as follows: firstly, the image is divided into different pixel levels such as \(w^{n}\), \(w^{k}\), \(w^{1}\), \(w^{0}\), etc. Then calculate \(w^{k}\),\(w^{1}\),\(w^{0}\) from the minimum pixel level in turn, and finally extract and reject the dynamic features of the image. The pyramid optical flow algorithm starts from the topmost level and iterates the optical flow calculation through the mapping relationship between each pyramid level to obtain the optical flow of the original image at the bottom level. Suppose the initial value of the top optical flow is \(w^{n}=(u^{n},v^{n})^{T}=(0,0)^{T}\). After that, we calculate the value of the top optical flow, and the obtained calculation result is expressed as \(dw^{n}=(du^{n},dv^{n})^{T}\). Then, the initial value \(w^{n}\) is added to obtain the initial value \(w^{n-1}=(u^{n-1},v^{n-1})^{T}=w^{n}+dw^{n}\) for the next level of calculation. Next, substitute this value to a higher resolution layer for optical flow calculation, repeat the process until the highest resolution of the original image is reached, i.e.,:  
\[w^{k}=w^{k+1}+dw^{k+1}=(u^{k+1}+du^{k+1},v^{k+1}+dv^{k+1})^{T} \tag{9}\]  
Figure 6: Flow chart of the pyramid optical flow method. The algorithm in this paper uses pyramidal optical flow to track the feature points in the image when performing dynamic feature point detection, so that the correspondence between the feature points can be obtained. When using the feature point method for feature point detection, it is necessary to calculate the descriptors and perform the matching process of feature points, whereas the optical flow method avoids the time required to calculate and match the descriptors, and therefore has better real-time performance. By tracking the optical flow information between each frame in a dynamic environment, the relationship between matching feature points in neighboring frames is obtained, and thus the position relationship between each frame is recovered. The basis matrix \(F\) describes this relative transformation relationship, and its solution can be computed using eight pairs of matching points. Given a pair of matched points whose normalized coordinates are \(p_{1}=\left[u_{1},v_{1},1\right]^{T}\) and \(p_{2}=\left[u_{2},v_{2},1\right]^{T}\), they satisfy between:  
\[s_{1}p_{1}=KP,s_{2}p_{2}=K(RP+t) \tag{10}\]  
where: \(K\)--internal reference matrix; \(R\)--rotation matrix; and \(t\)--translation matrix. According to the pair of polar geometric constraints, we have:  
\[(u_{2},v_{2},1) \begin{bmatrix}e_{1}&e_{2}&e_{3}\\ e_{4}&e_{5}&e_{6}\\ e_{7}&e_{8}&1\end{bmatrix}\begin{pmatrix}u_{1}\\ v_{1}\\ 1\end{pmatrix} =0 \tag{11}\]  
where \(F\) is the matrix in the middle, and Equation (11) can be further written as:  
\[p_{2}^{T}Fp_{1}=[u_{2},v_{2},1]F[u_{1},v_{1},1]^{T}=0 \tag{12}\]  
Using the basis matrix \(F\), the key point \(p_{1}\) in the previous frame can be projected into the current frame to obtain the polar line \(l_{2}\) in the current frame. The distance from the key point to the polar line obtained by optical flow tracking is calculated to determine whether it is a dynamic point or not. \[l_{2}=[X,Y,Z]^{T}=Fp_{1}=F[u_{1},v_{1},1]^{T} \tag{13}\]  
where \(X\), \(Y\), \(Z\) are the normal vectors of the polar lines and \(F\) is the basis matrix. The distance \(D\) from the matching feature \(p_{2}\) of the optical flow tracking to the corresponding polar line \(l_{2}\) can be calculated by Equation (14). \[D=\frac{\left|p_{2}^{T}Fp_{1}\right|}{\sqrt{\left\|X\right\|^{2}+\left\|Y \right\|^{2}}}>\sigma \tag{14}\]  
where \(\sigma\) is the threshold value.