## 3 Methods
### Calibration
#### 3.3.2 Estimating the Forward Direction  
Once \(\underline{e}_{e,z}\) is determined, the next step is to compute the corresponding \(x\)-directional vector \(\underline{e}_{e,x}\). This vector is defined to align with the direction of the rails (see Figure 1). Its determination involves two stages: first, estimating the direction approximately using the **Line of**Sight (LoS), and second, refining it to ensure \(\underline{e}_{e,x}\) runs precisely parallel to the rails. First, we estimate the LoS, which we define as the primary direction the camera is oriented along at ground level. This direction corresponds to the line of intersection between the camera's \(y,z\)-plane and the ground-level plane. Its directional vector \(\underline{e}_{los}\) can be computed as follows:  
\[\underline{e}_{los}=\underline{e}_{e,z}\times\underline{e}_{e,x}\,. \tag{5}\]  
In camera coordinates, the camera's unit vector in the \(x\)-direction is \(\underline{e}_{e,x}=[1,0,0]^{T}\). It is important to note that \(\underline{e}_{los}\) does not necessarily align with the rails, as the camera is usually angled relative to the rail direction. However, assuming the camera is mounted in the _driver's view_, \(\underline{e}_{los}\) is expected to be approximately parallel to the rails. To accurately align the \(x\)-directional vector, we optimize the rotation of \(\underline{e}_{los}\) around the \(\underline{e}_{e,z}\)-axis so that the resulting \(x\)-axis lies precisely parallel to the rails. For this, we define the initial \(x\)- and \(y\)-directional vectors of the _ego_ coordinate system as:  
\[\underline{e}_{e,x}^{init}=\underline{e}_{los}\,,\underline{e}_{e,y}^{init}= \underline{e}_{los}\times\underline{e}_{e,z}\,. \tag{6}\]  
The algorithmic process for this initial step of estimating the ego coordinate system is presented in Appendix A, Algorithm A2. Next, we project \(\mathbf{P}_{d}^{r}\) onto the ground plane:  
\[\underline{k}^{T} =\frac{\underline{e}_{e,z}^{T}}{\|\underline{e}_{e,z}\|_{2}^{2}} \cdot\left(\underline{M}_{d}^{r}\cdot\mathbf{J}_{1,N}-\mathbf{P}_{d}^{r^{\prime}} \right), \tag{7}\] \[\mathbf{P}_{g}^{r} =\mathbf{P}_{d}^{r}+\underline{e}_{e,z}\cdot\mathbf{k}^{T}\,,\] (8) \[\underline{P}_{g,x}^{r} =\left[\left(\underline{e}_{e,x}^{init}\right)^{T}\cdot\mathbf{P}_{g} ^{r^{\prime}}\right]^{T},\] (9) \[\underline{P}_{g,y}^{r} =\left[\left(\underline{e}_{e,y}^{init}\right)^{T}\cdot\mathbf{P}_{g} ^{r^{\prime}}\right]^{T},\] (10) \[\mathbf{P}_{g}^{r} =\left\{\underline{P}_{g,x}^{r},\underline{P}_{g,y}^{r}\right\}^ {T}. \tag{11}\]  
All projected points, \(\mathbf{P}_{g}^{r}\), are now in 2D within the ground-level coordinate system. This coordinate system has its origin at \(\underline{M}_{d}^{r}\), with the \(x\)-axis aligned along \(\underline{e}_{los}\). To refine the alignment, we rotate all points \(\mathbf{P}_{g}^{r}\) around their center of mass until the rails are optimally parallel to the \(x\)-axis. The optimization objective is based on the histogram of the distribution of the \(y\)-coordinates. When the rails are perfectly aligned with the \(x\)-axis, the histogram exhibits the maximum deviation from a uniform distribution, as illustrated in To quantify the alignment, we use the chi-square test against a uniform distribution as the optimization objective:  
\[\chi^{2}=\sum_{j=1}^{n}\frac{(n_{j}-n_{0,j})^{2}}{n_{0,j}}\,, \tag{12}\]  
where \(n_{j}\) represents the observed frequency of the \(j\)-th bin, and \(n_{0,j}\) is the expected frequency of the \(j\)-th bin. Since we are testing against a uniform distribution, the expected frequency is equal for all bins:  
\[n_{0,j}=\frac{n}{n_{bins}}\,, \tag{13}\]  
where \(n\) denotes the total number of elements in \(\underline{P}_{g,y}^{r}\).