# Visual SLAM Mapping Based on YOLOv5 in Dynamic Scenes
## 3 Dynamic Feature Point Rejection
### Dynamic Feature Point Detection Based on Optical Flow Method  
The LK optical flow method is a two-frame differential optical flow estimation algorithm that computes a sparse optical flow field, based on the following features: 1. constant luminance: the luminance or color of adjacent frames is constant; 2. temporal continuity: continuous \"small motion\"; and 3. spatial consistency: the motion between adjacent pixels is consistent. The gray value of a point \((x,y)\) in the image at time \(t\) is \(I(x,y,t)\), and at time \((t+\Delta t)\) the point moves to a new position, \((x+\Delta x,y+\Delta y)\), and the gray value becomes \(I(x+\Delta x,y+\Delta y,t+\Delta t)\). According to the luminance constancy assumption, \(\partial I(x,y,t)/\partial t=0\), the grayscale value of the point remains constant before and after the motion; therefore:  
\[I(x,y,t)=I(x+\Delta t,y+\Delta y,t+\Delta t) \tag{5}\]  
According to the time-continuous assumption that the image motion is tiny, using the Taylor expansion for Equation (5), and ignoring the second-order infinitesimal, we get:  
\[\frac{\partial I}{\partial x}\Delta x+\frac{\partial I}{\partial y}\Delta y+ \frac{\partial I}{\partial t}\Delta t=0 \tag{6}\]  
Let \(u=\Delta x/\Delta t,v=\Delta y/\Delta t\) be the velocity of the optical flow in the \(x\) and \(y\) directions, respectively. Then, Equation (6) can be rewritten as:  
\[I_{x}u+I_{y}v+I_{t}=0 \tag{7}\]  
in which \(I_{x}=\frac{\partial I}{\partial x}\), \(I_{y}=\frac{\partial I}{\partial y}\), and \(I_{t}=\frac{\partial I}{\partial t}\) are the partial derivatives of the grayscale values concerning \(x\), \(y\), and \(t\), respectively. Equation (8) is the optical flow constraint equation, which includes two unknown variables \(u,v\). According to the assumption of spatial consistency, a small space \(w\) of \(m\times m\) can be established, and the optical flow \(U=(u,v)^{T}\) remains constant within the space \(w\). Then, a system of overdetermined equations can be obtained, and the least squares method can be applied to the system of equations to estimate \(u\) and \(v\). In the range of space \(w\), the error function of the optical flow equation can be expressed as: \(E=\sum\limits_{x,y\in w}{(I_{x}u+I_{y}u+I_{t})}^{2}\). The derivative of the function \(E\) is obtained for both \(u\) and \(v\), and when the derivative value is 0, the estimated \(E=(u,v)^{T}\) of \(u\) and \(v\) is obtained, which is the optical flow vector at that point. Figure 5: Dynamic object rejection algorithm framework. \[ \begin{bmatrix}u\\ v\end{bmatrix} = \begin{bmatrix}\sum\limits_{w}I_{x}^{2}&\sum\limits_{w}I_{x}I_{y}\\ \sum\limits_{w}I_{x}I_{y}&\sum\limits_{w}I_{y}^{2}\end{bmatrix} ^{-1} \begin{bmatrix} \sum\limits_{w}I_{x}I_{t}\\ \sum\limits_{w}I_{y}I_{t}\end{bmatrix}  \tag{8}\]  
When \( \begin{bmatrix}\sum\limits_{w}I_{x}^{2}&\sum\limits_{w}I_{x}I_{y}\\ \sum\limits_{w}I_{x}I_{y}&\sum\limits_{w}I_{y}^{2}\end{bmatrix} \) is invertible, find \(U=(u,v)^{T}\), at the location in the image where the two directional edges intersect along the line, where the point has two larger eigenvectors that can ensure matrix integrability. The three assumptions of the LK optical flow algorithm are not easily satisfied, especially when the target speed is fast, and the optical flow vector cannot be calculated using the traditional LK optical flow algorithm. Therefore, in this paper, combining the LK optical flow method, the pyramid optical flow method is used to calculate the optical flow for different pyramid layers, which improves the accuracy of the optical flow calculation. The basic idea of the pyramid optical flow method is as follows: First, calculate the optical flow field at the top layer of the pyramid, and then at each step pass the optical flow information to the next lowest layer, until finally to the original image is reached at the bottom; then, set the bottom layer as level 0, and upgrade upwards through the layers in turn.