# Reconstructing Nerve Structures from Unorganized Points
## 4 Materials and Methods
### Trajectory Renewing  
The objective of this part of the algorithm is forming an oriented graph that will, starting from the two points determined during the cluster initialization, result in a sequence of points. Once connected, the points from this sequence will form a line. The authors found that the way to accomplish this without the risk of diverging, as described in the previous section, is by combining the valley-seeking clustering method with an adaptive Kalman filter [34; 35; 36; 37; 38; 39]. If we associate point-by-point contour reconstruction with target tracking, as in the example in Figure 6, the state vector \(\mathbf{x}_{k+1}\) at time step \(k+1\) can be expressed as:  
\[\mathbf{x}_{k+1}=\big{[}x_{k+1}\quad\dot{x}_{k+1}\quad y_{k+1}\quad\dot{y}_{k+ 1}\big{]}^{T}, \tag{11}\]  
where \(x_{k+1}\) and \(y_{k+1}\) are the position coordinates, while \(\dot{x}_{k+1}\) and \(\dot{y}_{k+1}\) are the speeds along those two coordinates at time step \(k+1\). The Kalman filter has the task of estimating the future states, i.e., the point positions, based on the available measurements. If we adopt the constant velocity model, i.e., we assume that the velocity is constant between two consecutive sampling steps, we can write the following:  
\[\mathbf{x}_{k+1}=\mathbf{F}\mathbf{x}_{k}+\mathbf{w}_{k+1}, \tag{12}\]  
\[\mathbf{z}_{k+1}=\mathbf{H}\mathbf{x}_{k+1}+\mathbf{v}_{k+1}, \tag{13}\]  
where \(\mathbf{F}\) is the state transition matrix, \(\mathbf{z}_{k+1}\) is the measurement at time step \(k+1\), \(\mathbf{H}\) is the measurement (i.e., observation) matrix, while \(\mathbf{w}_{k+1}\) and \(\mathbf{v}_{k+1}\) are the process noise and the measurement noise, respectively. The transition matrix is given by:  
\[\mathbf{F}= \begin{bmatrix}1&\triangle t&0&0\\ 0&1&0&0\\ 0&0&1&\triangle t\\ 0&0&0&1\end{bmatrix}  \tag{14}\]  
Figure 6: Representation of similarity between nerve contour reconstruction and target tracking (e.g., a car) at different time steps. Black and gray points represent the previous and future trajectory, respectively. The processed data indicate the vehicle positions sampled at equal time step. A higher point density indicates a higher speed of the vehicle. where, for simplicity reasons, it was assumed that \(\triangle t=1\), and where the measurement matrix is:  
\[\mathbf{H}= \begin{bmatrix}1&0&0&0\\ 0&0&1&0\end{bmatrix} . \tag{15}\]  
With the nature of the particular points in mind, we can assume that the measurement noise \(\mathbf{v}_{k+1}\) is negligible. On the other hand, the process noise \(\mathbf{w}_{k+1}\) is very important, since its fluctuations indicate changes in trajectory, i.e., the presence of a maneuver. Prediction can be defined as:  
\[\hat{\mathbf{x}}_{k+1/k}=\mathbf{F}\hat{\mathbf{x}}_{k/k}, \tag{16}\]  
\[\mathbf{P}_{k+1/k}=\mathbf{F}\mathbf{P}_{k/k}\mathbf{F}^{T}+\mathbf{Q}_{k+1}, \tag{17}\]  
while updating can be expressed through the following set of equations:  
\[\hat{\mathbf{x}}_{k+1/k+1}=\hat{\mathbf{x}}_{k+1/k}+\mathbf{K}_{k+1}(\mathbf{ z}_{k+1}-\mathbf{H}\hat{\mathbf{x}}_{k+1/k}), \tag{18}\]  
\[\mathbf{P}_{k+1/k+1}=(\mathbf{I}-\mathbf{K}_{k+1}\mathbf{H})\mathbf{P}_{k+1/ k}, \tag{19}\]  
\[\mathbf{K}_{k+1}=\mathbf{P}_{k+1/k}\mathbf{H}^{T}(\mathbf{H}\mathbf{P}_{k+1/ k}\mathbf{H}^{T}+\mathbf{R})^{-1}, \tag{20}\]  
where \(\hat{\mathbf{x}}_{k+1/k}\) denotes the estimate for the following time step \(k+1\), \(\mathbf{P}_{k+1/k}\) and \(\mathbf{P}_{k+1/k+1}\) denote the covariance matrices of the state estimations \(\hat{\mathbf{x}}_{k+1/k}\) and \(\hat{\mathbf{x}}_{k+1/k+1}\), respectively, \(\mathbf{Q}_{k+1}\) denotes the covariance matrix of the process noise, \(\mathbf{R}\) denotes the covariance matrix of the measurement noise, while \(\mathbf{K}_{k+1}\) is the Kalman gain. Assuming that the individual components of the process noise and the measurement noise are uncorrelated, we can write that:  
\[\mathbf{R}= \begin{bmatrix}\sigma_{x}^{2}&0\\ 0&\sigma_{y}^{2}\end{bmatrix} , \tag{21}\]  
\[\mathbf{Q}_{k+1}=